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

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.

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 - Correct way to compare radio button check against a database

I have this database setup:
Alongside this code,
if($result = $db->query("SELECT * FROM questions")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
echo $row->question, '<br><br>';
echo '<input type="radio" name="q'.$row->id.'" value="a" id="q1a"><label for="q1a">' . $row->ans1 . '</label><br/>';
echo '<input type="radio" name="q'.$row->id.'" value="a" id="q1a"><label for="q1a">' . $row->ans2 . '</label><br/>';
echo '<input type="radio" name="q'.$row->id.'" value="a" id="q1a"><label for="q1a">' . $row->ans3 . '</label><br/>';
echo '<input type="radio" name="q'.$row->id.'" value="a" id="q1a"><label for="q1a">' . $row->ans4 . '</label><br/>';
echo '<input type="radio" name="q'.$row->id.'" value="a" id="q1a"><label for="q1a">' . $row->correct_ans . '</label><br/><br/>';
}
$result->free();
}
}
Which gives this output:
I was wondering, how could I check if the users answers against the correct answer stored in the database?
I figured it would be best to do an if statement and check if the correct answer was not selected. I'm just unsure how I would execute this, if someone could point me in the right direction, that would be of great help.
just give the right answer a different value and then write a while loop that check the question's.
something like that (let's say every question has the name q and a number after it):
$count = 1;
$right_answers = 0;
while ($count <= 5) {
if ($_POST['q'.$count] == "right") {
$right_answers++;
}
$count++;
}
$right_answers is the counting of the right answers.
take same name for all radio buttons but values should be different. on click of radio button you will get a value to be matched for checking.

Reading and Overwriting radio button values in mysql database

I'm describing the correcting functioning of the process.
First the user posts an order, and the status initially is set to NOT APPROVED, but his manager is able to see the order request and can change the status to APPROVED, and then it is forwarded to the IT guy and he can change status to UNDER PROCUREMENT.
I am using radio buttons for gm and IT guy for changing the status. However, i am unable to change the status. Here's the code for better understanding
echo'<th>Product</th><th>Type</th><th>Quantity</th><th>Order Status</th>';
while ($row = mysqli_fetch_array($result)){
$type=$row["type"];
$make=$row["make"];
$quantity=$row["quantity"];
echo "<tr>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo '<form method="post">';
//Radio buttons used here
echo '<td><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</td>';
echo '</form>';
$_SESSION['radio'] = $_POST['status[]'];
$qr = "UPDATE order_info SET status = '".$_SESSION['radio']."' WHERE username = '".$_SESSION['username']."'";
$rs = mysqli_query($con3,$qr) or die(mysqli_error($con3));
}
echo "</table>";
Also, I want the radio button to be checked by default according to the status of the order.
The problem is with your input names:
echo '<td><cb><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</cb></td>';
echo '</form>';
You're creating an array. You can see this with this example:
<form method ='post'>
<input type='radio' name='status["25"]' value='not approved'>Not Approved
<input type='radio' name='status[25]' value='approved'>Approved
<input type='submit' value='Submit'>
</form>
<pre>
<?
session_start();
print_r($_POST);
$_SESSION['radio'] = $_POST['status'];
print_r($_SESSION);
?>
</pre>
This may have been to keep the order_id, but your update query is not specifying an order_id and will update all rows for that username. If you're only updating one row at a time, you can pass the order id with a hidden field (if you even need it?):
echo '<input type="hidden" name="order_id" value="'.$row['order_id'].'">';
Which means you're probably missing an "and order_id =" qualifier in your update.

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

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']);

dynamically set value for a radio button with php

Hello Coders,
I have a dynamically created table in PHP and for each row there is a radio button. The intention is that depending upon which radio box is selected, the user can perform multiple functions like, Delete, Amend etc. I can make this all work with hard coded "Value" for the radio button like
echo "<td>" . '<input type="radio" name="radioSelect" value="2" checked="checked" />' . "</td>";
What I am looking for is a way to set the "2" dynamically. For example:
while($row = mysql_fetch_array($result) and $i<=100)
{
echo "<tr>";
$sno= $row['SNo'];
echo "<td>" . '<input type="radio" name="radioSelect" value= $sno checked="checked" />' . "</td>";
}
What is the syntax for this? Is this possible at all?
Thanks for your help.
Yes, it's called concatenation, and you're already doing it with the . operator after "<td>", for example.
echo "<td>" . '<input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" />' . "</td>";
I'm not sure why you put it that way, though.
echo '<td><input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" /></td>';
Also, note that only one radio button with the same name can be checked at a time, so you might want to rethink what you're doing there.
You can do like this ? if you are having a problem in CONCATENATION.
<?php
while($row = mysql_fetch_assoc($result) and $i<=100)
{ $sno= $row['SNo'];?>
<table>
<tr>
<td><input type="radio" name="radioSelect" value= <?php echo $sno; ?> checked="checked" /> <?php } ?></td>
</tr>
</table>

Categories