Page 1 of 1

A beginner with php needs help

Posted: Mon Oct 27, 2003 9:29 am
by vics
Hi, i am a beginner when it comes to php, I am having great difficulties doing one specific thing in php and that is.......accessing form variables and i cant do it!!!

I have two files, 1 HTML file and 1 php file, when i call up the HTML file it allows me to input information, and this information that i input should be put into an array and outputted on the processorder.php screen - however it doesnt - all i get is the headings on the page but no results - can anyone help me with this?????i am just finding it confusing as to why it doesnt work?

This is the HTML file

Code: Select all

<html>

<head>

</head>

<body>


<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>

</body>

</html>

This is the PHP file;

Code: Select all

<html>

<head>

<title> Bob's Auto Parts - Order Results</title>

</head>

<body>

<h1>Bob's Auto Parts</h1>

<h2>

Order Results

<?
	echo "<p>Order processed at ";//this displays the date
	echo date("H:i, jS F");
	echo "<br>";

	echo"<p>Your order is as follows:";
	echo"<br>";
	$HTTP_POST_VARS["tireqty"];
	$HTTP_POST_VARS["oilqty"];
	$HTTP_POST_VARS["sparkqty"];
		
?>

</h2>

</body>

</html>
[mod_edit:

Code: Select all

and

Code: Select all

tags added][/size]

Posted: Mon Oct 27, 2003 9:44 am
by Jean-Yves
You need to "echo" the POST variables too:

Code: Select all

echo $HTTP_POST_VARS["tireqty"];
echo $HTTP_POST_VARS["oilqty"];
echo $HTTP_POST_VARS["sparkqty"];
?>
Also, on your calling page, your html is incorrect in that you have a missing equal signs (=) after the "name" attribute and it's value in the oiltqty and spqrkty fields.

thanks!

Posted: Mon Oct 27, 2003 9:52 am
by vics
I have made the changes - and it works!thank you very much, i knew it would be something easy!!

i really appreciate your help!so thanks

Posted: Mon Oct 27, 2003 9:55 am
by Jean-Yves
Glad I could help :)

Posted: Mon Oct 27, 2003 11:53 am
by m3rajk
two pieces of advice:
1: go find any post by volka and read the link in his sig. (trust me, this is useful info to know that he has in that link)

2: why not use one self-processing php file so that you can extend upon the form to include user info that's needed for the sale and make sure you have everything? ie: instead of those two, use this...

note: i changed $HTTP_POST_VARS to $_POST because as of, i believe 4.1.0, the former is deprecated, and it may not be supported much longer. if i'm wrong, then i'm confusing this switch and the register globals one... one is 4.0.6 and the other 4.1.0

Code: Select all

<?php
/* put all include calls here; all pages that have the same begining should use a variable in an include file... easier future maintainence */

if(isset($_POST['verify'])){ # this is not the user entered something
	$startpg=pgstart($_POST['name']); # page with title customized to person's name
	$procdate=date("H:i, jS F"); # date of order processing
	$endpg=pgend(); # function to end the page giving all disclaimer, etc, and if you want give an argument and personalize it
	echo <<<END
$startpg
<h1>Bob's Auto Parts</h1> 

<h2> 

Order Results 

<form action="{$_SERVER['PHP_SELF']}" method="POST">
<p>Order processed at: $procdate
</p>
<p>Your order is as follows:
<br />Tires: <input type="text" name="tireqty" value="{$_POST['tireqty']}">
<br />Oil: <input type="text" name="oilqty" value="{$_POST['oilqty']}">
<br />Spark Plugs: <input type="text" name="sparkqty" value="{$_POST['sparkqty']}">
</p>
<p>Customer billing information (please verify)
<br /><!-- put your customer billing information here -->
</form>
</h2>
$endpg
END;
elseif(isset($_POST['complete'])){ // display when we're sure that we have what the customer wants, and the billing info is right
	$startpg=pgstart($_POST['name']); # page with title customized to person's name
	$procdate=date("H:i, jS F"); # date of order processing
	$endpg=pgend(); # function to end the page giving all disclaimer, etc, and if you want give an argument and personalize it
	echo <<<END
$startpg
<h1>Bob's Auto Parts</h1> 

<h2> 

Order Results 

<p>Order processed at: $procdate
</p>
<p>Your order is as follows:
<br />Tires: {$_POST['tireqty']}
<br />Oil: {$_POST['oilqty']}
<br />Spark Plugs: {$_POST['sparkqty']}
</p>
<p>Customer billing information (please verify)
<br /><!-- put your customer billing information here -->
</form>
</h2>
$endpg
END;
}else{ // first time calling the form
	$startpg=pgstart(new customer); # page with title customized to person's name
	$procdate=date("H:i, jS F"); # date of order processing
	$endpg=pgend(); # function to end the page giving all disclaimer, etc, and if you want give an argument and personalize it
	echo <<<END
$startpg
<form action="{$_SERVER['PHP_SELF']}" 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>
$endpg
END;
}
?>

i should explain some things:

1: there's no reason to ever leave php, so i didn't. 2: i use the "heredoc" syntax of echo (for more info, please see the source ofphp, php.net, and search for echo)

2: there's no begining and ending because i figure you will make that in an include file, and then convert the site to php since you can then maintain the entire ite easier (i know i would do that)

3: i do NOT use <br> since that's html and html itself is going to be deprecated soon for XHMTL. i use <br /> since it's xhtml
similarly, i closed off a bunch of open ended tags because even though html is very forgiving and doesn't care, xhtml is not and cares a lot

Posted: Mon Oct 27, 2003 6:16 pm
by JAM
Speaking of xhtml...

Code: Select all

<input type="text" name"oilqty" size=3 maxlength=3>
Why not use <input ... /> in those also in that case (among other tags)? And for compilance, you should add double quotes around the values at all times.

Posted: Tue Oct 28, 2003 3:13 am
by twigletmac
If you want to save yourself some typing you can use $_POST instead of $HTTP_POST_VARS.

Mac