Stoopid Newbie question about form variables

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!

Moderator: General Moderators

Post Reply
mick_otoole
Forum Newbie
Posts: 4
Joined: Thu Aug 03, 2006 6:38 am

Stoopid Newbie question about form variables

Post by mick_otoole »

feyd | Please use

Code: Select all

,

Code: Select all

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]

Code: Select all

<html>
<head>
<title>Bob’s Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob’s Auto Parts</h1>
<h2>Order Results</h2>
<?php
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>";
echo $oilqty." bottles of oil<br>";
echo $sparkqty." spark plugs<br>";
?>
</body>
</html>
THe values that I type in do not show up. The only thing that appears on the page is the descriptions.

I know that there is probably a very very simple solution for this I just can't seem to find it!

Oh yeah and before you ask I have installed Apache and PHP on my local machine!!!


feyd | Please use

Code: Select all

,

Code: Select all

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]
ScottRiley
Forum Newbie
Posts: 6
Joined: Tue Aug 08, 2006 7:44 am

Post by ScottRiley »

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:

Code: Select all

<?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.
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

how can you send these variable to another page?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

get, post, sessions, curl.. to name a few.
ScottRiley
Forum Newbie
Posts: 6
Joined: Tue Aug 08, 2006 7:44 am

Post by ScottRiley »

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:

Code: Select all

print($_POST['sparkqty']);//outputs the 'sparkqty' index of the post array
or, if you will be using it more often, its better to store it as a variable:

Code: Select all

$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
Post Reply