Jump to content
xisto Community
Sign in to follow this  
ginginca

Got A Wee Error - Can You Help?

Recommended Posts

I'm taking a PHP course at college and am working on an assignment. There's no database involved.

I've declared my variables from a submit form on another page, and on the page I'm working on I'm trying to bring them up in a table. But my start-table command is outputting an error. Here's the code relating to where the error is:

<?php//Get First Item	if (isset($_GET['item_1_product'])) {				$item_1_product = $_GET['item_1_product'];	} else {		$item_1_product = ""; //Default value if data missing	}		if (isset($_GET['item_1_price'])) {				$item_1_price = $_GET['item_1_price'];	} else {		$item_1_price = ""; //Default value if data missing	}		if (isset($_GET['item_1_purchased'])) {				$item_1_purchased = $_GET['item_1_purchased'];	} else {		$item_1_purchased = ""; //Default value if data missing	}		if (isset($_GET['item_1_size'])) {				$item_1_size = $_GET['item_1_size'];	} else {		$item_1_size = ""; //Default value if data missing	}		if (isset($_GET['item_1_qty'])) {				$item_1_qty = $_GET['item_1_qty'];	} else {		$item_1_qty = ""; //Default value if data missing	}		//Get Second Item	if (isset($_GET['item_2_product'])) {				$item_2_product = $_GET['item_2_product'];	} else {		$item_2_product = ""; //Default value if data missing	}		if (isset($_GET['item_2_price'])) {				$item_2_price = $_GET['item_2_price'];	} else {		$item_2_price = ""; //Default value if data missing	}		if (isset($_GET['item_2_purchased'])) {				$item_2_purchased = $_GET['item_2_purchased'];	} else {		$item_2_purchased = ""; //Default value if data missing	}		if (isset($_GET['item_2_size'])) {				$item_2_size = $_GET['item_2_size'];	} else {		$item_2_size = ""; //Default value if data missing	}		if (isset($_GET['item_2_qty'])) {				$item_2_qty = $_GET['item_2_qty'];	} else {		$item_2_qty = ""; //Default value if data missing	}		//Get Third Item	if (isset($_GET['item_3_product'])) {				$item_3_product = $_GET['item_3_product'];	} else {		$item_3_product = ""; //Default value if data missing	}		if (isset($_GET['item_3_price'])) {				$item_3_price = $_GET['item_3_price'];	} else {		$item_3_price = ""; //Default value if data missing	}		if (isset($_GET['item_3_purchased'])) {				$item_3_purchased = $_GET['item_3_purchased'];	} else {		$item_3_purchased = ""; //Default value if data missing	}		if (isset($_GET['item_3_qty'])) {				$item_3_qty = $_GET['item_3_qty'];	} else {		$item_3_qty = ""; //Default value if data missing	}				?><html><head><meta http-equiv="Content-Language" content="en" /><meta name="GENERATOR" content="Zend Studio" /><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Process Order</title></head><body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">			<table align ="center" cellpadding="1" cellspacing="1" width="550" border="0">	<tr>		<td colspan="5" height="50" align="center" valign="middle">		<font size="4" face="Arial, Helvetica, sans-serif"><B>Products Ordered</B></font><BR>				</td>	</tr>	<tr bgcolor="#CCCCCC">				<td align="center" valign="middle" width="200"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Product Name</B></font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Price</B></font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Size</B><BR>(S, M, L or XL)</font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Qty</B></font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Subtotal</B></font></td>	</tr>	<tr bgcolor="#EEEEEE">		<td align="center" valign="middle" width="200"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_1_product; ?></font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_1_price; ?></font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_1_size; ?></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_1_qty; ?></td>				<td></td>	</tr>	<tr>		<td align="center" valign="middle" width="200"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_2_product; ?></font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_2_price; ?></font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif">N/A</td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_2_qty; ?></td>				<td></td>	</tr>	<tr bgcolor="#EEEEEE">		<td align="center" valign="middle" width="200"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_3_product; ?></font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_3_price; ?></font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_3_size; ?></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif"><?php print $item_3_qty; ?></td>				<td></td>			</tr>	</table>				</body></html>


