Jump to content

Where do i put php code when making a form


Jpower24

Recommended Posts

Its for a web programming class, I have to design an index page that displays the products. I'm done that which was simple enough.

Now I have to make an order form.

Build another XHTML page and using PHP, determine the following things:

 

a) That the visitor did indeed select something to purchase.

b) Calculate the price of the purchase (factoring in PST and GST).

c) Output a friendly message thanking them for their purchase. Include a full description of what they bought, the price and when they can expect their order.

d) Valid XHTML for your order form and the thank you message.

 

I really need some help here

 

Ill put up the xhtml code first then the php. I dont know how to work them into eachother. I dont know how I would echo "you bought xxinches waterpipe" when that part is in the order form.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="styles.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<h1>Order Form</h1>


<form action="orderform.php" method="post">



<h2>Color:</h2>

<select name="color">
<option value="Blue Transparent ">Blue Transparent </option>
<option value="Green Transparent ">Green Transparent )</option>
<option value="Yellow Transparent ">Yellow Transparent </option>
<option value="Red Transparent ">Red Transparent </option>
</select>

<h2>Size</h2>

<select name="Size">
<option value=" 8 inches ">8 inches </option>
<option value="12 inches ">12 inches</option>
<option value="24 inches ">24 inches</option>
<option value="36 inches ">36 inches</option>
</select>




<h2>Quantity</h2><input type="text" name="Quantity" value="1">
<h2>Name</h2><input type="text" name="Name" value="">
<h2>Address</h2><input type="text" name="Address" value="">
<h2>City</h2><input type="text" name="City" value="">
<h2>Province</h2><input type="text" name="Province" value="">

<label for="cost"><h2>Cost: $</h2></label>
<input type="text" name="cost" id="cost" value="Total before taxes/shipping" />
<input type="submit" value="Calculate GST" />

<input type="hidden" name="recipient" value="JPOWER24@hotmail.com">
<input type="hidden" name="redirect" value="">
<input type="hidden" name="required" value="">
<input type="hidden" name="env_report" value="REMOTE_HOST, HTTP_USER_AGENT">
</form>

 

the php code

<?php
if(isset($_POST['cost']) && ($_POST['cost'] != 0)){
$cost = $_POST['cost'];
$gst = $cost*0.06;
$pst = $cost*0.08;
$ship = 10;
$cost += $gst + $pst +$ship;
echo "<h1>Total Cost: $".$cost."</h1>";
};
?> 

 

Link to comment
Share on other sites

Depending on what you want to output, because you are using the POST Method in your form, you would use $_POST['variable]', where variable is what you have defined in your form, such as <select name="Size">

 

So, in your example, if say, for example, a customer selects 12 inches of pipe, in PHP you would type:

 

<?php
print "You have purchased, " . $_POST['Size'] . " of pipe.";
?>

Link to comment
Share on other sites

so when I put Size in the form here

<select name="Size">

php recognizes those values. more or less meaning if I did

 

echo ' you purchased .$size pipe ' it would come out

you purchased 12 inches pipe

 

while if I changed

<option value="12 inches ">12 inches</option>

to

<option value="12">12 inches</option>

 

I could rewrite the above as

echo ' you purchased .$size inches pipe '

Link to comment
Share on other sites

Yes that is correct, however i noticed something in your last post

 

echo ' you purchased .$size inches pipe ' 

 

if $size is defined as $size = $_POST['Size']; then this will work, if not, you would use

 

echo ' you purchased ' . $_POST['Size'] . ' inches pipe ' 

Link to comment
Share on other sites

Perfect so then in adding up the total cost I could go $ship= $size*0.2

 

$ship representing shipping and $size*0.2 is just saying that shipping will be 20% of length, inches to dollars.

 

<?php
if(isset($_POST['cost']) && ($_POST['cost'] != 0)){
$cost = $_POST['cost'];
$size = $_POST['Size']; 
$gst = $cost*0.06;
$pst = $cost*0.08;
$ship = $size*0.2
$cost += $gst + $pst +$ship;
echo "<h3>You've purchased .$size inches of pipe at a total cost of $.$cost , thanks for choosing....</h3>";
};
?> 

 

assuiming that is right just one more thing has me puzzled.

 

the <form action ="____" > I believe thats where you tell the form where to go. So do i tell it to go where the php file is such as    form action="orderform.php       or do i just put the php code in the web page itself and tell the form to go somewhere else?

Link to comment
Share on other sites

yea the cost could actually be replaced with total it might make it easier to understand for you. But I'm pertty sure i got this right.

It just gets the total cost, which is, the amount they enter  is inputted into the php code, and is then times by .06 .08 (tax) and then .2 (shipping).

then when i hit the get total button I want it to give that value

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.