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>
Related
Still getting used to stackoverflow excuse my rookie-ness.. :)
Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.
My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.
My code is as follows:
$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>
<div class="wrapper">
<h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">
<?php
echo
"<table border='1'>
<tr>
<th> Name </th>
<th> Picture </th>
<th> Console </th>
<th> Description </th>
<th> Price </th>
<th> Amount </th>
</tr>";
echo '<form id="gamesOrder" action="purchase.php">';
while($row = mysqli_fetch_array($result)) {
$id = $row['Pd_Key'];
echo"<tr>";
echo"<td>" . $row['Name'] . "</td>";
echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
echo"<td>" . $row['Type2'] . "</td>";
echo"<td>" . $row['Description'] . "</td>";
echo"<td>" . $row['Price'] . "</td>";
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
echo"</tr>";
}
echo '<input type="submit" value=" BUY ">';
echo '</form>';
?>
When I click the submit it changes the url but nothing happens, it doesn't redirect.
Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.
EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.
Now redirects but not to purchase.php
URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0
Thanks you legends you!! =D
You are missing the closing form caret:
echo '<form id="gamesOrder" action"purchase.php"';
should be:
echo '<form id="gamesOrder" action="purchase.php">';
Also you concatenation for ID is incorrect:
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should just be:
echo"<td>" . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
To access the id in purchase.php use the following code:
$id = isset($_GET['id']) ? $_GET['id'] : null;
And you need to assign the action to the form with an equals sign:
action="myaction.php"
And you don't pass the id right...
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should be
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
Ough, and on form you need to put action =
echo '<form id="gamesOrder" action="purchase.php">';
Ok, first you've got some HTML error, lets see:
In the Form element you need to close it and also include the method (as POST), see:
< form id="gamesOrder" action="purchase.php" method="POST">
To send data using a form you will need to include the data from the data base in form fields like this:
echo '< input type="text" name="myFieldName" value="'.$row['Price'].'">';
Any question let me know...
Cheers.
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.
I have a php page which is reading a text file and printing the file line by line. and also I have given 'yes' 'no' option with every line.Now I want to print those lines in the 2nd page which is selected as yes.If user select yes then I want to print those line to next page.I have the below code.Can any one check this code?
<?php
$i=0;
$lines = file("text.txt");
foreach($lines as $line) {
$select_val=$line.'-'.$i;
print " <br> ";
print " <br> ";
echo($line);
echo '<select name=".$select_val.">';
echo '<option value="Yes">Yes</option>';
echo '<option value="No">No</option>';
echo '</select>';
print " <br> ";
print " <br> ";
$i++;
}
?>
<input type="hidden" name="ival" value="$i" /> <?
print "<input type=submit value=Submit><input type=reset>";
?>
2nd page:
<?php
$i=0;
$lines = file("text.txt");
foreach($lines as $line) {
$select_val=$line.'-'.$i;
$a=$_POST[$select_val];
if ($a == 'Yes') {
echo($line);
}
$i++;
}
?>
The code would almost achieve what you're trying to do, but a 2-value selection may be better achieved with a radio selection instead of a drop-down. This makes better usability since a radio click is achieved with one action vs. a drop-down select which requires two actions.
echo '<input type="radio" name="' . $select_val . '" id="'
. $select_val . 'yes" value="Yes" /><label for="'
. $select_val . '1"> Yes</label> ';
echo '<input type="radio" name="' . $select_val . '" id="'
. $select_val . 'no" value="No" /><label for="'
. $select_val .'no"> No</label> ';
Your original code has some formatting error. If you're going to use single quotes for your echo statements, you have to concatenate the variable with the single quotations to return to PHP parsing like this (recommended method):
echo '<select name="' . $select_val . '">';
or use double quotations like this (while escaping the double quotations inside of the select tag):
echo "<select name=\"$select_val\">";
Otherwise the select tag will show up as (using your original code)
<select name=".$select_val.">
instead of
<select name="line-0">
Examine the HTML output of your PHP script and you'll see this.
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.
I've looked through some posts around here, but am still struggling to get my code to work correctly....
I've got a form that is being dynamically created so I never know how many rows it will have, the number of columns is static though. Here is my current code...
echo '<form name = "confirmPlayers" enctype="multipart/form-data" action="page.php" method="POST">';
echo '<input type="hidden" name="confirm" value="1"/>';
echo '<input type="hidden" name="pg_function" value="match"/>';
echo '<table border = "1">';
echo '<tr> <td colspan = "7"> <b>Matched Members</b></td> </tr>';
echo '<tr> <td> Match </td> <td>Division</td> <td>Number</td> <td>Upload Number</td> <td>KDGA Name</td> <td>Upload Name</td> <td>Score</td></tr>';
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[]" value="1" checked>';
echo '<td> ' . $row['division'] . ' </td>';
echo '<td> ' . $row['mplayer_number'] . ' </td>';
echo '<td> ' . $row['tplayer_number'] . ' </td>';
echo '<td> ' . $row['mfirst_name'] . ' ' . $row['mlast_name'] . ' </td>';
echo '<td> ' . $row['tfirst_name'] . ' ' . $row['tlast_name'] . ' </td>';
echo '<td> ' . $row['score'] . ' </td>';
echo "</tr>";
}
echo '<tr> <td>';
echo '<input type="submit" value="Submit" />';
echo '</td> </tr>';
echo '</table> </form>';
I've tried several variations on creating the array but just seem to be creating a mess. I basically need all of the information from each cell returned in order to pass on to the database for further processing. I was hoping to return an array with a row for each corresponding row in the generated table, and then keys/indexes for each column displayed. Right now if I use a print_r($_POST['match_chkBox']); then it is returning correctly with the number of rows I left checked before submitting, but any other changes.. not so pretty.
Would appreciate any help you can give... this is driving me nuts!
try giving value unique id
echo '<td> <input type="checkbox" name="match_chkBox[]" value=value=\"".$row['id']."\"checked>';
if i were you i will post $row[id] (as defined by angel) only and fetch respective data at my php code from the database.
but if you want to take all data with you you can simply do the following..
<pre>
$i=0;
while($row = mysql_fetch_array($result)){
$i++;
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[$i]" value="'.$row['id'].'" checked>';
echo '<td> ' . $row['division'] . ' <input type="hidden" name="division['.$i.']"> </td>';
echo '<td> ' . $row['mplayer_number'] . ' <input type="hidden" name="mplayer_number['.$i.']"> </td>';
// and so on....
echo "</tr>";
}
</pre>
in since html form sent only checked checkbox to the server you can have the following code to check user selections
<pre>
foreach($_POST['match_chkBox'] as $k=>$value) {
$divi = division[$k];
$mplay = mplayer_number[$k];
//now insert into your database.
}
</pre>
hope this will help
Set the value of the checkbox to the unique key of that row, there by when the form is posted, match_chkBox[] will contain the unique keys.
echo"<input type='checkbox' name='match_chkBox[]' value=\"$row[unique_id]\" />";
Use view source to see if each rows value is set to the unique_id of that row