Form disappears on submission Shopping cart - php

So at the moment I am trying to create a shopping cart using PHP
currently I'm having trouble submitting a form which adds a product to cart. So essentially I have linked up my database to the my shop.php page, so it loops through each item in the DB and displays the product. Each product has a form which uses POST which when submitted, adds the item to the cart SESSION.
So at the moment, when I click the 'add to cart' button the item is added and works fine. But when i click 'add to cart' for the same item again, the entire product gallery stops working. The issue only occurs when I try to add an item that's already been added. I feel like the issue revolves around submitting the same form twice but I am not quite sure.
<div class="container">'
<div class="box">'
<?php
$query = "SELECT * FROM items";
$result_set = mysqli_query($link, $query);
if (mysqli_num_rows($result_set) > 0){
while($row = mysqli_fetch_array($result_set)){
?>
<div class="displayCase"><br><br><br>
<form method="POST" action="shop.php?action=add&id=<?php echo $row['id']; ?>">
<img class="phone1" src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] )?>" />
<h1> <?php echo $row['name'] ?> </h1>
<h2> <?php echo "$" . $row['price'] ?> </h2>
<label id="QTY">QTY </label><input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number"><br />
<select class="phoneType">
<option value="X">Select Device</option>
<option value="X">iPhone X/XS</option>
<option value="X">iPhone X/XS Max</option>
<option value="X">iPhone 11</option>
<option value="X">iPhone 11 Pro</option>
<option value="X">iPhone 11 Pro Max</option>
</select><br />
<input type="hidden" name="name" value="<?php echo $row['name'] ?> ">
<button type="submit" name="add" value="addToCart">Add To Cart</button>
</form>
</div>
<?php
}
}
?>
</div>
</div>
Cheers
EDIT:
Okay so for some reason it seems to take a full reload after the form is submitted for it to work. So I tried this and it seemed to fix it. Still unsure why this is the case, and whether this is a clean way or if there is a better way of doing it
if (isset($_POST['name'])){
header("Loction: redirect.php");
}
then I created a redirect.php page redirects back to shop.php

Related

How to maintain the PHP GET value on the address bar if I use another GET?

I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>

Why am I unable to access form data when submitting using OnChange?