(1) I'm getting no values coming up on this page when I submit.

(2) Am I doing this correctly where I start and stop php continually down the page?

Thanks,

Gin
Edited by ginginca (see edit history)

Share this post


Link to post
Share on other sites

The way you are using the php start/end tags is okay as far as I see. No problem there.
I am suspicious that the Form on the other page is faulty, or the variable names are messed up?

As a test, add and echo command into your code like so:

echo 'start';echo "$_GET['item_1_product']";echo 'end';//Get First Item	if (isset($_GET['item_1_product'])) {				$item_1_product = $_GET['item_1_product'];	} else {

This will tell you if the variables are being passed as you expect them to be passed.
Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

Yeah, so no major problems with the code provided!If you manually input variables in the url, everything works fine. :Dginginca.php?item_1_product=Baseball%20Cap&item_1_price=$1.00&item_1_size=XL&item_1_qty=200This url correctly displayed "Name", "Price", "Size", and "Quantity" for item one in the table. Try a manual url entry to see for yourself!The real issue must be in the form that submits the data. I'm guessing that your form uses the "POST" method and your script uses the "GET" method! Can't be sure until I see the submisstion form.Now on to the basics. :)In your table HTML, you never close many of your <font> tags. I'm guessing that that will be considered by your professor.By the way, not sure what your original assignment was but it seems like a lot of work to set all of your variables this way. Have you considered an array and a loop? Your PHP code would be about 10 lines long instead of 90. Just a thought.vujsa

Share this post


Link to post
Share on other sites

Ah ... and you come to my rescue once again!

Here's the code for the entry page.

I'll post them online and try them there in case there's an issue with my PHP running on the PC here.

<?php/** * Student Assignment - Chapter 4 * Order Form *  */?><html><head><meta http-equiv="Content-Language" content="en" /><meta name="GENERATOR" content="Zend Studio" /><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Order Form</title></head><body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99"><form id="MyOrder" action="process_order.php" method="POST"><table align ="center" cellpadding="1" cellspacing="1" width="550" border="0">	<tr>		<td colspan="5" height="50" align="center" valign="middle">		<font size="4" face="Arial, Helvetica, sans-serif"><B>Order Form</B></font><BR>		<font size="2" color="Red" face="Arial, Helvetica, sans-serif">Check off the products you wish to purchase.</font>		</td>	</tr>	<tr bgcolor="#CCCCCC">		<td align="center" valign="middle" width="50"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Select Items</B></font></td>		<td align="center" valign="middle" width="200"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Product Name</B></font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Price</B></font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Size</B><BR>(S, M, L or XL)</font></td>		<td align="center" valign="middle" width="100"><font size="2" color="Blue" face="Arial, Helvetica, sans-serif"><B>Qty</B></font></td>	</tr>	<tr bgcolor="#EEEEEE">		<input type="hidden" name="item_1_product" value="Zend Studio T-Shirt"/> 		<input type="hidden" name="item_1_price" value="14.45"/>  		<td align="center" valign="middle" width="50"><input type="checkbox" name="item_1_purchased" value="YES"/></td>		<td align="center" valign="middle" width="200"><font size="2" face="Arial, Helvetica, sans-serif">Zend Studio T-Shirt</font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif">$14.45</font></td>		<td align="center" valign="middle" width="100"><input type="text" name="item_1_size" size="5" value=""/></td>		<td align="center" valign="middle" width="100"><input type="text" name="item_1_qty" size="5" value=""/></td>			</tr>	<tr>		<input type="hidden" name="item_2_product" value="Zend Studio Jacket"/>  		<input type="hidden" name="item_2_price" value="125.75"/>		<td align="center" valign="middle" width="50"><input type="checkbox" name="item_2_purchased" value="YES"/></td>		<td align="center" valign="middle" width="200"><font size="2" face="Arial, Helvetica, sans-serif">Zend Studio Jacket</font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif">$125.75</font></td>		<td align="center" valign="middle" width="100"><input type="text" name="item_2_size" size="5" value=""/></td>		<td align="center" valign="middle" width="100"><input type="text" name="item_2_qty" size="5" value=""/></td>			</tr>	<tr bgcolor="#EEEEEE">		<input type="hidden" name="item_3_product" value="Zend Studio Mug"/>  		<input type="hidden" name="item_3_price" value="7.95"/>		<td align="center" valign="middle" width="50"><input type="checkbox" name="item_3_purchased" value="YES"/></td>		<td align="center" valign="middle" width="200"><font size="2" face="Arial, Helvetica, sans-serif">Zend Studio Mug</font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif">$7.95</font></td>		<td align="center" valign="middle" width="100"><font size="2" face="Arial, Helvetica, sans-serif">N/A</font></td>		<td align="center" valign="middle" width="100"><input type="text" name="item_3_qty" size="5" value=""/></td>			</tr>	<tr>		<td colspan="5" height="40" align="center" valign="middle">		<input type="submit" name="submit" value="Order Now!"/>  		</td>	</tr></table></form></body></html>

