I'm having a problem adjusting the values being passed into $_POST from the table I have set up.
Here's the code that's being used to set up the table:
print "<form action=\"updateitem.php\" method=\"post\">\n";
print "<table border cellpadding = 3>\n";
print "<tr>\n";
print "<th>Menu item id</th>\n";
print "<th>Menu item name</th>\n";
print "<th>Menu</th>\n";
print "<th>Menu item price</th>\n";
print "</tr>\n";
while($info = mysql_fetch_array($results)){
$mi = explode("_",$info['meta_key']);
if ($mi[2] == "food")
$mi[2] = "takeout";
print "<tr>\n";
print "<td id=\"meta_id\">".$info['meta_id']."<input type=\"hidden\" name=\"meta_id".$i."\" value=\"".$info['meta_id']."\" /></td>\n";
print "<td>".$info['post_title']."</td>\n";
print "<td>".$mi[2]."</td>\n";
print "<td id=\"price\" contenteditable=\"true\">".$info['meta_value']."<input type=\"hidden\" name=\"meta_value".$i."\" value=\"".$info['meta_value']."\" /></td>\n";
print "</tr>\n";
$i++;
}
print "</table>\n";
print "<input type=\"Submit\" value=\"Update\" />\n";
print "</form>\n";
What I'm trying to do is that when someone changes the price cell, I want the updated price to be passed in the $_POST for PHP. Is that possible?
The contenteditable attribute is not going to help you here.
You have to use javascript to notice when that is changed and do something with it.
The simple answer is, you need to make the <input> field available to the user. So change the type="hidden" to type="text" or add another field that is not hidden for the user to interact with, and then the value will be passed back to your script in $_POST.
Suggested changes
print "<form action=\"updateitem.php\" method=\"post\">\n";
print "<table border cellpadding = 3>\n";
print "<tr>\n";
print "<th>Menu item id</th>\n";
print "<th>Menu item name</th>\n";
print "<th>Menu</th>\n";
print "<th>Menu item price</th>\n";
print "</tr>\n";
while($info = mysql_fetch_array($results)){
$mi = explode("_",$info['meta_key']);
if ($mi[2] == "food")
$mi[2] = "takeout";
print "<tr>\n";
print '<td id="meta_id">' . $info['meta_id'] . '<input type="hidden" name="meta_id[]" value="' . $info['meta_id'] . '" /></td>';
print '<td>'.$info['post_title'].'</td>';
print '<td>'.$mi[2].'</td>';
print '<td id="price">';
print '<input type="hidden" name="original_price[]" value="' . $info['meta_value'] . '" />';
print '<input type="text" name="price[]" value="' . $info['meta_value'] . '" />';
print '</td>';
print "</tr>\n";
$i++;
}
print "</table>\n";
print "<input type=\"Submit\" value=\"Update\" />\n";
print "</form>\n";
I took the liberty of changing the names ( name="name[]" ) to use arrays instead of ( name="name" . $i )
This will return the fields as arrays in the $_POST array
So $_POST[price] will itself be an array like so:
$_POST['price][0]
$_POST['price][1] // and so on
So you can process the changed prices as
foreach ( $_POST['price'] as $i => $price ) {
if ( $price != $_POST['original_price'][$i]) {
// We have a price change lets update the database
// using $_POST['meta_id'][$i] as the key to the database table ? I assume ??
}
}
Related
I need to create an order form that has a checkbox for each product in a table and have a quantity box next to each product.
I need to process the products that have been checked and update an orders table with the checked products and the quantity entered.
I don't know how to link the product checkbox to the quantity field.
$sql="select * from tproducts";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table>";
echo "<tr>";
echo "<th>Product Names</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=welcome1.php method=post>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "</tr>";
}
}
echo "</table>";
echo '<input type="submit" name="submit" value="submit" />';
echo "</form>";
echo "</html>";
welcome1.php
<?php
echo "materialcode<br>";
print_r($_POST['materialcode']);
echo "<br>";
echo "qty<br>";
print_r($_POST['qty']);
?>
PHP will keep the key name you supply in the fields html.
$productIndex = 0;
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[$productIndex]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[$productIndex]' value='qty' size=5></td>";
echo "</tr>";
$productIndex++;
}
This example uses numeric indexes but you could probably use $row['intProductId'] as well.
In your php:
$_POST['materialCode'][0] will be the analog to $_POST['qty'][0] but only the checked checkboxes will be supplied. You might end up with a $_POST['materialCode'] array that has index 0, 5, and 7 if those were the only ones checked.
So use a foreach loop instead of a for loop.
foreach( $_POST['materialCode'] as $key => $value ){
$productId = $value;
$quantity = $_POST['qty'][$key];
}
I m trying to store checkbox value subject_id but when i store this value to database it just store the last selected checkbox inside the database... please help me to sort out the code so I can get all selected values.
I want to save all the checkbox values I selected,but if I check more then one checkbox it always save the last checkbox.
function get_class_section($day='',$section='',$class='')
{
print_r($section);
$sections = $this->db->get_where('class_routine' , array(
'day' => $day,'section_id'=>$section,'class_id'=>$class
))->result_array();
foreach ($sections as $row) {
echo "<table class=\"table table-bordered datatable\" id=\"table_export\" >";
echo "<thead>";
echo "<th>Subject</th><th colspan=\"2\">status</th>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>". $this->crud_model->get_subject_name_by_id($row['subject_id'],"<input type=\"text\" name=\"subject_id\" value=\"subject_id\" >");
echo "<input type=\"checkbox\" name=\"types[]\" value=\"".$row['subject_id']."\" set_checkbox('types[]', 'subject_id')>";
echo "</td>";
echo "<td>present <input type=\"checkbox\" value=\"1\" ></td>"
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
}
how to save all the check-boxes that i selected
You need foreach loop to get all checked checkboxes
Try This:
if(isset($_REQUEST['types']) && $_REQUEST['types']!=array())
{
foreach ($_REQUEST['types'] as $key => $value) {
// in $value,you will get checkbox value
}
}
You should use hidden element for all checkbox
Try this
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>
<input type=\"hidden\" name=\"types[]\" value=\"0\"> ";
I'm trying to create a Table where you can 'Sign Off' items populating it. Currently the table looks to be populated fine, however when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
When I say 'Sign Off' I mean to insert someone's initials into the mysql table row associated with that item. I first of course need to get the POST submission handled correctly.
That probably wasn't articulated perfectly, so please take a look at my current code;
echo "<form action='signoff.php' method='post' id='signoffform' style='display:inline;'>";
echo "<p3><table id='myTable3' class='tablesorter'>
<thead>
<tr>
<th>To Delete</th>
<th>Directory</th>
<th>Space Used</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='signoff' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
echo "</table></p3>";
echo "</form>";
signoff.php
<?php
echo $_POST["signoff"];
echo "<br />";
echo $_POST["dir"];
?>
Please feel free to request more info
... when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
The problem is because of your name attributes, name='signoff' and name=dir. When you loop through the result set, the name attribute value gets overwritten in each iteration. Hence whatever button you click, you'll always get the last row values.
So the solution would be like this:
Change your while loop in the following way,
// your code
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><input type='text' name='signoff[".$row['directory']."]' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir[".$row['directory']."]' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
// your code
And on signoff.php page, process your form in the following way,
foreach($_POST['dir'] as $dir){
if(!empty($_POST['signoff'][$dir])){
$signoff = $_POST['signoff'][$dir];
// That how you can get $dir and the corresponing $signoff value
}
}
Sidenote: If you want to see the entire $_POST array structure, do var_dump($_POST); on signoff.php page.
I currently have two dropdown menus that a user will select the course and a golfer which will load a scorecard.
What I am trying to do now is when a user enters a value into the "Score" field I want the "points" to be automatically shown ("Points" is a read only input field). To do this I am assuming that I will have to give each table row for score and points a specific ID.
echo "<div class='scorecardTable'>
<table 'id=scorecardTable'>
<tr>
<th>HoleNumber</th>
<th>Par</th>
<th>Stroke Index</th>
<th>Score</th>
<th>Points</th>
</tr>'";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['holeNumber'] . "</td>";
echo "<td>" . $row['par'] . "</td>";
echo "<td>" . $row['strokeIndex'] . "</td>";
echo "<td> <input type='text' maxlength='2' id='score' /></td>";
echo "<td> <input type='text' id='points' readonly></td>";
echo "</tr>";
}
echo "</table>";
From the above code I am using PHP to print the information from my database but I was just wondering if anyone would know how to assign a unique ID to each of the score and points input fields so I can apply the calculation to each user input.
You could use a variable to count the rows and set the id.
$rowIndex = 0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['holeNumber'] . "</td>";
echo "<td>" . $row['par'] . "</td>";
echo "<td>" . $row['strokeIndex'] . "</td>";
echo "<td> <input type='text' maxlength='2' id='score$rowIndex' /></td>";
echo "<td> <input type='text' id='points$rowIndex' readonly></td>";
echo "</tr>";
$rowIndex++;
}
You may also use
id='score[$rowIndex]'
I have a simple PHP form which displays inputs with values from a mysql DB and sends the form results to another page which updates a db table based on the GET results:
echo "<table>";
echo "<tr>";
echo " <th>Project No</th>
<th>Customer Name</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input value=" . $row['project_no'] . "></input></td>";
echo "<td><input value='" . $row['cust_name'] . "'></input></td>";
echo "<td><input value='" . $row['description'] . "'></input></td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' value='Update' />";
echo "</form>";
In updateprojects.php when I do:
echo $_GET['project_no'].$_GET['cust_name'].$_GET['description'];
I don't see any values. Why is this?
In the input field you need to specify the parameter name with the "name" attribute.
echo "<tr>";
echo "<td><input name=\"project_no\" value=" . $row['project_no'] . "></input></td>";
echo "<td><input name=\"cust_name\" value='" . $row['cust_name'] . "'></input></td>";
echo "<td><input name=\"description\" value='" . $row['description'] . "'></input></td>";
echo "</tr>";
You are forgetting the input names:
<td><input name='project_no' value=" . $row['project_no'] . "></input></td>
If you don't do this php doesn't know what you mean by 'project_no'. Each input needs a name.
It is because you do not assign names to the input tags:
echo "<td><input name=\"project_no\" value=\"" . $row['project_no'] . \"></input></td>
The above should work.
Your inputs have no name attribute
Since you are creating form inputs with a loop this should work for you (you can't have 2 input fields with same name), just grab an id of row/record from the database too:
echo "<form method='get' action='updateprojects.php'>";
echo "<table>";
echo "<tr>";
echo " <th>Project No</th>
<th>Customer Name</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo '<td><input value="' . $row['project_no'] .'" name="form['.$row['id'].']['project_no']"/></td>';
echo '<td><input value="' . $row['cust_name'] .'" name="form['.$row['id'].']['cust_name']"/></td>';
echo '<td><input value="' . $row['description'] .'" name="form['.$row['id'].']['description']"/></td>';
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="submit" value="Update" />';
echo "</form>";
if(isset($_GET['submit'])){
foreach($_GET['form'] as $id=>$column){
//update your database where id=$id. This is just testing
echo 'Row '.$id .' =>'. $column['project_no'].'-'.$column['cust_name'].'-'.$column['description'];
}
}
You need to add the name="" attribute to your inputs.
Something that has helped me in the past, is to use the Net tab in Firebug and enable the Persist option to see what the browser is actually sending back. In one case, the form had duplicate entries for some for fields, but the second entry wasn't sending the correct values.
Please inform the type of submit, or hidden text. And / or verify that you are returning data from sql query.
It appears to me that you need to have a name="project_no" in you HTML input.