PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm trying to teach myself PHP and I've been working form a numer of different books. THe problem I'm having is when I pass form variables from one page to another they do not appear.
A very simple example is when I try to pass values from this page (orderform.html)
[syntax="html"]<HTML>
<form action="processorder.php" method=post>
<table border=0>
<tr bgcolor=#cccccc>
<td width=150>Item</td>
<td width=15>Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align=center><input type=”text” name=”tireqty” size=3 maxlength=3></td>
</tr>
<tr>
<td>Oil</td>
<td align=center><input type=”text” name=”oilqty” size=3 maxlength=3></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align=center><input type=”text” name=”sparkqty” size=3 maxlength=3></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value=”Submit Order”></td>
</tr>
</table>
</form>
</HTML>
i.e. The Values tireqty, oilqty and sparkqty to the following page (processorder.php)[/syntax]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
You have to use$_POST or $_GET. In your example, you'v used the form method "post" so to get the entered information into the php file, you need to use $_POST['name'}, where name is the 'tireqty' in name="tireqty" in your html form.
So, in your PHP you'll need to assign these $_POST['names'] to variables. So your code should look something like:
<?php
$tireqty=$_POST['tireqty']; //THESE VARIABLES WILL BE USED......
$oilqty=$_POST['oilqty'];
$sparkqty=$_POST['sparkqty'];
echo ("Order Processed on :");
echo date(" jS F, H:i");
echo "<br>";
echo "<p>Your order is as follows:";
echo "<br>";
echo $tireqty." tires<br>"; //........HERE
echo $oilqty." bottles of oil<br>";
echo $sparkqty." spark plugs<br>";
?>
You are getting the information from the form using the $_POST command, they are not instantly registered as variables from the form, you must either assign the $_POST values to variables as I have done above, or replace every instance of the variables you used with $_POST['variablename'] - where variable name is the name of the object within the HTML form.
The "post" found after the 'method' attribute in <form method="post" part of your HTML tells you you will be sending it via post. the "processorder.php" in 'action="processorder.php"' tells you the file that the information will be sent to and processed in. To retrieve these variables, you use the $_POST['name'] code, where name corresponds to the 'name' attribute in <type="text" name="sparkqty" tag. Here I have used your 'sparkqty' variable as an example. Now, this would send all the information to a php file, in an array called $_POST, with indexes of all the fields of the form the user fills in. So in your case the $_POST array would include the indexes sparkqty, oilqty and tireqty. You use the $_POST['index'] to interpret the information sent. Example, if you wanted to output the sparkqty variable, you could either output the sparkqty index of the $_POST array, which would look like:
$sparkqty = $_POST['sparkqty']; //stores the 'sparkqty' index of the post array as $sparkqty
print($sparkqty); //outputs the value of $sparkqty which is the 'sparkqty' index of the post array