I have a simple HTML select option in which it should automatically send OnChange. The form seems to send or "Reload the page" should I say. I am not getting ANY data the other end. I have tried POST and GET I have renamed every section of the forms data and changed the entire if(isset() information. Any ideas why I am not getting any data?
FORM:
<?php
$system1 = mysqli_fetch_assoc(mysqli_query($conn,
"SELECT * FROM ap_system_status WHERE system_id = '1'"));
$system_id_1 = $system1['system_id'];
$system_name_1 = $system1['system_name'];
$system_status_1 = $system1['system_status'];
?>
<strong><? echo $system_name_1; ?></strong>
<div align="right" style="float:right">
<div class="form-group">
<form action="" method="GET" id="system_status" name="system_status">
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" />
<select class="form-control" onchange="this.form.submit()" id="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors'>Experiencing Errors</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Being Updated'>Being Updated</option>
<option value='<i class="fa fa-exclamation-triangle" style="color:#F60"></i> Maintenance Mode'>Maintenance Mode</option>
<option value='<i class="fa fa-times-circle-o" style="color:#F00"></i> System Offline'>System Offline</option>
<option value='<i class="fa fa-check-circle-o" style="color:#090"></i> Good Service'>Good Service</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
DATA COLLECTION (Very basic but only testing!!):
<?php
if(isset($_POST['system_status'])){
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
}else{
}
?>
A couple of changes to your form and PHP should get this working. In order for the form elements to be submitted with the form, they must have name attributes, and the form method must match the superglobal ($_GET or $_POST) you are accessing in PHP.
<!--Change the form method to match what you are using in your PHP-->
<form action="" method="POST" id="system_status" name="system_status">
<!--Add a name to this input-->
<input type="hidden" id="system_id" value="<? echo $system_id_1; ?>" name="system_id" />
<!--Add a name to this select-->
<select class="form-control" onchange="this.form.submit()" id="status" name="status">
<option disabled="disabled" selected="selected" hidden="hidden"><? echo $system_status_1; ?></option>
<!--Not related to your question, but switch the value attribute with the
option text so you can see your icons and get the value I assume you expect-->
<option value="Experiencing Errors"><i class="fa fa-exclamation-triangle" style="color:#F60"></i> Experiencing Errors</option>
...
</select>
<noscript><input type="submit" value="Submit"></noscript>
I would actually not recommend getting rid of the isset check altogether, but modifying it so that it actually verifies that the values you are going to use are present.
<?php
if (!empty($_POST['system_id']) && !empty($_POST['status'])) {
$system_id = $_POST['system_id'];
$status = $_POST['status'];
echo $system_id;
echo $status;
} else {
// error handling for missing values
}

How to change content in div, based on dropdown selection

I load my content from my DB with PHP, into my div.
I have a dropdown above it, where i should be able to sort out the content, meaning that when dropdown is chosen, the old content in the div is removed, and the new appears.
So how is it possible to "remove" the old content in the div, and get the new content based on the dropdown selection ?
This is what i got so far:
<div class="col-md-4">
<ul class="nav nav-tabs top_nav">
<li role="presentation" class="active">Edit</li>
<li role="presentation" class="inactive">
<!-- THE DROPDOWN THAT SHOULD SORT THE CONTENT BELOW -->
<!-- REMOVE OLD, AND GET NEW AFTER SELECTED -->
<select id="filter_type">
<option selected value="Alle">Alle</option>
<option value="Privat_Tale">Privat Tale</option>
<option value="Erhverv_Tale">Erhverv Tale</option>
<option value="Gamle">Gamle</option>
<option value="Privat_MBB">Privat MBB</option>
<option value="Erhverv_MBB">Erhverv MBB</option>
<option value="Familietele">Familietele</option>
<option value="Retention">Retention</option>
</select>
</li>
</ul>
<!-- LOAD THE CONTENT FROM DB, AND SHOW IT -->
<div class="abo_load" id="abo_load">
<?php
if (mysql_num_rows($sql_select_edit) > 0) {
while ($row = mysql_fetch_assoc($sql_select_edit)) {
?>
<div class="abo_box" id="abo_box">
<label class="abo_navn"><?php echo $row['abo_name']; ?></label>
<input type="hidden" value="<?php echo $row['category']; ?>" name="category" />
<form action="conn/delete.php" method="post">
<input type="hidden" value="<?php echo $row['id'];
?>" name="slet">
<input class="delete" value="Delete" type="submit" onclick="return confirm('Er du sikker??\nDet ville jo være ÆV med en Fejl 40!');">
</form>
<label class="edit">Edit</label>
<?php include("files/edit_list_content.php"); ?>
</div>
<?php
}
}
?>
</div>
</div>
EDIT:
I have already made it with JQuery, i load a php page, that gets the selected item, and then get the data from the DB. But the problem is, that when its loaded, i can't use any JQuery to manipulate with the new content. Like i have a edit label on the content with a JQuery hide function on. That function is working when i get the data though php from my DB, but not when i get the data though JQuery load. Thats why i want to load the data though php
You need to bind change event on your dropdown & based upon selection, fire an ajax request to get related content & then upon receiving update content inside DIV.
If you are using jQuery, Some useful functions would be:
Ajax: http://api.jquery.com/jquery.ajax/
Binding event: https://api.jquery.com/change/
A good example of implementation has been given here:
https://stackoverflow.com/a/4910803/2004910

Handling isset and select box in php html and for loop

Here is my code. This needs some optimization if you think the below code is bad and someone can really help me
I'm using two if condition and two else condition. Is there anyway this can be fine-tuned.
In the search box i need to retain the previous selected book name. i.e when the submit button is clicked the select box should display the result with the
selected item in the select box. for ex: if in dropdown if i select "book1" and click the submit button, the result should be displayed along with the "book1" as default inside the select box. in my case, the search box is getting reset on each submit click.
My Code:
<?php
if(isset($_POST['submit'])){
$get_project_id = $_POST['project_name'];
if($get_project_id == 1){
..display all the books(mysql query)..
}else{
..display the particular book(mysql query)..
}
}else{
..if the search button is not clicked then display all the books(mysql query)..
}
?>
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Any help will be appreciable.
Than,ks
Kimz
You can do it by checking the condition inside while loop. Checking the value of option and the post value are same or not as shown in the below code.
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option <?php if(isset($_POST['book_list']) && ($_POST['book_list'] == $row['Project_Auto_Id'])) { echo 'selected'; } ?> value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Not sure if there is any better way to do the two if... else statements but even if there is there's nothing wrong with the way you've done it, just a different way.
With regards to having the option selected after submitting you need to change your output of the options to this:
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>"<?php if(isset($_POST['book_list']) && $_POST['book_list'] == $row['Project_Auto_Id']) echo ' selected'; ?>>
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>

Reload the page/form for refreshing the data when clicking the button

How to reload the page or form in PHP.
The scenario: I have two forms with the same design/gui. I named it main.php and process.php.
Both page also have dropdownlist, same items. The items in the dropdownlist are "Net 1" and "Net 2" and under of "Net 1" is "Bldg 1" and for "Net 2" is "Bldg 2". So in the main.php when I click the submit it will proceed to the process.php. Then process.php will display the under item of the item I choose in the dropdownlist in main.php.
My problem is:
When the user want to see the under item of "Net 2". I want is, in the page of button_process.php will just reload or refresh the data so that the user will see the items for "Net 2", what should I do?
Code for main.php:
<form action="process.php" method="post">
Advertisement Name: <input type="text" name="adName" size="50"><br/><br/>
Duration: <select id="duration"name="duration">
<option value="5 s" >5 s</option>
<option value="10 s" >10 s</option>
<option value="15 s" >15 s</option>
<option value="20 s" >20 s</option>
<option value="30 s" >30 s</option>
<option value="60 s" >60 s</option>
</select><br/><br/>
<b>Period</b> <br/>
From: <input type="text" id="datepickerfrom"name="from"> To: <input type="text" id="datepickerto"name="to">
<span id="span_select">
<select name="id">
<option value="" >- select -</option>
<?php
include 'connect.php';
$q = mysql_query("select fldNetname from tblnetwork");
while ($row1 = mysql_fetch_array($q))
{
echo "<option value='".$row1[fldNetname]."'>".$row1[fldNetname]."</option>";
}
?>
</select>
</span>
<input type="submit" name="pass" value="View Building/s">
</form>
Here's my Code for
process.php:
EDIT:
<form action="button_process.php" method="post">
echo "<div><input type='checkbox' class='checkall'> Check all</div>";
$all = mysql_query("SELECT fldBldgName FROM tblbuildings");
while ($row = mysql_fetch_array($all))
{
echo "<div><input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName']."'>";
echo $row['fldBldgName'];"</div>";
}
<input type="submit" name="pass" value="View Building/s">
</form>
Code for button_process.php
<?php
include("connect.php");
switch ($_POST['pass']) {
case 'View':
//here I want to reload the page.
break;
case 'Save':
echo "Add";
break;
}
?>
If reloading the page requires a user action, you will not be able to use PHP to reload the page, in this case, you would have to use Javascript.
If you want to reload a page using PHP, which will happen as the page load and before any html code is outputted, you can use this :
header('Location: '.$_SERVER['REQUEST_URI']);
If you want to refresh the page after the user has executed an action, you will need a javascript event method on an element (for exemple, onclick() on a button), and within the method you can use the following code :
window.location.reload();
If this isn't sufficient to answer your question, please provide more details as to what you want to do. There is a better way to do it than what you are doing, with more details we might be able to help you more.
A quick and dirty example of setting expanding menus on different pages. Build the menu items first then use a php script to set the parent menu item to have a active css style and have your css styles give the proper hide/show styles.
HTML
<div class="menu">
<div class="menuitem <?php echo $Page=="Net 1" ? "active" : "";?>">
<span>Net 1</span>
<div class="submenu">
<div class="submenuitem">
<span>Bldg 1</span>
</div>
</div>
</div>
</div> class="menuitem <?php echo $Page=="Net 2" ? "active" : "";?>">
<span>Net 2</span>
<div class="submenu">
<div class="submenuitem">
<span>Bldg 2</span>
</div>
</div>
</div>
</div>
CSS
.menuitem .submenu {
display:none;
}
.menuitem.active .submenu {
display:block;
}
where $Page is some php variable that holds the name of your menu item or however you want to tell your script which menu item the user is on.

Categories