Dynamic naming of radio buttons - php

I have "n" number of rows being returned by a sql statement. Works great. For every row returned, I would like the user to pick an option (either option 'a' or option 'b')
As standard practice a while loop is used to iterate through each row, and I attempt to name my options by the row number. However, I run into problems when I attempt to get the value of the radio button the user has selected.
I can't seem to dynamically get the value of the radio button named 1 in the first loop (or any loop with it's appropriate name) of the where clause.
Code snippet here:
$counter = 1;
while($row = mysql_fetch_array($result))
{
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $counter . ">";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['d_id'] ."> ";
echo "<br>";
echo "<input type='hidden' name='choice' value=" . $_GET["m" . $counter] . " >";
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
$counter ++;
}
updatepics.php:
Game
<?php
echo $_POST['game'];
echo " " . $_POST['player'] . " picks: ";
echo " " . $_POST['choice'] ;
?>
output of updatepics.php...
Game 1 granny picks:
Notice 'choice' is null

According to the HTML4 specification, names attributes are defined as CDATA and CDATA is defined as follows:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Modify your code to have all name values start with at least one letter.

I bailed out from dynamically creating a radio button name. Now all radio buttons have the same name through each iteration of the loop.
It seems to work passing the correct value at the correct time even if you select all of the buttons are set before hitting the submit button for each iteration.
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='mw' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $row['f_id'] . ">";
echo "<input type='hidden' name='player' value='" . $user . "' >";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='mw' value= ". $row['d_id'] ."> ";
echo " " . $row['line'];
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
updatepics.php is just as simple, and it works:
$_POST['mw']

Related

PHP Update always update empty string

let me explain the code I'm getting data from the db and display the data in text input then when the user make changes I get the user input and update the table however when it update it update to empty data
<?php
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
echo "</tr>";
echo "</table>";
echo "<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : '';
$products_desc = isset($_GET['products_desc']) ? $_GET['products_desc'] : '';
$products_price = isset($_GET['products_price']) ? $_GET['products_price'] : '';
if (isset($_POST["update"])) {
$sql1 = "UPDATE tbl_products SET products_desc='" . $products_desc . "',product_name='" . $product_name . "',products_price='" . $products_price . "' WHERE products_id='" . $product_id . "'";
mysqli_query($conn, $sql1) or die(mysqli_error($conn));
echo "yess";
}
?>
Your input elements have no name attribute, so things like $_GET['product_name'] will always be empty. The name attribute is the key in the key/value pair sent to the server.
Additionally, the type attributes are all broken. (Though I suspect the browser is automatically "correcting" that by defaulting to a text input.)
Add the name attribute (and fix type):
<input type='text' name='product_name' id='product_name' ...
Additionally, you have two forms. So when you click your button, that form doesn't submit any of the inputs. Because they're in a different form.
Put them all into the same form, and decide whether you want to use GET or POST (since your server-side code is going to need to know that).
You defined your data form in one form tag and submit form in the another form tag:
<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
When you submit your first form but your data is on another form
this is what is wrong

Trying to create an editable HTML table using PHP and mySQL but the table won't update

I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.

when I am trying to get values from checkbox in PHP- i get "on" as an output - how is that?

when I am trying to get values from checkbox in PHP , I get
this as output, printing $_POST
Array ( [days] => on [submit] => save )
the view code
$days_numbers = explode(',',$user->work_days);
$week = array('Saturday','Sunday' ,'Monday','Tuesday' ,'Wendnesday' ,'Thursday' ,'Friday');
?>
<form method='post' action='' >
<?php
for($i=0 ; $i< count($week); $i++)
{
if(in_array($i,$days_numbers))
{ echo "<input type='checkbox' name='days' checked >" . $week[$i]. "<br/>";
}else
echo "<input type='checkbox' name='days' >" . $week[$i] . "<br/>";
}
?>
Your input element is missing with value attribute
replace
echo "<input type='checkbox' name='days' >" . $week[$i] . "<br/>";
with
echo "<input type='checkbox' name='days' value=".$week[$i]." >" . $week[$i] . "<br/>";
I found that i forget to get write html view like this:
echo "<input type='checkbox' name='days[]' value='$i' checked >" . $week[$i] . "<br/>";
I forget to give value of input so the output was on and also get the value as an array by adding name name="days[] .

How to make radio button with mysql value?

I want to make radiobutton with mysql
MY CODE
But it isn't make sense (SERVER ERROR:500)
Beacause of html form tag!
So I want to ask you!
"How can I make radiobutton with mysql value?"
I don't know what you are trying to achieve but I think I know what you mean. You want to create a radio button giving it a value from your database.
You don't create a form for each radio button. Instead, create the radio buttons within a single form tag.
Something like this:
echo "<form name='' action='index.php' method='post'>";
while($row=mysqli_fetch_array($res)){
echo "<input type='radio' name='" .$q . "' value='".$q ."'>" .$q . "<br />";
echo "<input type='radio' name='" .$a1 . "' value='".$a1 ."'>" .$a1 . "<br />";
echo "<input type='radio' name='" .$a2 . "' value='".$a2 ."'>" .$a2 . "<br />";
echo "<input type='radio' name='" .$a3 . "' value='".$a3 ."'>" .$a3 . "<br />";
}
echo "</form>";
In your echo statement change the parameter " to '.
Try this it will be defiantly works.
Try like this,
while ($row = mysqli_fetch_array($res))
{
$q = $row['q'];
echo "<tr>
<td>
<form name='' action='index.php' method='post'>
<input type='radio' name='q' value='".$q."'
</form>
</td>
</tr>";
}
Are you properly able to establish a connection to the server? Just var_dump or print_r to see what you are getting from the database.

Jquery Multiple Attribute Selector not working

I am trying to select for 2 attributes that are variable on a multiple choice quiz. I echoed the correct answer to each question in an input that doesn't display on the page. I think my line where I select for 2 different attributes is the problem.
JQUERY:
var zz = 1;
while (zz <= <?php echo $num_rows ?>){ //I'm 100% postive $num_rows returns a value of 3
var zstring = '#answer' + zz;
var theanswer = $(zstring).attr('value'); //should return "a" or "c" or whatever the answer is
if $("input[name=zz][value=theanswer]").is(':checked') { //this is the line that's not working
alert("the code worked");
}
zz++;
}
HTML echoed from PHP
echo "<input type='radio' name=" . $question_number ." value='a'> A. " . $chA . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='b'> B. " . $chB . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='c'> C. " . $chC . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='d'> D. " . $chD . "<br><br>";
echo "<input type='radio' name=" . $question_number ." value='e'> E. " . $chE . "<br>";
echo "<input type='text' id='answer".$question_number."' style='display: none;' value='".$correct."' />";
I see that in the if statement you have this:
if $("input[name=zz][value=theanswer]").is(':checked') { //this is the line
it should be:
if ($('input[name="zz"][value="theanswer"]').is(':checked')) { //this is the line

Categories