php get value of submit button when value is a variable - php

I tried to search but was unable to find an answer for this question.
I am trying to get the value of the button in my submit button that is a variable.
CODE is as follows
$penrequest = "select * from request where status='pending';";
$penreg = mysql_query($penrequest);
echo "<form method='post' action=''>";
while ($row = mysql_fetch_array($peneg))
{
echo "<input type='submit' name='answer' value='$appdeny'>";
}
if (isset($_POST['answer']))
{
echo $appdeny;
}
Ok the code works...if you hit the button its caught by the if statement like its supposedt o be. the variable $appdeny is a messageID number filled from a mysql database which can change. When the user clicks a button i want to print the messageID of the number displayed as the value of the answer button.

Change:
echo "<input type='submit' name='answer' value='$appdeny'>";
to:
echo "<input type='submit' name='answer' value='" . $row['appdeny'] . "'>";
Change:
echo $appdeny;
to:
echo $_POST['answer'];
You also need to do:
echo "</form>";
after the while loop.

Related

update mysql datarow from php form with button

I am trying to update a php form that holds a few rows of mysql data. I have a button next to each row and when i click on that I want to update the row. The issue im having below is the ID is only set as the last row. How do i get this to push the ID to the button? So basically no matter what button i press i always get the same ID which is the last one to load.
if($result){
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$beername = $row["BeerName"];
$beertype = $row["BeerType"];
$beerpercent = $row["BeerPercent"];
$beerdescription = $row["BeerDescription"];
$nowpouring = $row["NowPouring"] =='0' ? '' : 'checked=\"checked\"';
$glutenreduced = $row["GlutenReduced"] =='0' ? '' : 'checked=\"checked\"';
$beertogo = $row["BeerToGo"] =='0' ? '' : 'checked=\"checked\"';
echo "<form action='' method='POST'>";
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id\" value=\"$id\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerName\" value=\"$beername\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerType\" value=\"$beertype\"></h6></td>";
echo "<td><h6><textarea size=\"90\" style=\"width:250px;height:150px;\" name=\"BeerDescription\" value=\"\">$beerdescription</textarea></h6></td>";
echo "<td><h6><input type=\"text\" size=\"5\" name=\"Percent\" value=\"$beerpercent\"></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"NowPouring\" value=\"true\" $nowpouring></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"GlutenReduced\" value=\"true\" $glutenreduced></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"BeerToGo\" value=\"true\" $beertogo></h6></td>";
#echo "<td><h6> <a href=\". $_SERVER["PHP_SELF"] .?id=".mysql_result($result,$j,'id')."\" onclick=\"\"></h6></td>";
echo "<td><h6> <button name=\"submit\" type=\"submit\" value=\"$id\">Save</button></h6></td>";
echo "</tr>";
echo "</form>";
}
}
if (isset($_POST['submit'])) {
$user = $_POST['submit'];
echo "<p style=\"color:#ffffff\">$id</p>";
#$delet_query = mysqli_query($mysqli, "UPDATE NowPouring SET NowPouring = '1' WHERE ID = '4'") or die(mysql_error());
if ($delet_query) {
echo '<p style="color:#ffffff">Beer with id '.$id.' is updated. To refresh your page, click ' . ' <a href=' . $_SERVER["PHP_SELF"] . ' > here </a></p>';
}
}
?>
The main problem I see here is that the while loop your code has is generating the same name for the inputs...
All of your "<button name=\"submit\" type=\"submit\" value=\"$id\">Save</button>" will have the same name, that's why it always has the last id as value.
Maybe you should try something such as..
<button name=\"$id_submit\" type=\"submit\" value=\"$id\">Save</button>
or if you want you can store it in an array..
<button name=\"submit[]\" type=\"submit\" value=\"$id\">Save</button>
You are seeing this result because the 'name' of each of your inputs is the same, so essentially you have a form with a bunch of elements that have the same names. You need to add a dynamic aspect to each name.
For example, you could update your output to something like this:
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id_$id\" value=\"$id\"></h6></td>";
Where each line adds the current id. Then when you retrieve the form data, you can append the submitted id to the field you want to update.
Have you considered using an AJAX approach so you can submit just the line in question and not have to reload the page and return the whole data set each time?
Make <form> for each submit button. Adding <form> in the while():
if($result){
while($row = mysqli_fetch_array($result)){
echo "<form action='' method='POST'>";
//...
echo "</form>";
}
}
Your form tag is placed at the wrong place.
It should be within:
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
//....
//....
echo "<form action='' method='POST'>";
echo"<tr>";
echo "<td>" . $id . "</td>";
//....
//....
echo "<td><button type='submit' name='submit'>Save</button></td>";
echo"</tr>";
echo "</form>";
}

