How to pass 2 radio selection value with a single Submit?? - php

echo '<form method="post" action="mod_slots.php">';
$i = 1;
while(cond)
{
echo '<input type="radio" name="change1" value="'.$i.'" />';
$i++;
}
$j = 1;
while(cond)
{
echo '<input type="radio" name="change1" value="'.$j.'" />';
$j++;
}
}
echo '<input type="submit" value="Change Selected" />';
echo '</form>';
The above code passes only change1 value to the next page! But I want a single submit button, which will send both value!!??
My implementation may be wrong!

Radios with same name could only have one selection, if you want to divide them, just use different name.

Either you can pass them by giving their names as array like
echo '<input type="radio" name="change[]" value="'.$i.'" />';
and after submit you can print them like
print_r($_POST['change']);

Related

Multiple Forms, Wrong Value Submitted

I have PHP code creating multiple forms on a single page and the submission of any of these forms should trigger an API call based on which form was submitted.
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
// $listOfApples is a list of 10 apples each with a unique ID
foreach ($listOfApples as $apple) {
echo '<form method="post" action="mypage.php">';
echo '<input type="hidden" name="appleId" value="' . $apple->{"id"} . '">';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
No matter which of the form buttons I click, the value in $_POST['appleId'] is the ID of the first apple in the list. I don't have much experience with PHP or HTML forms, is my approach completely off?
I am seeing some small mistakes as
echo '<input type="hidden" name="appleId" value"' . $apple->{"id"} . '">';
You should fix it,instead of foreach yor can use for loop to display forms,
as I haven't $listofapples,I used 10,you can query for row numbers than place row numbers value as $listofapples,then echo your unique id to input value,
Finally it's working as you wished
Check it & let me know if it's the perfect fit for you
<?php
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
$listOfApples = 10;
// $listOfApples is a list of 10 apples each with a unique ID
$sn=0;
for ($i = 1; $i <= $listOfApples; $i++) {
echo '<form method="post" action="hy.php">';
echo '<input type="hidden" name="appleId" value=" ' .++$sn. '"/>';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
?>

PHP form submission with echo html post's empty array

I'm using php to dynamically create multiple buttons on my web page that have increasing values 01 to 09. When I create the forms as below the buttons are created as expected with the correct values but when I submit the form an empty array is posted.
The code here is inside a for loop that increments $i:
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
Here is the HTML that is created:
However, when I change the code to take out the $i variable, the form will be submitted as expected but all the buttons will have the same value which I can't have.
Here is the start of test.php that prints out array(0) { } when I click the submit button and an alert comes up with message 'empty'.
var_dump($_POST);
if(empty($_POST['RunSmokeTest']))
{
echo '<script language="javascript">';
echo 'alert("empty")';
echo '</script>';
}
else{
...
}
Try this .
<?php
if(isset($_POST['submit'])) {
echo $_POST['RunSmokeTest'];
}
for ($i=0; $i < 10; $i++) {
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
}
?>
Demo is here

How can I get the sender ID of dynamically created submit buttons?

I'm generating a html table. Each row starts with a Edit-Button.
Site A:
echo "<table border='1' align='center'>";
echo "<tr>"
. "<th style='font-weight:bold'></th>"
. "<th>".$nr.":</th>"
. "<th>Anschrift</th><th>Nachname</th>"
. "<th>Vorname</th>"
. " <th>PLZ</th>"
. "<th>Ort</th>"
. "</tr>";
echo "<form action='lxKundenEdit.php' method='POST'>";
$i=0;
foreach($arr as $key =>$value)
{
echo "<tr><td><input type='submit' name='btn'.$i.'\'' value='Bearbeiten'/></td>";
foreach($value as $subkey=>$subValue)
{
echo "<td>".$subValue."</td>";
}
echo "</tr>";
$i++;
}
echo "</form>";
echo "</table>";
Then I want to know which button has been pushed. When vardump the POST-Array it seems nothing really works. Any hints about this? regards, Ismir
Site B:
var_dump($_POST['btn0']); //f.e.
Simply pass the sender ID in a hidden input
<input type="hidden" name="sender" value="<?= $senderID; ?>">
On Site B you can then get the value from the $_POST variable:
echo $_POST['sender'];
Edit
I see I misread your question. What you can do is pass your subkey on the submit button as follows:
<input type="submit" name="submit[<?= $subkey; ?>]" value="send" />
You should give each button a unique value. When the form is posted you can then check for that value for that button.
echo '<input type="submit" name="btn'.$i.'" value="'.$i.'" />';
Edit:
As Peter pointed out in the comments, if you want to change the text of your button, you can use the button element:
<?php
echo '<form method="post">';
echo '<button type="submit" value="button 1 was used" name="button">Send</button>';
echo '<button type="submit" value="button 2 was used" name="button">Send</button>';
echo '</form>';
var_dump( $_POST[ 'button' ] );
All you need to do is:
Make array of all your submit buttons with ids as keys.
echo '<tr><td><input type="submit" name="btn[<?php echo $i;?>]" value="Bearbeiten"/></td>';
And then in PHP, get posted submit buttons like:
if (! empty($_POST['btn']) {
foreach ($_POST['btn'] as $btnId => $btnVal) {
// $btnId is Id of the button that is $i
// $btnId means button is pressed.
// $btnVal is value of the button
}
}
Just include a hidden input inside your form, eg
<input type='hidden' name='submit'/>
So you can use it to check, if the form has been sent (if(isset($_POST['submit'])) etc). Then check any other $_POST fields looking for any ['btn...'], eg
foreach($_POST as $key => $value){ // check all the $_POST fields
if(substr($key,0,3) == 'btn'){ // if its name begins with 'btn'...
$button_id = substr($key,3,NULL); // then that's some button. Get its ID
}
}
Think this should work

How do I get values from a table out of a foreach loop?

What I'm doing is trying to set up a page where users can see their created tools and delete them whenever they want. I'm querying three different tables and putting the results into an array. Once those values are in an array, a foreach loop goes through and populates a table with all the information in a table, like so:
$counter = 1;
echo '<table>'
foreach ($recent_saved_tools as $key => $value) {
echo '<tr name="item'.$counter.'">';
echo '<td>';
echo '<input type="hidden" name="tablename" value="'.$value['table'].'" />';
echo '<input type="hidden" name="tabledelete" value="'.$value['delete'].'" />';
echo '<input type="hidden" name="tableidfield" value="'.$value['idfield'].'" />';
echo '<input type="hidden" name="tableid" value="'.$value['id'].'" />';
//code to display the tool name and link
echo '<a style="text-decoration:none;" href="'.WEBSITE.'tools/'.$value['URL'].'?saved_data_id='.$value['id'].'">'.$value['display'].'</a><br />';
echo date("m/d/Y H:i:s", $key).'<br />';
echo '</td><td>';
//code to display the delete button
echo ' <input class="cssformbutton bluebutton" type="button" name="delete" id="deletebtn'.$counter.'" value="Delete" /><br /><br /><br /><br /></td>';
$counter ++;
}
echo '</table>';
The problem is whenever I run the SQL query, no matter what button I click it always takes the values from the last table row. I know it has something to do with the way they're named (multiple elements have the same name) but I'm at a loss on how to fix this. Any help would be appreciated.
Here's the query I'm using to delete the item:
$query = 'UPDATE '.$value['table'].' SET '.$value['delete'].' = 1 WHERE '.$value['idfield'].' = '.$value['id'];
$sql->query($query);
EDIT: added delete code
Every row has some inputs which are posted to the server. They have the same names - for every row. You could change it like this:
echo '<input type="hidden" name="tablename'.$counter.'" value="'.$value['table'].'" />';
Then you can use $_POST['tablename'.$rownr] in your delete code.
Doesn't look like you're closing the anywhere...
What results return from the query?
What output are you expecting?
try making it a for loop in stead and adding the variable to the end.
$array;
$counter = count($array);
echo '<table>'
for($i = 0; $i < $counter - 1; $i++) {
echo '<tr name="item'.$i.'">';
echo '<td>';
echo '<input type="hidden" name="tablename" value="'.$value['table']. $i'" />';
echo '<input type="hidden" name="tabledelete" value="'.$value['delete']. $i'" />';
echo '<input type="hidden" name="tableidfield" value="'.$value['idfield']. $i'" />';
echo '<input type="hidden" name="tableid" value="'.$value['id'].'" />';
//code to display the tool name and link
echo '<a style="text-decoration:none;" href="'.WEBSITE.'tools/'.$value['URL'].'?saved_data_id='.$value['id'].'">'.$value['display'].'</a><br />';
echo date("m/d/Y H:i:s", $key).'<br />';
echo '</td><td>';
//code to display the delete button
echo ' <input class="cssformbutton bluebutton" type="button" name="delete" id="deletebtn'.$counter.'" value="Delete" /><br /><br /><br /><br /></td>';
}
echo '</table>';

Having trouble getting submitted values and names in dynamic radio button loop

I have selected data from mysql and inserted them into a radio button form which is looped as shown:
if($num_rows) {
echo '<form name="fixtureform" method="POST" action="index.php">';
while ($row = mysql_fetch_assoc($result)) {
echo $row["home_team"] . " vs " . $row["away_team"]. '<br />' . " Home" . '<input type="radio" name="win'.$row["home_id"].'" value="'.$row["home_id"].'"/>' ." Draw" . '<input type="radio" name="'.$row["home_id"].$row["away_id"].'" value="draw"/>' ." Away" . '<input type="radio" name="win'.$row["away_id"].'" value="' . $row["away_id"] . '"/>' .'<br />';
}
echo '<input type="Submit" Name="Submitacc" Value="Submit your teams">';
echo '</form>';
} else {
echo "There are no fixtures today ";
}
I then have an if statement for submitted values:
if($_POST['Submitacc']=='Submit your teams') {
}
How would I say if the selected name of the radio button is win.$row[home_id] - insert value into mysql table. I'm struggling with the fact I can't get $row[home_id] outside of the loop?
thanks in advance
Try as below
$i=0;
while ($row = mysql_fetch_assoc($result)) {
echo $row["home_team"] . " vs " . $row["away_team"]. '<br />' . " Home" . '<input type="radio" name="result['.$i.']" value="h_'.$row["home_id"].'"/>' ." Draw" . '<input type="radio" name="result['.$i.']" value="draw"/>' ." Away" . '<input type="radio" name="result['.$i.']" value="a_' . $row["away_id"] . '"/>' .'<br />';
$i++;
}
In Your PHP
foreach($_POST['result'] as $value){
if($value == 'draw') {
//do stuff
}
else {
$apart = explode('_',$value);
if($apart[0]=='h'){
//home team win
echo $apart[1];
}
else {
//away team win
echo $apart[1];
}
}
}
use this
echo"<input name='win' type='radio' value='".$row['home_id']."' />";
Because your radios have different names they are all separate and all being submitted, I recommend naming them all "result" and giving them values of "win", "draw" and "loose" respectively.
<label>Win: <input type="radio" name="result" value="win" /></label>
<label>Draw: <input type="radio" name="result" value="draw" /></label>
<label>Loose: <input type="radio" name="result" value="loose" /></label>
<input type="hidden" name="home_team" value="<?php echo $row['home_team']; ?>" />
<input type="hidden" name="away_team" value="<?php echo $row['away_team']; ?>" />
You can access the value with $_POST['result'] and since you know the home team and away team you can handle the data accordingly
i think a diferent usage of the radio button mechanic shuold be used in this case.
in your example you are creating 3 diferent instances of a radio button for each row, instead create one instance of a radio button set with diferent values depending on the answer given
like:
<input type="radio" name="game'.$row["game_id"].'" value="'.$row["home_id"].'">
<input type="radio" name="game'.$row["game_id"].'" value="draw">
<input type="radio" name="game'.$row["game_id"].'" value="'.$row["away_id"].'">
this means that the variable $_POST["game1"] will have the value of the winner or draw that was selected for game which is game_id 1
hope this helps you
Change your element name to win.home without the id, since you will get it by the value.
Then go like:
if(isset($_POST['win.home'])) { }
Edit:
If you want to do it like this, try this:
3 radio buttons, named win.home, draw, win.away plus a unique id (mysql id?) like win.home.1234 then you can use as many games as you want. You can get the id back with explode() later.
Then the values:
1st: $row["home_id"] . "-" . $row["away_id"]
2nd: draw
3rd: $row["away_id"] . "-" . $row["home_id"]
Then when processing the form:
Split the value with explode() at the -. So the first id is always the winner.

Categories