I am generating a dynamic order form in php based on varying search criteria from data in a MySQL database.
The order form is in effect a line-item list and could have several hundred line-items.
Each line-item will have the following data (simulated names) :
Row# ItemID ItemName Units/Pack Price/Unit.
In addition to the above data extracted from the database, I am adding 2 dynamic fields :
1. a check box to identify items to order
2. a text box to enter the qty ordered
<?php
$row=0;
while($item = mysqli_fetch_array($result))
{
$row++;
$id = "$item[ProductID]";
$name = "$item[Name]";
$units = "$item[PerPack]";
$price = "$item[Price]";
echo "<tr>
<td align='center'>$row</td>
<td align='center'>$id</td>
<td>$name</td>
<td align='center'>$units</td>
<td align='center'>$price</td>
<td align='center'><input type=checkbox id=$row name=ReviewID[] value='$id' /></td>
<td align='center'><input type=text size=3 maxlength=3 id=$row name=ReviewQty[] /></td>
</tr>";
}
?>
The order form is then processed by an order review script.
After much searching and experimenting I have come up with the following script.
<?php
$keys_array = $_POST['ReviewID'];
$keys_count = count($keys_array);
$values_array = array_filter($_POST['ReviewQty']);
$values_count = count($values_array);
$order_array = array_combine($keys_array, $values_array);
if($keys_count != $values_count){
$status = 'Error';
$error_message = "ORDER Quatities Mismatch";}
// html code omitted
if($status == 'Error') echo $error_message;
//html code omitted
if($keys_count != $values_count) exit();
//html code omitted
while (list ($key, $val) = each ($order_array))
{
echo "<tr>
<td align='center'>...</td>
<td align='center'>$key</td>
<td>...</td>
<td>...</td>
<td align='center'>...</td>
<td align='center'>...</td>
<td align='center'>...</td>
<td align='center'>$val</td>
</tr>";
}
?>
The problem with my solution is that is does not check that each selected checkbox has a corresponding qty value in the same row.
In other words if I check any 5 boxes and enter values in any 5 text boxes, it will pass the test.
I need a check which verifies the checkbox is in the same row (line-item) as the qty value.
It would also be great if I could parse all the line-item data for a selected checkbox, otherwise I would need to do another MySQL query in the review script.
I am having trouble getting an array with 2 values per key to work.
Any help/improvements would be appreciated.
TIA.
FWIW by experimentation I found that by using the row number as the key, I can match keys and do a cross-check the arrays that both have the same keys.
<td align='center'><input type=checkbox name=ReviewID[$row] value='$id' /></td>
<td align='center'><input type=text size=3 maxlength=3 name=ReviewQty[$row] /></td>
I can also parse the other data via hidden fields in separate arrays once again with the row number as keys.
<td align='center'>$price</td><input type=hidden name=ReviewPrice[$row] value='$price' />
All I have to do now is match key values, then I can pull up only the data I need.
I am still interested in any better solutions, o/wise I hope someone else finds this useful - I know I really struggled to get to this point.
Related
I have a table created based on the rows in a mysql database table. So the rows are not fixed but depend on records from the source database table.
HTML and part of DB query:
$query=mysqli_query($db_server, "SELECT DateTime, RefNumber, COUNT(*) AS count FROM requests GROUP BY DateTime");
$i=1;
foreach($query as $row)
{?>
<tr>
<td> <?php echo $i ?> </td>
<td><input type='text' name='date_sent[]' value='<?php echo $row['DateTime'];?>' style='border-radius:0; margin:0' /></td>
<td><input id='ref<?php echo $i?>' type='text' name='ref[]' value='<?php echo $row['RefNumber'];?>' style='border-radius:0; margin:0' /></td>
<td ><input id='noofitems<?php echo $i?>' type='text' class='prices' name='noofitems[]' value='<?php echo $row['count'];?>' style='text-align:center; border-radius:0; margin:0' /></td>
<td align='center'><input id='action1<?php echo $i?>' type='checkbox' class='prices' name='action2[]' style='width:20px; border-radius:0; margin:0' /></td>
</tr>
<?php
$i++;
}
Table:
Dynamic table - from DB
What I want:
A pure PHP code that will pick REFENCE NUMBERS of the exact corresponding checked rows. I need these reference numbers to build another DB query to create a second table on another page using PHP sessions.
I have tried calling the following to store the ref numbers. Unfortunately, it only picks the first consecutive reference numbers. I need to pick only those whose rows are checked.
<?php
session_start();
$rowCount = count($_POST['action2']);
for($i=0; $i<$rowCount; $i++){
if(isset($_POST['action2'][$i])){
$_ref='ref'.$i;
$_SESSION["$_ref"]=$_POST['ref'][$i];
}
}
$_SESSION['rowCount']=$rowCount;
Or even this:
<?php
session_start();
if(isset($_POST['action2'])){
$rowCount=count($_POST['action2']);
$i=0;
foreach($_POST['action2'] as $i){
$ref='ref'+$i;
$_SESSION["$ref"]=$_POST['ref'][$i];
$i++;
}
}
You have generated checkboxes that look like this in HTML
<input id="action14" type="checkbox" class="prices" name="action2[]" style="width:20px; border-radius:0; margin:0">
How can you tell which ID this checkbox refers to? You need to assign a value to each checkbox so that when it's sent to PHP you can tell which row was marked.
Use value='<?=$row['SomeID']?>' to populate the value attribute that will be sent to PHP. Then you can do foreach ($_POST['action2'] as $ID)
I'm relatively new to php and mysql. What I'm trying to do in this section of code is update 2 columns of information in a database based on the inputs of 2 text boxes. Whenever I try and update the values in the database they update to 0. I placed echo statements after I declared the $ variables and the values of both variables was the same as what I had typed into the boxes. But when I run the sql_query the new values in the database are 0 rather than the values of the 2 $ variables. Any help would be much appreciated! Thanks in advance!
<?php
$result = mysql_query("SELECT * from place_order ORDER BY item_name;");
echo "<form action='' method='POST'>Select an item:<select name='selection'><option>Select...</option>";
while ($row = mysql_fetch_assoc($result))
{
$item_name = $row["item_name"];
echo "<option>$item_name</option>";
}
echo "</select>
<input type='submit' value='Select Item' style='float:right;'/>";
$selection = $_POST['selection'];
echo "<br><br>Type the updated information into the text fields<br><br>";
echo "
<table width='300'>
<tr>
<td align='left'>Item Cost(€): </td>
<td align='left'><input type='text' name='cost'></td>
</tr>
<tr>
<td align='left'>Item Quantity: </td>
<td align='left'><input type='text' name='quantity'></td>
</tr>
<tr>
<td align='left'></td>
<td align='left'><input type='submit' name='button' value='Submit'></td>
</tr>
</table>
";
$cost = $_POST['cost'];
$quantity = $_POST['quantity'];
$selection = $_POST['selection'];
$sql = "UPDATE place_order SET item_cost='$cost', quantity='$quantity' WHERE item_name='$selection' ORDER BY item_name;;";
$query_update = mysql_query($sql);
if($query_update)
{
echo "Table updated! Click View Stock in the menu to view the updated table";
}
echo"</form>";
?>
There are two forms in this script:
one with the select box
another with the input boxes
each of these forms post individually.
Let's have some kind of state transition table:
forms are showing first time initially, all values are either empty
or default.
you select an item name and submit the first form.
option list is posted and $_POST['select'] has a value which then
is properly populated in the second form
the script runs til the end and immediately inserts an empty row to
your database with the selected item name
you make your inputs in the second form and submit it
this has two input variables: cost and quantity which get posted
due to the fact that the option list is in the other form, it will
not be posted and $select is empty at that state
the update statement will run but with no result since there is no
empty item_name in the db - useless update.
simple solution: make just one form out of that and you're done (except error handling which is missing entirely ...)
second solution: if only $_POST['select'] is set, put the value of $select in a hidden field and exit the script after echo-ing the second form.
I am trying to create a table showing multiple users data. The data in the table will then be able to be edited and updated. Below is an example of the way the form is laid out:
echo "<form action=AdminUpdateLecInfo.php method=post>";
while ($return = mysql_fetch_assoc($result)) {
$phonenumber = "$return[PhoneNumber]";
$number = str_pad($phonenumber, 11, "0", STR_PAD_LEFT);
echo " <tr class='data'>
<input type='hidden' name='id'".$return['ID']."'' value= '".$return['ID']."' />
<td class = 'title'><input class = 'title' type='text' name='title'".$return['ID']."'' value= '".$return['Title']."' /></td>
}
echo "</table>
<input class='submit' type='submit' value='Update Info' />
</form>
Once the table is created the information is passed to the 'update.php' script.
$sql="UPDATE completeinfo SET Title='".$_POST['title'][$return['ID']]."'
WHERE ID = '".$_POST['id'][$return['ID']]."'";
header("Location:Home.html");
The problem I'm having is that I need to add the '".$return['ID']."' to the name of each input field so that not all users details are updated with the same values. I am unsure if I need to apply a foreach loop around this query so that it applies to each user and updates their details. Currently however the update query is not working presumably because the post method is not fetching the values from the form correctly.
Your problem is the name of your fields, use that :
$return_id = $return['ID'];
echo "
<tr class='data'>
<td class = 'title'>
<input type='hidden' name='id$return_id' value='$return_id' />
<input class='title' type='text' name='title$return_id' value='$return_id' />
</td>
</tr>";
Before doing your mysql update, do var_dump($_POST);, you'll be shown the content of the HTTP POST parameters so you can see what they are and how to use them in the query.
You have to use the names of your text fields as array so you can iterate your array
name='title'".$return['ID']."'
name='id'".$return['ID']."'
I'm having problems with extracting the data from a radio button form.
It looks like this:
<?php
## color
$stripe = 1;
$att_color=mysql_query("SELECT * FROM product_attributes WHERE products_id ='".$row['products_id']."' AND att_head='colors' AND att_show='1' ORDER BY att_order asc");
if(mysql_num_rows($att_color)!=0)
{
echo "
<table>
<tr>
<td width=\"90%\" align=\"left\" bgcolor=\"#EDEDED\">Color</td>
<td width=\"10%\" align=\"center\" bgcolor=\"#891531\">
<img src=\"images/info.png\">
</td>
</tr>";
while($color_row=mysql_fetch_array($att_color)){
if ($color_row['att_standard'] == 1) { $standard = "checked"; }
echo "
<tr class=".(($stripe++%2==1) ? 'odd' : 'even').">
<td colspan=\"2\" width=\"100%\">
<input name=\"color_name[]\" value=\"".$color_row['att_name']."\" id=\"".$color_row['att_order']."\" type=\"hidden\" />
<input type=\"radio\" name=\"color_selected[]\" id=\"".$color_row['att_order']."\" value=\"".(round($color_row['att_val']*0.85))."\" ".$standard.">
<img src=\"images/colors/".$colorimg."\"> ".$color_row['att_name']."</td>
</tr>";
unset($standard); }
echo "</table>"; }
# END color
?>
Only ONE option is to be allowed.
When someone hits submit I* need to get the value AND the corresponding name from that row.
However I can't seem to figure the foreach syntax out.
if( isset($_POST['submit']) ) {
$c_selected = $_POST['color_selected'];
$c_name = $_POST['color_name'];
foreach($c_selected as $key => $n) {
echo "<br><br>". $c_selected[$key] ." ".$c_name[$key];
}
The current code gives me the correct value, but the wrong colorname. (always 1st option)
How can I get this to work?
This is broke:
<input type=\"radio\" name=\"color_selected[]\" id=\"".$color_row['att_order']."\" value=\"".(round($color_row['att_val']*0.85))."\" ".$standard.">
name=\"color_selected[]\" should not be an array.
Instead just
name=\"color_selected\" will provide the chosen color.
So the value of the radio button should be the color, not the cost of the item.
You should make another hidden field with the value of the item, unless each color is a different price.
I think the problem is more complicated because you are trying to send an order array rather than a simple form or something. If this is the case, maybe you have to rethink the way you are doing this.
You seem to have a products id field, it should be unique for each color and can be used on the other side to get all the data values from your database.
<input name=\"att_order\"
value=\"".$color_row['att_order']."\"
type=\"hidden\" />
<input type=\"radio\"
name=\"color_selected\"
value=\"".$color_row['products_id']."\"
".$standard.">
I have a simple input form with input fields that are 3 characters wide. To gain focus to these fields however, I must click the very, very LEFT of the field, about 1 pixel or cursor width wide. The form works fine in Firefox and Safari, where you can click anywhere in the text input field to gain focus.
The form:
<form name=game_edit method=post action="?includepage=game_results.php&gameID=<?=$gameID?>" onsubmit="return validate_form();">
<table border="0" id=gameResults>
<tr><th colspan="4">RESULTS FOR: <?=$gamedate?> HOSTED BY <?=$hostName?></th></tr>
<tr>
<td colspan="2">PLAYERS: <input type=text name=playercount value="<?=$playercount?>" size="3"></td>
<td colspan="2">Championship money colleted: $<?=$champamount?></td>
</tr>
<tr><th>PLAYER</th><th>Place</th><th>Champ Discount</th><th>Didn't play</th></tr>
<? Player_list($gameID); ?>
<tr><td colspan="2"><input type="text" name="manualAdd" size="17" /></td><td colspan="2">Add a username who didn't RSVP</td></tr>
<input type=hidden name=gameID value="<?=$gameID?>">
<tr>
<td colspan="2"><input type=submit name='saveGameResults' value='SAVE CHANGES' style='width:100px;font-size:11px;'></td>
<td colspan="2" align="right"><input type=submit name='saveGameResults' value='LOCK GAME' style='width:100px;font-size:11px;'></td>
</tr>
</table>
The function that writes the fields:
function player_list($gameID)
//////////////////////////////////////////////
// Creates player list for specified //
// game. Provides input for game results. //
//////////////////////////////////////////////
{
//*****Get list of players for gameID*****
$sql = "SELECT rsvpid,rsvp.playerid,concat(fname,' ',lname) as playerName,place,points,freeChamp FROM rsvp LEFT JOIN players ON rsvp.playerid=players.id WHERE rsvp.gameID=$gameID ORDER BY place,lname";
$playerlist = mysql_query($sql);
if ($listrow = mysql_fetch_array($playerlist))
{
#--Get number of players who signed up on the website. This may differ from the actual player count --#
#--entered by the host. A player may have played but not be signed up on the website. --#
echo "<input type=hidden name='recordedPlayerCount' value='".mysql_num_rows($playerlist)."'>";
$i = 0; // index for form rows
do
{
foreach($listrow as $key=>$value) $$key = $value;
($freeChamp) ? $freeChampChecked = "checked" : $freeChampChecked = ""; //check the freechamp box if appropriate.
if ($place==100) $place="";
?>
<tr>
<td><?=$playerName?>:</td>
<td style="text-align:center"><input type=text size="2" name="place<?=$i?>" value="<?=$place?>"></td>
<td style="text-align:center"><input type=checkbox name="freeChamp<?=$i?>" value='true' <?=$freeChampChecked?>></td>
<td style="text-align:center"><input type=checkbox name="noShow<?=$i?>" value='true'></td>
<input type=hidden name="rsvpid<?=$i?>" value="<?=$rsvpid?>">
</tr>
<?
$i++; //increment index for next row
}while ($listrow = mysql_fetch_array($playerlist));
}else
echo "<tr><td colspan='4'>NO RECORDS FOUND FOR THIS GAME!</td></tr>";
}
?>
The offending form field is the "place" field...it is clickable only on the very left of the form field instead of the entire form field, in IE only. (IE 7, that I know of).
Help?
I am willing to bet that the culprit is setting the size of the input element to 2. Give this a shot and let me know if it works any better:
<td style="text-align:center"><input type=text size="3" maxlength="2" name="place<?=$i?>" value="<?=$place?>"></td>
There are other things that I notice in your code:
<tr><th>PLAYER</th><th>Place</th><th>Champ
Discount</th><th>Didn't play</th></tr>
<? Player_list($gameID); ?>
You call the function "Player_list()", but your function is named "player_list()".
foreach($listrow as $key=>$value) $$key = $value;
is the same as:
extract($listrow);
Thanks for the tips, Michael. Found the issue. I have a css-driven horizontal dropdown menu (http://www.udm4.com/) in a header div that drops down over the content div's. Above that, I have a javascript-driven login panel that can drop down over the header div. In playing around with the z-indexes so that everything played nice, I set the content div that holds the form to a z-index of -1. Even after taking out everything except that one div and that one form, IE doesn't like negative z-indexes. I don't know if negative z-indexes constitute valid CSS and IE is just being a tard again, or if other browsers are just too lenient.