I am making a store for a school project and I am having a hard time making text of the item name and price be saved in a variable then shown on the checkout page.
My code for the item description looks like this:
<div id="section">
<img src="http://ecx.images-amazon.com/images/I/71nodfoCvyL._SL1500_.jpg" alt="HTML tutorial" style="width:152px;height:172px;border:0;" align="right">
<?php $item->name='ASUS M32CD Desktop'; $item->price='549.99'; $i++; ?>
<h4>ASUS M32CD Desktop</h4>
<h6> $549.99 </h6>
<br>
<input type="checkbox" name="item<?php echo $i; ?>" value="<?php echo $item->name.'#'.$item->price; ?>">
Buy
When I select it and press purchase, it takes me to the checkout page but doesn't have the items listed. My checkout page looks like this:
<!DOCTYPE html>
<!-- HardDrive1.html -->
<!-- Jan 22, 2016 -->
<html>
<head>
<title>Order Form</title>
</head>
<body>
<h3>Please Input All Information to Complete Your Order</h3>
<?php echo $item->name; ?>
<?php echo $item->price; ?>
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value=""><br>
Last name:<br>
<input type="text" name="lastname" value=""><br>
Street Address:<br>
<input type="text" name="address" value=""><br>
City:<br>
<input type="text" name="city" value=""><br>
You can avhieve what you want, catch a shopping basket in three ways:
1) Server side, on a SESSION, so once the user selects an item it must be stored in a SESSION, with its quantity. I don't recommend to store prices on it, they shall be fetched from a data source in order to prevent user manipulation of forms.
$_SESSION['cart']['item']=$_POST['item_name']
$_SESSION['cart']['item_quantity']=$_POST['item_quantity']
2). Client side, on local Storage API, using Javascript.
localStorage.setItem("item", "ASUS M32CD DESKTOP");
3) Less recommended to store on a cookie, is a file created by the server and deployed on user's hard disk and accessed only by your server.
Related
so I have 2 pages . On the first page I have a form , where I need to input values and the other page receives those values and saves them. Then I go back to page 1 , input new values and those new values get saved again on page 2 , right next to the previous values. Basically like a shopping list. The problem is that i'm really new to PHP and I can't really find solution how to save these forms next to each other. Massive thanks to anyone who replies.
Page 1 :
<fieldset style="width:fit-content; margin:170px; auto 10px; font-size: 30px; justify-content:center;">
<form action="./site2.php" method="post" >
<legend>Product add</legend>
<label>SKU:<br />
<input type="text" name="SKU" pattern="[0-9.]+" required id="SKU" /></label><br />
<label>Name:<br />
<input type="text" name="name" required id="name" /> </label><br>
<label>Price($):<br />
<input required id="price" pattern="[0-9.]+" type="text" name="price" ></label><br />
<input type="submit" name="Submit" value="Save" >
<input type="reset" value="Cancel">
Page 2 :
<form action="site1.php" method="get">
<?php
session_start();
$data=array();
if(!isset($_SESSION['data'])){
$_SESSION['data'] = array();
}
if (isset($_POST)) {
$_SESSION['data'][] = $_POST['SKU'];
$_SESSION['data'][] = $_POST['name'];
$_SESSION['data'][] = $_POST['price'];
}
foreach($_SESSION['data'] as $d){
}
?>
<form id="form-list" >
<fieldset style="width: fit-content; margin:130px; auto 10px; font-size: 20px; justify-content:center; " >
SKU: <?php echo $d; ?><br>
Product name: <?php echo $d; ?><br>
Price($): <?php echo $d; ?><br>
<input type="checkbox" value="asd" id="test" name="test" />
</form>
It looks like you have read a little, and are trying to build something based on that reading. Good start. but I think you need to take a further dive into the litterature.
Concepts
PHP is a server side language. everything inside the <?php and ?> tags is executed on the server, and not on the client.
HTML is a (most often) client side markup (display) language, and is executed on the client (in the browser)
When you have both php and html in a php file (and it is sent with the proper headers) the php code is executed, and any echo or print or <?="hello world"?> is converted to text on your html page, and the resulting file is executed by the client's browser
Anything in your $_SESSION is available to your PHP sections as they are executed, providing the session (set by the cookie or a path parameter) is the same
What your code is doing
Page 1 (site1.php)
Displays a html form (could in reality be just a .html file instead, if you have no other php in the file)
Page 2 (site2.php)
starts a <form> element which would submit the form to site1.php
parses the submitted form data and saves it in a session variable
parses an empty foreach loop
displays some html elements with an apparently unset php variable $d
creates a checkbox
closes the <form> without a sumbit button, which means you never get back to site1.php
Solution 1: Do everything in a single php file
<!-- input part -->
<html>
<body>
<fieldset style="width:fit-content; margin:170px; auto 10px; font-size: 30px; justify-content:center;">
<form action="" method="post" >
<legend>Product add</legend>
<label>SKU:<br />
<input type="text" name="SKU" pattern="[0-9.]+" required id="SKU" /></label><br />
<label>Name:<br />
<input type="text" name="name" required id="name" /> </label><br>
<label>Price($):<br />
<input required id="price" pattern="[0-9.]+" type="text" name="price" ></label><br />
<input type="submit" name="Submit" value="Save" >
<input type="reset" value="Cancel">
<!-- form input handling -->
<?php
session_start();
$data=array();
if(!isset($_SESSION['data'])){
$_SESSION['data'] = array();
}
if (isset($_POST)) {
$_SESSION['data'][] = $_POST['SKU'];
$_SESSION['data'][] = $_POST['name'];
$_SESSION['data'][] = $_POST['price'];
}
foreach($_SESSION['data'] as $d){
echo "SKU: ".$d['SKU']."<br>".
"Product name: ".$d['name']."<br>".
"Price($): ".$d['price']."<br>";
}
?>
<!-- some link to save.php where you push $_SESSION['data'] to your persistent databse (MySQL/PostgresQL/whatever)-->
</body>
Solution 2 (advanced):
keep your setup with site1.php and site2.php, but keep site2.php as pure php without html, and return the data as json or xml
use ajax (javascript) to submit the form to site2.php and parse the return data to a visible format
have a third file (e.g. save.php) where you do the saving to a dbms
Solution 3: keep your code and fix the issues
put your display code inside the foreach (see my code)
remove the <form> from site2.php and simply add a add more items link at the bottom to go back to the input page
So a couple of things here, if you want to save your values on another page, you're going to need some sort of intermediary to save your values. This is where databases / caches shine.
Just use your preferred storage utility, and call it in your shopping cart with a SELECT / foreach loop.
I have my form done but I am trying to create a handling form which calculates the total plus shipping of $5 and 8% tax rate and displays it.
Need a handling form for my following PHP code. The handling form should calculate total price, 8%tax, $5 shipping fee. It should say thank you for ordering (name entered in form) on (date entered in form)
Need a handling form for my following PHP code. The handling form should calculate total price, 8%tax, $5 shipping fee. It should say thank you for ordering (name entered in form) on (date entered in form)
<html
<head>
</head>
<body>
<link rel= "stylesheet" href= "order.css">
<form action="complete.php" method="post">
<form name="order">
<fieldset><legend>Complete Order:</legend>
<h1> Choose Design </h1>
<p><label>Your Name: <input type="text" name="name"></label>
<label>Address: <input type="text" name="address"></label>
<label>Credit Card #: <input type="text" name="creditcard"></label>
<label>Date: <input type="date" id="datepicker" name='date' size='9' value="" > </label>
<br><label> Design Types: <img src="1.jpg"><input type="checkbox" name="1"></label> $1.00
<label><img src="2.jpg"><input type="checkbox" name="2"> </label>$1.00
<label><img src="3.jpg"><input type="checkbox" name="3"> </label>$1.00
<br></p>
<input type="submit" value="Submit Order">
</form>
</body>
</html>
This is the handler, but before I start on the answer. I just wish to make it clear that Stack Overflow is a Q&A environment and not a place for someone to do your work. That being said, I have answered this question...
Place this code at the top of the HTML page that I will post (I have made some amendments).
Note the $date variable uses the ternary operator to ensure a date will get posted to the database. I have formatted the date in the yyyy-mm-dd format which is the format of MySQL databases (I'm not sure about the format the others use, but I imagine it is the same).
<?php
// only run if form is posted
if (isset($_POST["submit"]))
{
// set POST to variables (easy to type variable name)
$name = $_POST["name"];
$address = $_POST["address"];
$creditcard = $_POST["creditcard"];
$date = $_POST["date"] == "" ? date("Y-m-d") : $_POST["date"];
$design = $_POST["designType"];
$total = $design; // add the cost of design
$total += $total * 1.08; // work out tax
$total += 5; // the shipping fee
}
?>
I have made a few changes to the form that you posted. Here they are:
I indented the tags (it's a personal preference but I think it makes it look neater)
Added </fieldset> to the end of the form. This ensures it passes W3C Validator which requires closing of tags
I changed the checkboxes to radio types. This was an assumption based on how I thought you form was supposed to be (I wasn't sure where total was coming from otherwise; so I assumed the "Design type" was the price)
I reformatted the <label> and <input> so that they weren't nested (with the exception of radio elements, but that is so you can click the label to select the radio)
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<link rel="stylesheet" href="order.css" />
</head>
<body>
<?php if (isset($_POST["submit"])) { ?><div id="message">Thank you, <?php echo $name; ?> for ordering on <?php echo $date; ?>. The total was <?php echo "$" . number_format($total, 2); ?> </div><?php } ?>
<form name="order" action="complete.php" method="post">
<fieldset>
<legend>Complete Order:</legend>
<h1>Choose Design</h1>
<label>Your Name:</label><input type="text" name="name" />
<label>Address:</label><input type="text" name="address" />
<label>Credit Card #:</label><input type="text" name="creditcard" />
<label>Date:</label><input type="date" id="datepicker" name='date' size='9' value="" />
<br />
<label>Design Types:</label>
<label><img src="1.jpg" alt="$1.00" /><input type="radio" name="designType" value="1" />$1.00</label>
<label><img src="2.jpg" alt="$2.00" /><input type="radio" name="designType" value="2" />$2.00</label>
<label><img src="3.jpg" alt="$3.00" /><input type="radio" name="designType" value="3" />$3.00</label>
<br />
<input type="submit" name="submit" value="Submit Order" />
</fieldset>
</form>
</body>
</html>
That is all.
Again, let me reinforce the idea that Stack Overflow is not here for this type of "question". Please also provide an attempt at your problem as well.
This is probably a really stupid question, but I can't figure out why my code is doing this. I'm trying to do a simple recordset update and yet when I hit the submit button on the page, it will reload the page and in the url have multiple url parameters of the info from the form.
I have two forms in the whole document, one, the one I'm working with is supposed to change a variable in the database to the username of the user
<form action="<?php echo $editFormAction; ?>" method="POST" name="Catch">
<input name="Horseid" type="hidden" value="<?php echo $colname_WildHorse?>"/>
<input name="Owner" type="hidden" value="<?php echo $colname_HorseImage?>"/>
<input name="Catch" type="submit" value="Catch"/>
<input type="hidden" name="MM_update" value="Catch"/>
</form>
This is the one that returns all the url stuff after I hit the submit button. The second form is under it, and I want that one to display in the Url, which it does perfectly fine. Why is this one freaking out though?
URL produced:
http://localhost:8888/TheMeadow.php?Horseid=38&Owner=redlilac78&Catch=Catch&MM_update=Catch
This is the result code: I left out the style sheet since it's a lot
<title>The Meadow</title>
<style type="text/css">
//Left out style
</style>
</head>
<body>
<div id="Background">
<div id="Logo"><img src="Images/RR_Art/Website pages/Logo/Ropin' Ranch logo.gif" width="229" height="192" /></div>
<div id="Map">Map</div>
<div id="Directories">Directories</div>
<div id="LogOut">Log out</div>
<div id="Note">
<form action="" method="get">
<p>
Welcome to the wild, where you can catch and search for wild horses! You can only catch one horse every two days so choose your horse wisely! </p>
<div id="Name"><br> <br> <br> <br> <br>
<form name = "WildHorse" id = "WildHorse" onSubmit="TheMeadow.php?HorseId43">
<input type = "hidden" name = "HorseId" value = "43"/>
<input type = "submit" name = "" value = "Search" />
</form>
</div>
</div>
</body>
In your form action the problem is with $editFormAction in php , it is either empty or having the same files name due to which the form is being submitted to the same page.
I'm really new to PHP but it's awesome language :)
I created simple mathematical calculation script that takes inputs from forms in terms of variables and perform simple calculations. I would like a result to be displayed in another form instead of simply being displayed at the bottom of the page.
<html>
<body>
<p>Simple PHP script for cloning calculations</p>
<form name="form1" method="post" action="">
<p>
<label for="vector_lenght">La taille du vecteur (kb)</label>
<input type="text" name="vector_lenght" id="vector_lenght">
</p>
<p>
<label for="insert_lenght">La taille de l'insert (kb)</label>
<input type="text" name="insert_lenght" id="insert_lenght">
</p>
<p>
<label for="conc_insert">La concentration de l'insert</label>
<input type="text" name="conc_insert" id="conc_insert">
</p>
<p>
<input type="submit" name="ratio_calc" id="ratio_calc" value="Calculer">
</p>
<p>
<label for="ratio">Le ratio à utiliser</label>
<input type="text" name="ratio" id="ratio">
</p>
<p>
<label for="ratio_1">ratio 1:1</label>
<input type="text" name="ratio_1" id="ratio_1">
<br>
<label for="ratio_3">ratio 1:3</label>
<input type="text" name="ratio_3" id="ratio_3">
<br>
<label for="ratio_5">ratio 1:5</label>
<input type="text" name="ratio_5" id="ratio_5">
</p>
</form>
<?php
if($_POST['ratio_calc']!=''){
define("conc_vector", 100);
$vector_lenght = $_POST['vector_lenght'];
$insert_lenght = $_POST['insert_lenght'];
$conc_insert = $_POST['conc_insert'];
$result = (conc_vector * $insert_lenght) / $vector_lenght;
echo "<h1>$result</h1>";
}
?>
</body>
</html>
In your opening form tag, the 'action' attribute specifies what file should receive the HTTP request created when you submit the form. As it is now the value is an empty string, and the script is defaulting to POSTing the data back to the current script.
(I assume this other form is in a different page - if not, skip to the last code snippet)
So, create another PHP script that represents the page you want to contain your results, and put the name of the new script as the value of the 'action' attribute. In the new script, you can retrieve the values sent by your form via the $_POST[] superglobal (in the same way that you already are in the code you posted).
If the new page is named resultPage.php (and is in the same directory as the script you posted), your form tag should look like this:
<form name="form1" method="post" action="resultPage.php">
If you want the data to display inside a form in the new page, do something like this:
<input type="text" value="<?php echo $_POST['conc_insert'] ?>">
Weird question this, going round in circles.
I have 2 pages.
Page 1. Has a button on it. Like
<form action="goto_page_2.php"><p class="longdesc"><span class="spanBold">Scrap Collections in North Lakes:</span><br />
<button class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
</p></form>
The above is a simple mockup. But what I want to do is, onclick of the button, parse the Value to ...
Page 2. Which has a form built in.
One of the fields is Suburb.
<!-- EMAIL SUBURB -->
<span class="commonControlLabel">Suburb:</span>
<span class="commonControlLabelItalic">(required)</span>
<span id="contactSuburbErrorMsg" class="commonControlErrorMsg"></span><br />
<input class="commonInput" type="text" id="inputSuburb" value=""/><br />
So what I want to do, is grab the VALUE of the button on PAGE 1, and add it to the Value in the input element on Page 2.
To complicate matters, Page 1. Has quite a few buttons, with different values, all unique, which we would like to pass, to the input element. Obviously onlcick of the button on page 1 we go direct to page 2.
Have the button post it's value to Page2:
Page1, I added type="submit" and name="suburb"
<button type="submit" name="suburb" class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
Page2: I added the php part in the value attribute
<input class="commonInput" type="text" id="inputSuburb" value="<?= $_POST['suburb'] ?>"/>
<!--page1::-->
<html>
<body>
<form action="test2.php" method="post">
<button name="desc" value="1">description!</button>
</form>
</body>
</html>
<!--page2::-->
<?php
echo "hello";
echo $_POST["desc"];
?>
There are several things you can do.
E.g: on form 2 say:
<input type="text" value="<?php if(isset($_POST['inputNameForm1'])){echo htmlentities($_POST['inputNameForm1']);} ?>
Or if that is not an option for you, try something like:
<? php
session_start();
$_SESSION['sessionName'] = $_POST['inputNameForm1']?>
<input type="text" value="<?php if(isset($_SESSION['sessionName']])){echo htmlentities($_SESSION['sessionName']]);} ?> />
Note: didn't test the code