Jump to content

PHP to create a Button While loop help.

TL;DR
I need PHP to generate multiple buttons that can call POST different data. 

 

Long version:
I'm doing some PHP work that interacts with a database. I'm mostly a C# Game programmer. 
I need the website to generate buttons that let me perform actions on the database.

In my specific circumstance, I am seeking to count +1 for the Table entry every time someone clicks a button. (very primitive voting system. I'm learning PHP & web development as I work on this.)

 

I don't know how to create a button in PHP & searching google mostly brought me HTML forms that tell you how to call a .php file. 

 

Code:

Spoiler

<?php
  echo "Vote here by clicking a button! <br>";
  
  $sql = "SELECT * FROM tableBeingCalled;";
  $results = mysqli_query($conn, $sql);
  $resultsCheck = mysqli_num_rows($results);
  
  if ($resultsCheck > 0) {
	  while ($row = mysqli_fetch_assoc($results)) {
		echo $row['nameOfEntry']; //Prints each item from the table. 
		//I need a way to generate the button here. 
	  }
  } else {
	  echo "No data in table.";
  }
  
?>

 

 

I have other code that posts & adds new items to the table so when people reload the page, they can see the new items that were added by other users. (It also has duplication protection.) My thoughts were I could generate a post & then pass the "nameOfEntry" value as the post value. 

Link to comment
https://linustechtips.com/topic/990216-php-to-create-a-button-while-loop-help/
Share on other sites

Link to post
Share on other sites

It goes kinda like this:

 

<html>
<head>
<title>Test page</title>
</head>
<body>
<?php 


if (isset($_REQUEST['the_button']) == TRUE) {
	// data was submitted to the server and PHP copied "the_request" with the value "1" to
	// the _REQUEST array.
	
	echo '<p>You\'ve submitted the form!</p>';
	if (isset($_REQUEST['question']) == TRUE) {
		echo '<p>You have selected the answer with the value '.$_REQUEST['question'].' !</p>';
	} else {
		echo '<p>You did not pick an answer :sadface: !</p>';
	}
	
}

// vv entert the name of the php file in action or leave empty to have form 
// data submitted to current php page 
?>
<form method="POST" action="index.php">
<input name="question" type="radio" value="1" />My first answer <br/>
<input name="question" type="radio" value="2" />My 2nd answer <br/>
<input name="question" type="radio" value="3" />My 3rd answer <br/>
<input name="the_button" type="submit" value="Hit me!" />

</form>
<?php
?>
</body>
</html>

 

Remember to escape submitted data before entering it into database, validate it (make sure it's a number if the column in database is numeric) and escape data you send to screen using functions like htmlspecialchars  (unless you're absolutely sure the text has no html tags or stuff that could corrupt the page layout)

 

If you want actual buttons, you can wrap each submit button in its own form, or you can have multiple inputs in the form with type submit  and only the one you hit should get sent to the server (so you should check with isset if the entry exists in the _REQUEST array)

 

Link to post
Share on other sites

Something like this:

(implying you have an id column that is numeric)
<?php
	if ($_SERVER['REQUEST_METHOD'] == 'POST') { // checks if it's a POST request
		if (
			is_empty($_POST['vote']) || // if nothing was sent OR
			!ctype_digit($_POST['vote']) // if the vote value is not numeric
		) {
			echo "Invalid vote";
		} else {
			$sql = "UPDATE tableBeingCalled SET votes = votes + 1 WHERE `id` = " . $_POST['vote'];
			$vote = mysqli_query($conn, $sql);

			if (mysqli_affected_rows($conn) === 0)
				echo "Unknown entry";
			else
				echo "Vote successful";
			
		}
	}
?>

[...]

<form action="" method="post">
<?php
	// code here that gets the entries

	while (...) {
		echo "<button type=\"submit\" name=\"vote\" value=\"" . $row['id'] . "\">" . $row['nameOfEntry'] . "</button>";
	}
?>
</form>

 

It's a bit rusty because I don't usually do PHP, heh

🙂

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×