Shopping cart's item will not update quantity - php

I am making a dummy online store for a university project. Ive run into a problem I can't get my head around. Items can be added/removed from the cart but the quantity will not be updated if someone tries to add an item that already exists in the cart.
The HTML shown is the code that displays the item on the homepage.php that can be added to the cart.
The PHP shown is code that works with the 'add' action - Adding new item to cart, or if it is already current, it should add the quantity selected to the quantity already in the cart. The latter function will not work.
HTML
<div class="laptops"><br />
<img src="<?php echo "img/" . $row["img"] ?>"><br />
<div class="laptopText">
<h3><?php echo $row["brand"] . $row["model"] ?></h3><br />
<p><?php echo $row["cpu_type"] . ", " . $row["cpu_model"] . ", " . $row["OS"] . ", " . $row["RAM"] . ", " . $row["RAM_type"] . ", " . $row["storage"] . ", " . $row["display"] . ", " . $row["gpu"]?></p><br />
<p class="price"><?php echo "£" . $row["price"] ?></p><br />
<div class="itemFormDiv">
<form method="post" action="homepage.php?action=add&id=<?php echo $row["ID"] ?>">
<input class="quantity" name="quantity" type="number" min="1" max="9" value="1">
<input type="hidden" name="hidden_name" value="<?php echo $row["brand"] . $row["model"] ?>">
<input type="hidden" name="hidden_desc" value="<?php echo $row["cpu_type"] . ", " . $row["cpu_model"] . ", " . $row["OS"] . ", " . $row["RAM"] . ", " . $row["RAM_type"] . ", " . $row["storage"] . ", " . $row["display"] . ", " . $row["gpu"]?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["price"] ?>">
<input class="addTooBasket" name="addTooBasket" type="submit" value="Add to Basket">
</form>
</div>
</div>
</div>
(When I add the PHP to stackoverflow it keeps preventing me from submitting the question so here it is on pastebin )
Can anyone work out how to fix this problem?

Set $counter = 0; before foreach, increment it in the end of each step and try something like $_SESSION["shopping_basket"][$counter]['item_quantity'] += intval($_POST["quantity"]);

Related

How to append new elements to the end of a session in php?