Share this post


Link to post
Share on other sites

http://forums.xisto.com/no_longer_exists/

 

Same thing online as it was doing on my PC.

 

Yeah, exactly as I suspected.. :)

 

In your form find on line #9:

<form id="MyOrder" action="process_order.php" method="POST">

 

If you use the "POST" method in your form, then to be able to used the passed variables in your "process_order.php" script, you have to use the "POST" method there as well!

 

So you need to change ALL of your $_GET in your script to $_POST!

 

OR

 

Change your form from "POST" to "GET"!

 

vujsa

Share this post


Link to post
Share on other sites

Of course you are absolutely correct and thank you. It works great now.I have to learn a couple of concepts here.(1) If I want to format my number output and don't have a programming background. Would I be looking to use the printf command? Or do I make my variable a float/double?(2) I need to also learn to do some error-checking, using if statements.I have a form that puts data into a database, but only want the submission to be processed if the 1st three digits of the phone number match from a list of two. The list will never change, so it doesn't have to be part of the database. (I'm going to put it into my form.) I plan on using a switch statement.What I don't quite get, is WHERE in my form the if statement goes.Thanks,Gin

Share this post


Link to post
Share on other sites

Well, for your phone number issue, here is what I would do. Since this will be a true/false situation, and since false will terminate the process, you should put the check at the beginning of the script like so:

<?php if (isset($_POST['phone'])){	$phone_prefix = $substr($_POST['phone'], 0, 3);	if($phone_prefix == 123 || $phone_prefix == 321){		echo "Good Number!";	}	else{		exit("Error! - Bad Phnoe Number!");	}}else{	exit("Error! - No Phone Number Entered!");}?>

Now what that does is check to see if the phone number starts with 321 or 123. If it doesn't start with 321 or 123 or if it isn't set, an error message is returned and the script ends

I'm not sure what it is that you need for your number format question.
If you give me an example of a number that you start with and also provide me with an example of the returned number, then I can tell you how to do it probably.

vujsa

Share this post


Link to post
Share on other sites

On my entry form regarding the phone number we were talking about, I am asking them only for the area code. I want to make sure that submissions are only from area code 519 and 226.

 

My field name is area_code

 

I want it to stop processing the form if the value is not one of the above.

 

But I will be also adding to the form, that if ANY of my fields are empty, not to submit it to the database. So I'm setting up a number of error checks.

 

My submit form and result page are on the same php page.

 

Does this make sense?

 

//Check All Required Variables To Make Sure They Are NOT Empty	if (empty($name) or empty($cat) or empty($url) or 	  empty($email) or empty($area_code) or empty($city)) {	print "one or more required fields are mising";	}//Check area code for valid region		if (int($area_code) !=519 or int($area_code) !=226) {		print "area code is not valid for this region";	   }

And if so, I'm not sure where on the page it fits into the structure, since it is all done on the same page.

 

 

Here's where they submit from: http://www.londonlink.ca/submit_a_site.php

Share this post


Link to post
Share on other sites

formatting ...

 

On my results page that you helped with yesterday (GET vs POST) I would like to format the numbers as $0.00.

 


Well, there is a very nice function that will format your monetary number for you if you have PHP 4.3.0+ and if it has been set up properly for your server.

 

If not, you have to use number format preceeded by a dollar sign.

 

I wrote this for you and will use use the money_format function if enabled on your system otherwise, it will use the number_forumat function:

<?php$number = 1234.56;if (function_exists(money_format)){	setlocale(LC_MONETARY, 'en_US');	$money = money_format('%(#0n', $number);}else {	$money = "$" . number_format($number, 2, '.', '');}echo $money;?>

Take a look here for more information: money_format

number_format

 

Now for your error checking, there are a couple of ways you can do it. The first is short and to the point but not very user friendlt I think.

 

if($this){	echo $that;}else{	exit('Form incomplete! - Go back and fill the form out completely please!");}

Your users won't like that much I think!

 

Your second option is probably better but more difficult. It records which form items were missed or incorrect then redirects the user back to the form prefilled with highlighting for the missed filleds or a message of which items need attention.

 

Basically, if the item needs some attention, we add it to an array then use that array later to list the problem fields!

<?php $item1 = 1;$item2 = 2;$item4 = 4;if (!isset($item1)){	if(isset($error_array)){		array_push($error_array, "Item 1");	}	else{		$error_array = array("Item 1");	}}if (!isset($item2)){	if(isset($error_array)){		array_push($error_array, "Item 2");	}	else{		$error_array = array("Item 2");	}}if (!isset($item3)){	if(isset($error_array)){		array_push($error_array, "Item 3");	}	else{		$error_array = array("Item 3");	}}if (!isset($item4)){	if(isset($error_array)){		array_push($error_array, "Item 4");	}	else{		$error_array = array("Item 4");	}}if (!isset($item5)){	if(isset($error_array)){		array_push($error_array, "Item 5");	}	else{		$error_array = array("Item 5");	}}if(isset($error_array)){	echo "You missed the following items:<br />\n";	$error_count = count($error_array) - 1;	for($x=0; $x <= $error_count; $x++){		echo "<font color=\"#FF0000\">$error_array[$x]</font><br>\n";	}// Here is where you need to add the reloading of the form and auto insert the correct data.}else{	echo "Congratulations, you managed to fill in all of the required fields!";// This would be where you insert your data into the database and inform the user that his order was submitted.}?>

That would output the following:

You missed the following items:

Item 3

Item 5

 


You would also have to reload the form and auto input the correct items from the last submission.

If you wanted to highlight each of the missed items in the form, you would have to do more coding for that.

 

 

The system can get kind of overwhelming if you don't try to break it up into smaller pieces.

Again, using a loop and/or a custom function to handle most of your work would probably be helpful. Otherwise, you will have to individually type out all of that code for each item to be checked. Not sure what the maximum number of items there might be in your form but it could be several.

 

Hope this helps.

 

vujsa

Share this post


Link to post
Share on other sites

That looks like quite a bit of code to format variables. I tried something this morning that didn't work:

<?php print money_format('%1', $item_1_price). '\n'; ?>

I was attempting to follow what was on php.net for the instructions. Do I have an error in this (above) or am I just completely wrong about how I am going about it?

Gin

This worked:

<?php echo '$' .number_format($item_1_price,2); ?>

Share this post


Link to post
Share on other sites

Well, that is the correct code but it will not work on windows machines and you have to have PHP4.3.0 or higher. Since that is the correct code, I think it is better to use that instead of number_format if your system allows for it.The code I gave you will use money_format if available but if not, it will use the number_format method!I generally think that you should always use the correct code for the situation which isn't always possible so you have to have a backup plan. That is what my code will do for you.But you are correct, if that was what was required to format a number, it would be a lot of code. This has a lot of optional code.It is of course possible to format you numbers manually in several ways which WOULD require a lot more code and gets kind of tedious. My point is, there is always more than one way to code something!Hope this helps.vujsa

Share this post


Link to post
Share on other sites

Yes I see what you mean about the amount of code, and the page is already a bit of a monster.I'm thinking of making a function for it, and then putting it on a require_once page.I went with the number format because it looked like less coding. I originally tried the money_format but had errors. On my PC I have PHP5 installed.BTW, can you be online when I do my exam next month? (Just kidding.)Gin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.