How to pass a textbox value in to url parameter

i have some problem in here. I have textbox value inside table, and when i clicked link "Valid", i can pass the textbox value to another page.
This is my textbox code in table
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "</form>";
This is my button
echo "<td class='center'>";
echo "<a class='btn btn-primary btn-sm' href='validasi.php?idorder=" . $row['id_order'] . "&pilih=" . $_GET['pilih'] . "'>
<i class='fa fa-check-square-o'>Valid</i></a>
echo "</td>";
Maybe someone can give me a solution, Thank you and have a nice day!!
In the page validasi.php add this:
$IDorder = $_GET['idorder'];
$Pilih = $_GET['pilih'];
Now you have two variables on validasi.php called IDorder and Pilih from your form
Or is it the nomortempat you want?
I think you can just add
$nomortempat = $_GET['nomortempat'];
to the validasi page and it should work. If I understand your code it should be sent
EDIT I was wrong, you need to add a sumbmit button to your form.
Add session start at the top of your pages: session_start();.
Then add this to your first page:
$_SESSION["idorder"] = $idorder;
$_SESSION["pilih"] = $pilih;
Then this code to your form:
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' name='submit' value='Submit'></input>";
echo "</form>";
Then on validasi page add this:
$nomortempat = $_GET['nomortempat']; // and:
$IDorder = $_SESSION["idorder"];
$Pilih = $_SESSION["pilih"];
Now you have all three values on validasi.php
with GET form, you could try this:
echo "<form name='nomor' action='action.php' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' value='valid'>";
echo "</form>";
and in action.php page, to catch value in textbox name='nomortempat', code is below:
if (isset($_GET['nomortempat'])){
echo $_GET['nomortempat'];
}
Of course, you could add more input fields whenever you want to inside form with different names.

How to know which submit button is when there are many submit buttons

while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<form action='./form_page.php' method='post'>";
echo "<td style='width:9%'>" . $row['studentID']."</td>";
echo "<td style='width:9%'><input type='submit' name='Prove' value='Approve' /></td>";
echo "<td style='width:9%'><input type='submit' name='$count' value='Delete' /></td>";
echo "</form>";
echo "</tr>";
}
I want get the specific student id, but when I reached to here, I dont know how can I get it. If I put $row['studentID'] for button name, I cant use POST['varname'] beacause the name would be different when I select is different.
Submit buttons are identified by their name.
For e.g. If there are 3 forms with 3 submit buttons named Accept1, Accept2 and Accept3.
When user submits a form by clicking on Accept1, the POST variable will be $_POST['Accept1']. Similarly $_POST['Accept2'], $_POST['Accept3'] respectively.
You can also print POST variable to get a clear idea i.e. print_r($_POST).
If i put $row['studentID'] for button name, i cant use POST['varname']
You can loop over all the values in $_POST and see if any of them match the pattern of a student ID.
Alternatively, you can use a button element (which will let you have display text and a value which don't match each other):
<button name="approve"
value="<?php echo htmlspecialchars($row['studentID'])>
Approve
</button>
and then just test for $_POST['approve'].
Try like this..
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><form action='./form_page.php' method='post'>";
echo "<td style='width:9%'>" . $row['studentID']."</td>";
echo "<input type='hidden' name='studentID' value='".$row['studentID']."'>";
echo "<td style='width:9%'><input type='submit' name='Prove' value='Approve'>;
echo "<td style='width:9%'><input type='submit' name='$count' value='Delete'>
</td></form></tr>";
}
By using input type hidden you will be able to get the student id when submit the from. Hope this will help

How do I autochange the name of radio buttons from while loop? php

