I am trying to update quantity as i fill the quantity field and click on update button. It works but it shows the form fields of each item in the loop and works for the last entry update only. Something like this.
/EcommerceClient/index.php?page=cart&action=update&id=3&name=%09%0D%0ACool+T-shirt&color=blue&size=XL&quantity=4&id=4&name=HBD+T-Shirt&color=yellow&size=XL&quantity=900
In the above link it should only get the information associated with id=3 only because i tried to update the quantity of id=3.
Here is my code. Any suggestions or help will be highly appreciated.
Code
if(isset($_GET['action']) && $_GET['action']=="update"){
$id= intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
$qty = $_GET['quantity'];
$index = $id." ".$color. " ".$size;
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']=$qty;
print_r($_SESSION['cart'][$index]);//It just shows me the last item array.
}
}
?>
<form class="product" method="get" action="index.php">
<table>
<input type="hidden" name="page" value="cart">
<input type="hidden" name="action" value="update">
<?php
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $value){
?>
<tr>
<input type="hidden" name="id" value="<?php echo $value['id'] ?>">
<input type="hidden" name="name" value="<?php echo $value['name'] ?>">
<input type="hidden" name="color" value="<?php echo $value['color'] ?>">
<input type="hidden" name="size" value="<?php echo $value['size'] ?>">
<td><?php echo $value['id'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['price']?> </td>
<td><?php echo $value['quantity']?><input type="text" name="quantity" value="<?php echo $value['quantity'] ?>"></td>
<td><?php echo $value['color'];?> </td>
<td><?php echo $value['size']; ?></td>
<td><?php echo "$" .$value['price']*$value['quantity']. ".00"; ?>
</tr>
<?php
}
}
?>
<tr><td><button type="submit">Update</button></td></tr>
</table>
</form>
You can update the quantity by not posting or getting all the cart product. Either you can use simple JS to update the field value or use SESSION variable array to update data as #Marc B mentioned.
Related
So I've been doing this school project and need to make it so I can edit whatever is in the table. Whenever i click on "Edit" it redirects me correctly but in the form it says there is an undefined variable eventhough that variable is used pretty much everywhere.
Here is some code of the table:
<table style='margin-left:auto ; margin-right:auto;'>
<tr>
<th>#</th>
<th>Name</th>
<th>Zeit</th>
<th>Datum</th>
<th>Titel</th>
<th>Inhalt</th>
<th>Ort</th>
</tr>
<?php
if($stmt=$db->prepare("SELECT * FROM terminkalender")) {
$stmt->execute();
$stmt->store_result();
$zeilen = $stmt->num_rows();
$stmt->close();
}else {
$zeilen = 0;
}
if($zeilen > 0) {
//nur wenn Einträge, dann ausgeben
if($stmt = $db->prepare("SELECT * FROM terminkalender ORDER BY zeit,datum DESC")) {
$stmt->execute();
$stmt->bind_result($id,$name,$zeit,$datum,$ort,$titel,$inhalt);
$stmt->store_result();
//Ausgabe starten
while($stmt->fetch()){
echo "<tr>";
?>
<td><?php echo $id ;?></td>
<td><?php echo htmlspecialchars($name) ;?></td>
<td><?php echo htmlspecialchars($datum) ;?></td>
<td><?php echo htmlspecialchars($zeit) ;?></td>
<td><?php echo htmlspecialchars($ort) ;?></td>
<td><?php echo htmlspecialchars($titel) ;?></td>
<td><?php echo htmlspecialchars($inhalt); ?></td>
<td><a href='edit.php?id=<?php echo $id;?>'>Edit</a></td>
<td><a href='delete.php?id=<?php echo $id;?>'>Delete</a></td>
<?php
echo "</tr>" ;
}
}
}
?>
</table>
and here for the edit.php file:
<?php
include("./config/connect.inc.php");
$id = $_GET['id']; // get id through get string
$result=mysqli_query($db,"SELECT * FROM terminkalender WHERE id=$id");
if(isset($_POST['update'])) {
$name=$_POST['name'];
$datum=$_POST['datum'];
$zeit=$_POST['zeit'];
$ort=$_POST['ort'];
$titel=$_POST['titel'];
$inhalt=$_POST['inhalt'];
$result = "UPDATE terminkalender
SET name='$name',
datum='$datum',
zeit='$zeit',
ort='$ort',
titel='$titel',
inhalt='$inhalt'
WHERE id=$id";
header("location: ausgabe.php");
}
?>
<form name="form" method="POST" action="edit.php">
<input type="text" name="name" value="<?php echo $name; ?>" Required>
<input type="date" name="datum" value="<?php echo $datum; ?>" Required>
<input type="time" name="zeit" value="<?php echo $zeit; ?>" Required>
<input type="text" name="ort" value="<?php echo $ort; ?>" Required>
<input type="text" name="titel" value="<?php echo $titel; ?>" Required>
<input type="text" name="inhalt" value="<?php echo $inhalt; ?>" Required>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<input type="submit" name="update" value="Update">
</form>
Ẁould be really awesome if anyone could help. Thanks is advance!
Maybe you can try using isset function of php to check if that variable contains a value or not for more details check:-https://www.stechies.com/notice-undefined-variable-in-php/
hope you are all doing well. I'm not too sure on how I worded the title so I'm sorry for that.
I have this code:
<form method="POST" action="checkoutManager.php" name="submitOrder">
<?php
if(isset($_SESSION["cart_item"])) {
$item_total = 0;
$total;
foreach ($_SESSION["cart_item"] as $item) {
$item_total += (($item["price"]-$item["discount"])*$item["quantity"]);
$total = $total + $item_total;
?>
<tr>
<td class="product-name">
<?php echo $item["name"]; ?> <strong class="product-qty"> × <?php echo $item["quantity"]; ?></strong>
</td>
<td class="product-total">
<span class="amount"><?php echo "$".$item_total; ?></span>
</td>
</tr>
<?php
$item_total = 0;
}
}
?>
<input type="submit" name="btnSubmitOrder" value="Submit Order">
</form>
How would I go about submitting all the $_SESSION items in using form tag. I have tried submitting as an array but failed. All help appreciated. Thanks in advance
why don't you create hidden elements like
<input type="hidden" name="total" value="<?php echo $total; ?>">
and anything you need use hidden input field and you can access them after the submit button is clicked by using $_POST in your checkoutManager.php
UPDATE
simply you can process all the fields in checkoutManager.php using session, you dont need any form to be submitted. create a link to checkoutManager.php and do all the calculations there
Try below code.
In this code I have added one hideen count field.
By using count u can use for loop after submit.
I display only one input for name but u can add multiple inputs inside for loop of form.
I also concated $i variables, so all input names are different as name1, name2,... U can use it on submit as $_POST['name'.$i] after submit.
<form method="POST" action="checkoutManager.php" name="submitOrder">
<?php
<input type="hidden" name="count" value="<?php echo count($_SESSION["cart_item"]); ?>">
$i=0;
foreach ($_SESSION["cart_item"] as $item)
{
?>
<input type="text" name="input_name<?php echo $i; ?>" value="<?php echo $item['name']; ?>" />
<?php
$i++;
}
?>
<input type="submit" name="btnSubmitOrder" value="Submit Order">
</form>
You need to add input fields in foreach loop for all your items that are coming in $_SESSION["cart_item"]. After submission, you need to use foreach again for inserting all your items in your temp table one by one.
Update your code like below:
<form method="POST" action="checkoutManager.php" name="submitOrder">
<?php
if(isset($_SESSION["cart_item"])) {
$item_total = 0;
$total = 0;
foreach ($_SESSION["cart_item"] as $item) {
$item_total +=
(($item["price"]-$item["discount"])*$item["quantity"]);
$total = $total + $item_total;
?>
<input type="hidden" name="price[]" value="<?php echo $item["price"]; ?>">
<input type="hidden" name="name[]" value="<?php echo $item["name"]; ?>">
<input type="hidden" name="discount[]" value="<?php echo
$item["discount"]; ?>">
<input type="hidden" name="quantity[]" value="<?php echo
$item["quantity"]; ?>">
<tr>
<td class="product-name">
<?php echo $item["name"]; ?> <strong class="product-qty"> × <?php
echo $item["quantity"]; ?></strong>
</td>
<td class="product-total">
<span class="amount"><?php echo "$".$item_total; ?></span>
</td>
</tr>
<?php
$item_total = 0;
}
}
?>
<input type="submit" name="btnSubmitOrder" value="Submit Order">
</form>
And you can access all your input fields after submission in the below way.
foreach($_POST['name'] as $key => $val){
echo $_POST['name'][$key];
echo $_POST['price'][$key];
echo $_POST['discount'][$key];
echo $_POST['quantity'][$key];
}
Hope it helps!
I am trying to pass the column values from a single row generated from a while loop. When the link is clicked I want certain values from that row to be passed as hidden fields to another page.
The code:
<form id="repeat" name="repeat" action="edit_user.php" method="post">
<?php
$counter = 0;
while($row = $result->fetch_array()) {
$color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
$counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
<td><a href="#" onclick="document.getElementById('repeat').submit();" >
<?php echo $row['firstname']; ?> <?php echo $row['surname']; ?></a></td>
<td><?php echo $row['userlogin']; ?></td>
<td><?php echo $row['accesslvl']; ?></td>
<td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="lvl" value="<?php echo $row['accesslvl']; ?>" />
<?php } ?>
</table>
</form>
What is happening is that the hidden fields are passing values for the last row of the while loop. Is it possible to do this so the hidden values being passed are from the same row of the link that is being clicked?
I've tried a few things and nothing works and I've not been able to find anything that directly relates to this problem. Any help would be appreciated. Cheers
You need to split this into two pages really. The first page displays the user info and link for each unique user, and the second page provides the edit form for posting the form data as follows...
<table>
<?php
$counter = 0;
while($row = $result->fetch_array()) {
$color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
$counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
<td>
<a href="edit_user.php?userID=<?php echo $row['userID']; ?>&accesslvl=<?php echo $row['accesslvl']; ?>">
<?php echo $row['firstname']; ?> <?php echo $row['surname']; ?>
</a>
</td>
<td><?php echo $row['userlogin']; ?></td>
<td><?php echo $row['accesslvl']; ?></td>
<td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<?php } ?>
</table>
on the edit_user page you can populate the hidden fields with the information passed in by the url query string. Remember to clean the values to prevent sql injection or xss attacks. I've used htmlentities here as an example and you may want to enhance this, read the php documentation page on htmlentites for more information (ENT_QUOTES).
$userID = htmlentities($_GET['userID']);
$accesslvl = htmlentities($_GET['accesslvl']);
<form action="modify_user.php" method="post">
<input type="hidden" name="id" value="<?php echo (int)$userID; ?>" />
<input type="hidden" name="lvl" value="<?php echo $accesslvl; ?>" />
//rest of form html
Having trouble looping through multiple rows in a SQL table and getting the info to display correctly. The closest I have got is the following:
<table border="1" cellpadding="10">
<th>Stock</th><th>Price</th><th>Shares</th><th>Value</th><th>Sell Stock</th>
<?php
$transactions = mysql_query("SELECT * FROM transactions WHERE email='$email'");
while ($rows = mysql_fetch_assoc($transactions)) {
foreach($rows as $row) {
$symbol = $row["symbol"];
$price = $row["price"];
$shares = $row["shares"];
$value = $price * $shares;
?>
<form name="sellStock" action="sell.php" method="get">
<tr>
<td><input name="symbol" type="hidden" value="<?php echo $symbol ?>"><?php echo $symbol ?></td>
<td><input name="price" type="hidden" value="<?php echo $price ?>"><?php echo $price ?></td>
<td><input name="shares" type="hidden" value="<?php echo $shares ?>"><?php echo $shares ?></td>
<td><input name="value" type="hidden" value="<?php $value ?>" /><?php $value ?></td>
<td><input name="sell" type="submit" value="Sell"></td>
</tr>
<?php
}
}
?>
</table>
The while/foreach loop goes on to display the info from the rows into a HTML table, but it displays the first character from every column as opposed to all the characters from the columns I echo to be displayed (symbol, price, and shares). Any ideas?
<table border="1" cellpadding="10">
<th>Stock</th><th>Price</th><th>Shares</th><th>Value</th><th>Sell Stock</th>
<?php
$transactions = mysql_query("SELECT * FROM transactions WHERE email='$email'");
while ($row = mysql_fetch_assoc($transactions)) {
$symbol = $row["symbol"];
$price = $row["price"];
$shares = $row["shares"];
$value = $price * $shares;
?>
<form name="sellStock" action="sell.php" method="get">
<tr>
<td><input name="symbol" type="hidden" value="<?php echo $symbol ?>"><?php echo $symbol ?></td>
<td><input name="price" type="hidden" value="<?php echo $price ?>"><?php echo $price ?></td>
<td><input name="shares" type="hidden" value="<?php echo $shares ?>"><?php echo $shares ?></td>
<td><input name="value" type="hidden" value="<?php $value ?>" /><?php $value ?></td>
<td><input name="sell" type="submit" value="Sell"></td>
</tr>
<?php
}
?>
</table>
You just had one loop too many. The while loop continues until !$row, with one row per execution, so you don't want the foreach loop you had.
I am trying to create a function where users can check a message to delete. Obviously if more than one message is checked, they will all be deleted.
I have the following code
<form action="process.php" method="post">
<input type="hidden" name="deleteMessages" />
<?php
while($row=mysql_fetch_assoc($q))
{
if($row['to_viewed'] == 0)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></b></td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></b></td>
<td><b><?php echo $row['created']; ?></b></td>
</tr>
<?php
}
else if($row['to_viewed'] == 1)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></td>
<td><?php echo $row['created']; ?></td>
</tr>
<?php
}
}
?>
<input type="submit" value="Delete All" />
</form>
I want to pass the checkbox through and if the value is 1, process it and delete it.
But how would I achieve this with mulitple messages no matter if there is one message or ten?
Thanks
In your form put:
<input type="checkbox" name="check_box_delete[]" value="<?php echo $row['id']; ?>" />
Then to process:
if(isset($_POST['check_box_delete']))
{
foreach($_POST['check_box_delete'] as $id)
{
// Delete $id
}
}
You can let php store mutliple values in an array by naming the input fields with an array designator suffix [].
For example, all checkboxes named checkbox[] will then be stored in $_POST['checkbox'][].
Note that this may not be applicable to checkboxes as their $_POST value only exists if they were checked.
<input type="checkbox"
name="mid_to_delete[]"
value="some message id"
id="mid_to_delete_some_message_id">
<label for="mid_to_delete_some_message_id">Delete "Some subject"</label>
and then
<?php
foreach ($_POST['mid_to_delete'] as $mid) {
delete_some_message_id($mid);
}
?>
Since only successful controls are submitted, and unchecked checkboxes are not successful, all the values you get will be ones that have been selected for deletion. (Obviously you still need to perform auth/authz to make sure that the messages being deleted are ones the user is allowed to delete)