For example, say you have a loop that produces a button every time it loops. So if the loop ran five times, there would be 5 buttons. Now say i have a variable like $num which will change each time the loop loops. Is there a way so that each button will pass its individual $num when the button is clicked?
$num = 0;
while(...){
//want this to send $num via POST or GET to xyz.php
echo '<button type="submit" value="3" name="editId">Remove</button>';
$num++;//$num changes every instance of the loop
}
Open your web browser and type your localhost address followed by '\form1. php'. Output: It will open your form like this, asked information will be passed to the PHP page linked with the form (action=”form2. php”) with the use of the POST method.
Three ways can pass the variable through the navigation between the pages: Put the variable into session in the first page, and get it back from session in the next page.
To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.
The easiest way is to create a <form>
surrounding each of the <button>
tags:
$num = 0;
while(...){
//want this to send $num via POST or GET to xyz.php
echo '<form method="post" action="xyz.php">';
// Pass $num into the form value
echo '<button type="submit" value="' . $num. '" name="editId">Remove</button>';
echo '</form>'
$num++;
}
Whichever is clicked will be passed to xyz.php
as $_POST['editId']
.
While this method creates a different form for each button, there are other ways to do it using JavaScript which would use a single form and assign the value to a hidden input based on the button clicked. If it's only the one input value being sent, the multiple <form>
s option is probably easiest to code.
The button's name & value should be submitted with the form.
For example:
<?php print_r($_POST); ?>
<form action="" method="post">
<button type="submit" name="button" value="value">Button</button>
</form>
When the button "Button" is clicked, the form will be submitted and
Array ( [button] => value )
Will be output above the form.
Applying this to your situation, one could use something like:
$num = 0;
while(...){
// Set $num as the value of each button
echo "<button type='submit' value='{$num}' name='editId'>Remove</button>";
$num++;//$num changes every instance of the loop
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With