Why does this form not update the values that the items in my session? The session seems to keep track of the value fine, before the form tries to allow users to edit the value. Here's what I wrote out form submit:
<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['Cart'][$key]);
}else{
$_SESSION['Cart'][$key]['quantity']=$val;
}
}
}
?>
And here's the form:
<?php
$sql="SELECT * FROM products where Product_ID IN (";
foreach($_SESSION['Cart'] as $id => $value){
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY Category ASC";
$query=mysql_query($sql);
$totalquantity=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity'];
$totalquantity+=$subtotal;
?>
<tr>
<td><?php echo $row['Name'] ?></td>
<td><input = type="text" name="Quantity [<?php echo $row['Product_ID'] ?>]" size="5" value="<?php echo $_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity'] ?>"/> </td>
</tr>
<?php
}
?>
And of course, the submit button is just
<button type="Submit" name="Submit">Update selection</button>
It looks like it should all work out properly, but it doesn't update.
Submit should be "submit" most probably.Change it either in the input filed or in the $_POST["Submit"]
Related
I'm making something for a stocktake.
I have a form that generates with 4 fields. item_code, item_name, packing, quantity
<form action="goods.php" method="post">
<table>
<!-- Headers -->
<tr>
<td><b>Item Code</b></td>
<td><b>Description</b></td>
<td><b>Packing</b></td>
<td><b>Quantity</b></td>
</tr>
<?php
for ($i = 0; $i <= 50; $i++) {
?>
<tr>
<td>
<INPUT TYPE="TEXT" NAME="item_code[<?php echo $i; ?>]" SIZE="6" VALUE="
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $_POST["item_code"][($i)];
}
?>"></td>
<td><?php
if (!empty($_POST["item_code"][($i)])) {
$result = FetchData($_POST["item_code"][($i)]);
echo $result['category'];
}
?>
</td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['item_name'];
}
?></td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['packing'];
}
?></td>
<td><INPUT TYPE="TEXT" NAME="quantity[<?php echo $i; ?>]" SIZE="5" VALUE="
<?php
if (!empty($_POST["quantity"][($i)])) {
echo $_POST["quantity"][($i)];
} else {
echo "";
}
?>"></td>
A js function for the button
<script>
function fillForm(value) {
document.getElementById('value').innerHTML = value;
}
</script>
and a list that is generated with 3 fields. item_code, item_name, packing
<?php
$dbh = dbh_get();
$sql = 'SELECT * FROM goods as goods(item_code, sort) order by human_sort(goods.item_code)';
$v = array();
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
print '
<tr>
<td class="buttonL" id="<php ' . $r['item_code'] . ' ?>" onclick="fillForm()">' . $r['item_code'] . '</td>
<td>' . $r['item_name'] . '</td>
<td>' . $r['packing'] . '</td>
</tr>' . "\n";
}
dbh_free($dbh);
?>
}
I want to put a button on each list row and when it's clicked it populates the first three fields in the form, leaving quantity to be filled out. Then when another is clicked it populates the next form row etc,. It's working fine manually entering from the list, but the list is nearly 5000 items so it's a hassle to keep searching then scrolling up and entering the values.
I don't see how to do this with PHP so I assume I need a javascript function, which is where I'm lost. Let me know if you need more info.
I am trying to pass hidden value from page to another page and it work fine only for first record however for other records it's showing error
Here is the code:
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_id" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead">
<?php echo $row["jdes"] ?>
</p>
<button type="button" id="requestthis" class="btn btn-primary">
Request
</button>
<?php
}
} else {
echo "Nothing to display Yet";
}
?>
jobs-inner.php
<?php
echo $_GET['hidden_id'];
?>
Javascript:-
$(function() { //ready function
$('#requestthis').on('click', function(e){ //click event
e.preventDefault();
var hidden_id = $('#hidden_user_id').val();
var url = "jobs-inner.php?hidden_id="+hidden_id;
window.location.replace(url);
})
})
Error:-
Undefined index: hidden_id in C:\wamp64\www\project\jobs-inner.php on line 3
It might be a simple problem but I am a beginner and I can't figure it out.
Your value is unique but the id isn't. Make the id of the input unique something like below.
<input type="hidden" id="hidden_user_<?php echo $row["id"] ?>" value="<?php echo $row["id"] ?>">
but you would have to do a count on code below to make it display base on how many rows you have.
<?php
echo $_GET['hidden_id'];
?>
Without JavaScript
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
$count = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_<?php echo $count ?>" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead"><?php echo $row["jdes"] ?></p>
<form id="<?php echo $count ?>" action="jobs-inner.php?hidden_id=<?php echo $row["id"] ?>" method="post">
<input type="submit" vaule="Request">
</form>
<?php
$count++;
}
} else {
echo "Nothing to display Yet";
}
?>
i want to create some simple system for my production which is they can scan bar-code on product and the system can view the JIT that they can use for paste the label on that product. Some of product must use 2 JIT for paste the label and some of JIT can be used on many product. I have create 3 table in mysql which is 'product','jit' and 'production_jit' with many to many relation.
i was created the inner join to pull the data from MySQL by using product number.
$code = $_POST['code'];
$sql = "SELECT product.product_number,product.product_name,product.product_jitqty,product.product_desc,jit.jit_number,jit.jit_name,jit.jit_drawer,jit.jit_port,jit.jit_specpath
FROM product
JOIN production_jit
ON production_jit.product_number = product.product_number
JOIN jit
ON jit.jit_number = production_jit.jit_number
WHERE product.product_number = '$code'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result))
{ ?>
<table>
<tr><td>Product Number</td><td>: <?php echo $row['product_number']; ?></td></tr>
<tr><td>Product Name</td><td>: <?php echo $row['product_name']; ?></td></tr>
<tr><td>Product Description</td><td>: <?php echo $row['product_desc']; ?></td></tr>
<tr><td>Product Jit Quantity</td><td>: <?php echo $row['product_jitqty']; ?></td></tr>
<tr><td>JIT Number</td><td>: <?php echo $row['jit_number']; ?></td></tr>
<tr><td>JIT Name</td><td>: <?php echo $row['jit_name']; ?></td></tr>
<tr><td><font color="red">JIT Drawer</font></td><td><font color="red">: <?php echo $row['jit_drawer']; ?></font></td></tr>
<tr><td><font color="red">JIT Port</font></td><td><font color="red">: <?php echo $row['jit_port']; ?></font></td></tr>
<tr><td>JIT Spec</td><td>: <?php echo $row['jit_specpath']; ?></td></tr>
</table>
<h1><strong>Please scan JIT barcode on JIT</strong></h1>
<table>
<form action="product3.php" method="post" name="check2">
<input type="hidden" name="id" value="<?php echo $row['ID']; ?>">
<input type="hidden" name="productnumber" value="<?php echo $row['productNumber']; ?>">
<input type="hidden" name="jitnumber" value="<?php echo $row['jitNumber2']; ?>">
<tr><td>JIT Barcode</td><td>:</td><td><input type="text" name="jitcode" maxlength="200"/></td></tr>
</table>
<table>
<td></td><td colspan="3" align="right"><input name="hantar" type="submit" value="Check"></td>
</table>
<?php
}
}
else {
?>
<script>
alert("Invalid Barcode");
window.location.href = "product.php";
</script><?php
}
mysqli_close($conn);
If the product used only one JIT, the interface look good,but if the Product used 2 JIT, the system show the looping data.
In logic situation, the view is correct, but how can i customize, the repeated data will not show by the system. for this example, only jit table can show in the looping. as the picture below, one product have 2 JIT, so i just to show about product information once, but only JIT Infomation i want to show follow the data..
IF you're saying you only want one of the datasets to show up, just use break so only the first dataset will show up:
<?php
$phead='';
$pbody='';
while($row = mysqli_fetch_assoc($result))
{
if(empty($phead)){
$phead.='<table>';
$phead.='<tr><td>Product Number</td><td>: '.$row['product_number'].'</td></tr>';
$phead.='<tr><td>Product Name</td><td>: '.$row['product_name'].'</td></tr>';
$phead.='<tr><td>Product Description</td><td>: '.$row['product_desc'].'</td></tr>';
$phead.='<tr><td>Product Jit Quantity</td><td>: '.$row['product_jitqty'].'</td></tr>';
}
$pbody.='<tr><td>JIT Number</td><td>: '.$row['jit_number'].'</td></tr>';
$pbody.='<tr><td>JIT Name</td><td>: '.$row['jit_name'].'</td></tr>';
$pbody.='<tr><td><font color="red">JIT Drawer</font></td><td><font color="red">: '.$row['jit_drawer'].'</font></td></tr>';
$pbody.='<tr><td><font color="red">JIT Port</font></td><td><font color="red">: '.$row['jit_port'].'</font></td></tr>';
$pbody.='<tr><td>JIT Spec</td><td>: '.$row['jit_specpath'].'</td></tr>';
}
//Now, we only have 1 instance of $phead and can have multiple $pbody;
if(!empty($phead)){
echo $phead;
echo $pbody;
echo '</table>';
}
?>
I've updated my answer to match your conditions. 1 set of product header information and multiple JIT info.
With this code, the ajax html is loaded fine and the click event triggers but when I try to get the form parent of the clicked button it seems to fail. The output from the alert is 'undefined'. How can I get the form data?
$( "#offertable" ).load( "offer-get.php", function() {
//$('#loadingDiv').hide();
$("#offertable").on('click', '#submit', function(event){
alert($(this).parents('form:first').attr("action"));
//event.preventDefault(); // this will prevent from submitting the form
//$(this).prop('disabled',true);
var postData = $(this).parents('form:first').serialize();
var formURL = $(this).parents('form:first').attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
alert("Offer updated successfully");
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("Failed to update offer");
}
});
return false;
});
} );
I've tried using the submit event on the form (which would give me form object that I can get the data from);
$("#offertable").on('submit', '#updateoffer', function(event){
But it never fires.
EDIT: Form returned from offer-get.php
<tr>
<form action="offer-update.php" id="updateoffer" name="updateoffer" method='post'>
<input type="hidden" name="pendingofferid" value="<?php echo $record['pendingofferid'] ?>">
<input type="hidden" name="offerid" value="<?php echo $record['offerid'] ?>">
<input type="hidden" name="oldpaused" value="<?php echo $record['paused'] ?>">
<input type="hidden" name="oldpayout" value="<?php echo $record['payout'] ?>">
<td><?php echo $record['offerid']; ?></td>
<td><img src="<?php echo $record['picture']; ?>" height="50" width="50"></td>
<td><?php echo $record['name']; ?></td>
<td><?php echo $record['trackinglink']; ?></td>
<td><?php echo $record['country']; ?></td>
<td><?php echo $record['device']; ?></td>
<td>
<?php if ($approved) : ?>
<input type="number" name="payout" min="0" step="0.01" value="<?php echo $record['payout']; ?>">
<?php else: echo $record['payout']; ?>
<?php endif; ?>
</td>
<td>
<?php if ($approved) : ?>
<select name="status" class="form-control required" >
<option value="Live" <?php if(!$paused) echo 'selected="selected"'; ?> >Live</option>
<option value="Paused" <?php if($paused) echo 'selected="selected"'; ?> >Paused</option>
</select>
<?php else: echo 'Pending'; ?>
<?php endif; ?>
</td>
<td>
<?php if ($approved) : ?>
<button type='submit' name='submit' id='submit' class='btn btn-userid'>Update</button>
<?php endif; ?>
</td>
</form>
</tr>
Obviously this is not the whole code, don't want to release clients sensitive information but this should be enough. This part is looped for each result from the DB query. So there are multiple forms with same ids. If I use different ids for each form, I'm not sure how I'd get the event to fire otherwise since the number of rows isn't known until the data is returned.
I have been toying with this for a while now and i can not get each row to send its specific values that are displayed to a javascript function only the 1st rows values are sent no matter which row is clicked?
I need to send the values for each specific row dependng on the results of the mysql result.
Below is the code that i have which only sends the values of the 1st row.
<?php
require 'core/init.php';
$records = array();
$result = ("(SELECT * FROM message ORDER BY id DESC LIMIT 25)ORDER BY id ASC");
$results = ($db->query($result));
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
if(!count($records)){
echo 'no records';
}else{
?>
<table>
<?php
foreach ($records as $r){
?>
<tr>
<td><div id="modOptions" onclick="modOptions()"><?php echo escape($r->sender); ?></div></td>
<td><?php echo escape($r->message); ?></td>
<input type="hidden" id="modOptionsIp" value="<?php echo escape($r->ip); ?>"/>
<input type="hidden" id="modOptionsSender" value="<?php echo escape($r->sender); ?>"/>
<input type="hidden" id="modOptionsMessage" value="<?php echo escape($r->message); ?>"/>
</tr>
<?php
}
?>
</table>
<?php
}
?>
It displays everything ok just doesnt give each row its specific values
Any pointers are much appreciated.
Change the js modOptions function so it can take parameters
function modOptions(ip, sender, message) {
//ip sender and message are from the row you clicked on
}
and render the onclick like this:
onclick="modOptions('<?php echo escape($r->ip); ?>', '<?php echo escape($r->sender); ?>', '<?php echo escape($r->message); ?>' );"
those hidden fields you have now are useless