Here is the scenario. in my database, I have like 4 questions. each questions have individual 5 radio buttons. I tried to retrieve the information from database. it shows my list of questions and radio buttons individually, but the BIG problem here is they are in the same group. example. in question 1, I picked 1st radio button, then in question 2, I picked the 2nd radio button. in question 1, the radio button I choose disappeared. so basically. the whole loop have single radio button name. how do I fix this dynamically? like auto change of radio-button-name for each question?
<form method='post' action='test.php'>
<?php
include 'db_connect.php';
$query = "SELECT * FROM test";
$result = $conn->query($query);
$num_results = $result->num_rows;
#if ($num_results > 0) {
#}
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
echo "<input type='radio' name='question_button' value='1'>";
echo "<input type='radio' name='question_button' value='2'>";
echo "<input type='radio' name='question_button' value='3'>";
echo "<input type='radio' name='question_button' value='4'>";
echo "<input type='radio' name='question_button' value='5'>";
echo "<br>";
}
$testbutton = isset($_POST['question_button']) ? $_POST['question_button'] : "";
if (isset($_POST['submit'])) {
echo $testbutton;
}
?>
<html>
<input type='submit' name='submit' value='submit'>
</form>
</html>
P.S. Edit. my original intention is to add or get the sum of the radio buttons. what syntax should i use?
to change button name dynamically
change like following
$question=0;
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
$question++;
echo "<input type='radio' name=".$question." value='1'>";
echo "<input type='radio' name=".$question." value='2'>";
echo "<input type='radio' name=".$question." value='3'>";
echo "<input type='radio' name=".$question." value='4'>";
echo "<input type='radio' name=".$question." value='5'>";
echo "<br>";
}
$question is just for example,
you can change redio button name according to your need.
I can't see what your database query is producing, but at a guess, I'd say you should be assigning a radio button id to your question in the database field. Then, when you get the results back, you can assign it with something like:
while ($row = $result->fetch_assoc()) {
extract($row);
echo "<input type='radio' name='".$row['question_name']."' value='".$row[question_id]."'>";
echo "<br>";
}
This should make every radio box unique so long as the data in the database is unique. If this doesn't help, may I see the results of the database query and I can edit my answer.
Simply give the radio button a group name differently as bellow example
// 1. Get the handler for counter.
$counter = 1;
// 2. Iterating through the results.
while ($row = $result->fetch_assoc()) {
$name = "question_" . $counter . "_button";
extract($row);
echo $row['test1'];
echo "<input type='radio' name=$name value='1'>";
echo "<input type='radio' name=$name value='2'>";
echo "<input type='radio' name=$name value='3'>";
echo "<input type='radio' name=$name value='4'>";
echo "<input type='radio' name=$name value='5'>";
// Increment the counter by 1 for each question.
$counter += 1;
}

multiple submit buttons in a loop php

I need to display a list of elements and after each and every element a delete button is added dynamically. Whenever the user presses a delete button the corresponding element should be deleted and rest of the list should be shown.
I have written the following php code to accomplish this:
for($i=0;$i<count($b);$i++)
{
$a=$b[$i];
echo "<li>$b[$i]</li> ";
$p="remove"."$j";
echo "<form action='' method='post'> <input class='z' type='submit' name='$p' value='delete'> </form>";
$j++;
}
if($_POST['$p'])
{
//code for deleting
}
The problem is whenever the user presses the delete button only the last element added is getting deleted and rest of the buttons are not working.Please tell me how to detect which button has been pressed dynamically and delete the corresponding element using php.
Thank you
You need to associate each button with its respective element. You'll wanna do this dynamically with an id or hidden input or something.
for($i=0;$i<count($b);$i++)
{
$a=$b[$i];
echo "<li>" . $b[$i] . "</li> ";
$p="remove" . $i;
echo "<form action='' method='post'>";
echo "<input type='hidden' name='item' value='" . $i . "' />";
echo "<input class='z' type='submit' name='delete' value='delete'> </form>";
$i++;
}
if($_POST['delete'])
{
$item = $_POST['item'];
//code for deleting $item
}
You're putting each delete button in its own form - you could also add in a hidden input with the ID to remove?
echo "<form action='' method='post'>\n";
echo "<input type='hidden' name='toDelete' value='" .$i ."'>\n";
echo "<input class='z' type='submit' name='$p' value='delete'>\n";
echo "</form>\n";
You'd then look for the element to delete with:
if(isset($_POST['toDelete'])) {
// $_POST['toDelete'] has the index number of the element to remove
}

Categories