I have a Session where I am storing some IDs and I would like to add to the end of that session a string (attach to its own respective id).
Here I have a table with some elements and I have some checkboxes there so when each checkbox is selected I am taking their ids and when posting I am storing those ids to a session:
<form method="post">
<?php if (!empty($arr_devices)) { ?>
<?php foreach ($arr_devices as $device) : ?>
<tr>
<td>
<input type="checkbox" name="devices[]" value="<?php echo $device["id"] ?>">
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["name"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["device_no"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["barcode"] . '</a>'; ?>
</td>
<td>
<div class="input-group">
<textarea name="dev_comment" placeholder="Write your comment here" rows="1" cols="50"><?php echo $dev_comment; ?></textarea>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
<input class="btn" type="submit" name="submit" value="Report">
<form>
Here is the the way I am storing those IDs:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$_SESSION['devices_id'] = $_POST['devices'];
}
what I would like to do is to add the $dev_comment to that Session. So that when I use that Session I need to have the $dev_comments attached to each respective id. How can I implement that?
At first, ensure that $_SESSION['devices_id'] exists and is an array:
if(!isset($_SESSION['devices_id'])) {
$_SESSION['devices_id'] = [];
}
The, $_POST['devices'] is an array, you can easily merge:
$_SESSION['devices_id'] = array_merge($_SESSION['devices_id'], $_POST['devices']);

When I search something in search box , the keyword which I entered disappears after giving the result

I have an ideal search-box which searches item from a given listing. My searching is working fine, the problem is when I enter something in search box to search, it gives me result but my search box gets empty.
For eg If I search "Electronics" in search box, it gives me result of electronics but my search box gets empty. It should be written with "Electronic" when it gives me result.
Probably, I should be using GET method instead is it so?
Here is my code for searching:
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
echo "<tr bgcolor=''>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td><img style='border:#666 1px solid;' width='70' src='http://localhost/jaymin/My%20Store/storeadmin/category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['parent'] . "</td>";
echo "<td>" . $row['cat_status'] . "</td>";
echo "<td><a href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
change your code to
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
instead of
Search: <input type="text" name="term" /><br />
Try this,
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
If you want this in your url use GET instead of POST like,
<form action="" method="get">
Search: <input type="text" name="term" value="<?php if(isset($_GET['term'])){ echo $_GET['term']; } ?>" /><br />
<input type="submit" value="Submit" />
</form>
This is because you are submitting the form and after form submit textbox values disappears. To overcome this try:
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>

checkbox array not working

<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$counter'>
for some reason, when I run the form with this name for this input, it won't run the PHP script, it won't even run the beginning of the script.
Is there some reason this isn't working? am I doing it wrong? I thought this is the actual way this would work.
[Edit]
this is the actual full script in the form:
foreach($producten as $row)
{
echo("
<label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect' for='checkbox-" . $counter ."'>
<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$counter'>
<span class='mdl-checkbox__label'>" . $row['productcode'] . ' ' . $row['categorie'] . ' ' . $row['merk'] . ' ' . $row['type'] . ' ' . $row['cpu'] . ' ' . $row['ram'] . ' ' . $row['os'] . ' ' . $row['hdd'] . ' ' ."</span>
</label>
");
$counter++;
}
.
I have checked with below code as related your code. It's working with Checkbox array. I hope it's will help you. All the best.
<form action="#" method="POST">
<?php
$producten = array('1a','2b','3c','4d');
$counter = 1;
foreach($producten as $row)
{
echo("
<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$row'> <label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect' for='checkbox-" . $counter ."'> $row </label><br>
");
$counter++;
}
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php echo "<pre>"; print_r($_REQUEST); ?>
Output:
Array
(
[product] => Array
(
[0] => 1a
1 => 2b
[2] => 3c
[3] => 4d
)
)

Add a 'name' to a Paragraph tag to work with POST

I'm working on a php project where I want to receive a _POST of a paragraph in my form.
What's the best way to do that ?
I thought the easiest way to do that was adding a 'name' to the tag but that seems not to work.
<form action="Index.php" method=post enctype="multipart/form-data">
<?php
foreach($optevragenDataVanWerknemer as $info)
{
echo "<p name = 'wnr'><strong>".$info->wnr."</strong></p> </br>";
echo "<p> Afdeling : " . $info->afdeling . "</p>";
echo "<p> Functie : " . $info->ftienaam . "</p>";
echo "<p> Salaris : " . $info->salaris . "</p>";
echo "<p> Vesnaam : " . $info->vesnaam . "</p>";
echo "<p><img src='" . Config::getConfigInstantie()->getUploadMap() . '/' . $info->foto . "' alt=foto width=45 height=30></p>";
?>
<p>
<input type=hidden name="MAX_FILE_SIZE" value=1000000>
<label for=foto>Nieuwe foto </label>
<input type=file name=foto id=foto>
<input type=submit name=uploadKnop value="Upload Foto">
</p>
<?php
}
?>
$wnr = $_POST["wnr"]);
You can get only form elements (Form Elements) in the action page ie from $_POST. You can better assign the same value to a hidden element and access it.
<form action="Index.php" method=post enctype="multipart/form-data">
<?php
foreach($optevragenDataVanWerknemer as $info)
{
echo "<p><strong>".$info->wnr."</strong></p> </br>";
echo "<input type='hidden' name = 'wnr' value='{$info->wnr}'> </br>";
echo "<p> Afdeling : " . $info->afdeling . "</p>";
echo "<p> Functie : " . $info->ftienaam . "</p>";
echo "<p> Salaris : " . $info->salaris . "</p>";
echo "<p> Vesnaam : " . $info->vesnaam . "</p>";
echo "<p><img src='" . Config::getConfigInstantie()->getUploadMap() . '/' . $info->foto . "' alt=foto width=45 height=30></p>";
?>
<p>
<input type=hidden name="MAX_FILE_SIZE" value=1000000>
<label for=foto>Nieuwe foto </label>
<input type=file name=foto id=foto>
<input type=submit name=uploadKnop value="Upload Foto">
</p>
<?php
}
?>
</form>
You need to have data you want in $_POST to be within a form element. A <p> tag will never submit data. Try <textarea> or <input>
foreach($optevragenDataVanWerknemer as $info) {
echo "<textarea name = 'wnr'>" . $info->wnr . "\n";
echo "Afdeling : " . $info->afdeling . "\n";
echo "Functie : " . $info->ftienaam . "\n";
echo "Salaris : " . $info->salaris . "\n";
echo "Vesnaam : " . $info->vesnaam . "</textarea>";
}
I took out your <img> tag because that's an entirely different process to upload. If you're trying to store the raw HTML you could add it back into the <textarea> and it would submit as text.

trying to get PHP to post string to a file

I'm trying to use PHP to send the values of a form, in the format of a list item which contains an image and a caption to be added to a textfile which is then read by another page. The image upload part works fine but the output of the php looks like
<li><img alt= " " src=" " > </li>
when it should be more like <li><img src="img/comics/sa005.png">Comic 5 - </li>
<?php
//get item details
$title = $_POST['title'];
$alt = $_POST['alt'];
$caption = $_POST['caption'];
$location = $_POST['location'];
//file location
$file = 'listitems.txt';
// The list item to add to the file
$string = ("\n <li>" . $title . "<img alt= \" " . $alt . " \" src=\" " . $location . " \" > " . $caption . "</li>");
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $string, FILE_APPEND | LOCK_EX);
echo "string = ";
echo $string;
?>
<form action="appendlist.php" method="post">
<input name="title" type="textbox" placeholder="Title" class="form-control" ><br><br>
<input name="alt" type="textbox" placeholder="Alt text" class="form-control" ><br><br>
<input name="caption" type="textbox" placeholder="Image caption" class="form-control" ><br><br>
<input name="location" type="textbox" placeholder="File location" class="form-control" >
<br>
<input type="submit" value="Add Image" name="submit">
</form>
Your code is also right. but you should have remove extra blank space. if that is not work then another problem.
instead of trying to escape the double quotes, try to just use single quotes where you need a string encapsulated
$string = ("\n <li>" . $title . "<img alt='" . $alt . "' src='" . $location . "' > " . $caption . "</li>");

Categories