Please help someone..

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
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Please help someone..

Post by m@ndio »

Hi I can't get my code to work the way i want it to.... I am not getting any errors but it just doesn't do what I want it to do.

Basically I am trying to do the following:

1. user goes to index.php.

2. types in a code

3. if the code is not correct then re-direct them back to index.php and tell them the code is invalid.

4. If they did not enter anything into the box then tell them (same as 3. but message different).

5. If they try to call password.php directly from a web-browser then re-direct them to index.php.

6. if the code is correct then let them see the page (this bit works woopee).

========== index.php ===========

Code: Select all

<?php
$errors = $HTTP_POST_VARS["errors"];
?>
<html>
<head>
<title></title>
<meta name="author" content="m@ndio">
</head>
<body>
<?=$errors?>
<FORM METHOD="POST" ACTION="thanks.php">
<input type="hidden" name="Xon" value="true">
<TABLE cellpadding="3">
<TR>
	<TD>Promo code:</TD>
	<TD><INPUT TYPE="password" NAME="Xpromo"></TD>
</TR>
<TR>
	<TD colspan="2" align="center"><INPUT TYPE="submit" value="Gimme Gimme"></TD>
</TR>
</TABLE>
</FORM>
</body>
</html>
========== thanks.php ===========

Code: Select all

<?php
$formsub = $HTTP_POST_VARS["Xon"];

if($formsub == "true")	
{
	$yourcode = $HTTP_POST_VARS["Xpromo"];

	$errors = array();

	$yourcode = strtoupper($yourcode);	
	if($yourcode !== "TEST")
	{
		if(empty($yourcode))
			{
			$errors[] = "you have not entered a promo code";
			}
		header ("Location: index.php");
	}
	elseif($yourcode == "TEST")
	{
		// They are legit
?>

<html>
<head>
<title></title>
<meta name="author" content="m@ndio">
</head>

<body>
Thank you. Here are the discounted prices.
</body>
</html>
	<?
	}

	else
	{
		// the page was called directly.
		header ("Location: index.php");
	}
}
?>
mod_edit: added

Code: Select all

tafs[/size]
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

I think the reason you arent getting any errors on the index.php page is that the array $errors[] is only good for the thanks.php page, once you redirect to the index.php, nothing is passed to it...

in your index.php there is the line: '$errors = $HTTP_POST_VARS["errors"];' im not too sure that this is the way to go about getting your $errors[] array to the index.php.

could you be a lil more specific about what it does do? do you have a link?
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

First of all why do $errors variable is an array :?: It doesn't have to be. But if you want to get it right you must send the $errors within header, like this:

Code: Select all

header ("Location: index.php?errors=".$errors);
and then in index.php you have got something like this:

Code: Select all

<?php 
$errors = $HTTP_GET_VARS["errors"]; 

// or more PHP up from 4.1.0 style and more secure
$errors = strip_tags($_GET["errors"]); 
// of course if the $errors is a typical string not an array

?>
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

Dolorian,

thx for that.. it kind of works :? it does parse it back but now in the top bar it says this:

http://localhost/index.php?errors=Array

and it just simply prints this on the screen:

r

why is that?

my code is now this:

------------------ thanks.php --------------------

Code: Select all

<?php
$formsub = $HTTP_POST_VARS["Xon"];

if($formsub == "true")	
{
	$yourcode = $HTTP_POST_VARS["Xpromo"];

	$errors = array();

	$yourcode = strtoupper($yourcode);	
	if($yourcode !== "TEST")
	{
		if(empty($yourcode))
			{
			$errors[1] = "you have not entered a promo code";
			}
		header ("Location: index.php?errors=".$errors);
	}
	elseif($yourcode == "TEST")
	{
		// They are legit
?>
<html>
<head>
<title></title>
<meta name="author" content="m@ndio">
</head>

<body>
Thank you. Here are the discounted prices.
</body>
</html>

Code: Select all

<?
	}

	else
	{
		// the page was called directly.
		header ("Location: index.php");
	}
}
?>

----------------- index.php ---------------------

Code: Select all

<?php
//$errors = $HTTP_GET_VARS["errors"]; 
// or more PHP up from 4.1.0 style and more secure 
$errors = strip_tags($_GET["errors"]); 
// of course if the $errors is a typical string not an array 

//$errors = $HTTP_POST_VARS["errors"];
?>
<html>
<head>
<title></title>
<meta name="author" content="m@ndio">
</head>
<body>

Code: Select all

<?=$errors[1];?>
<FORM METHOD="POST" ACTION="thanks.php">
<input type="hidden" name="Xon" value="true">
<TABLE cellpadding="3">
<TR>
<TD>Promo code:</TD>
<TD><INPUT TYPE="password" NAME="Xpromo"></TD>
</TR>
<TR>
<TD colspan="2" align="center"><INPUT TYPE="submit" value="Gimme Gimme"></TD>
</TR>
</TABLE>
</FORM>
</body>
</html>


thanks for your help
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

First of all, delorian, not Dolorian :D

It displays you Array because the $errors variable is an array. I've written that you don't need the array here so erase line:

Code: Select all

$errors = array();
and get:

Code: Select all

$errors = "you have not entered a promo code";
But if you still insist on using $errors as an array. You should:

Code: Select all

------------------ thanks.php -------------------- 
<?php 
$formsub = $HTTP_POST_VARS["Xon"]; 

if($formsub == "true")    
{ 
   $yourcode = $HTTP_POST_VARS["Xpromo"]; 

   $errors = array(); 

   $yourcode = strtoupper($yourcode);    
   if($yourcode !== "TEST") 
   { 
      if(empty($yourcode)) 
         { 
         $errors[1] = "you have not entered a promo code"; 
         } 
      header ("Location: index.php?errors=".$errors[1]); // you must put the error number here
   } 
   elseif($yourcode == "TEST") 
   { 
      // They are legit 
?> 

<html> 
<head> 
<title></title> 
<meta name="author" content="m@ndio"> 
</head> 

<body> 
Thank you. Here are the discounted prices. 
</body> 
</html> 

<?php 
   } 

   else 
   { 
      // the page was called directly. 
      header ("Location: index.php"); 
   } 
} ?> 

----------------- index.php --------------------- 

<?php 
//$errors = $HTTP_GET_VARS["errors"]; 
// or more PHP up from 4.1.0 style and more secure 
$errors = strip_tags($_GET["errors"]); 
// of course if the $errors is a typical string not an array 

//$errors = $HTTP_POST_VARS["errors"]; 
?>  

<html> 
<head> 
<title></title> 
<meta name="author" content="m@ndio"> 
</head> 
<body> 

<? echo $errors; ?>  

<FORM METHOD="POST" ACTION="thanks.php"> 
<input type="hidden" name="Xon" value="true"> 
<TABLE cellpadding="3"> 
<TR> 
   <TD>Promo code:</TD> 
   <TD><INPUT TYPE="password" NAME="Xpromo"></TD> 
</TR> 
<TR> 
   <TD colspan="2" align="center"><INPUT TYPE="submit" value="Gimme Gimme"></TD> 
</TR> 
</TABLE> 
</FORM> 
</body> 
</html>

It should do the trick, but I don't know how to parse whole table in url :( Maybe someone would help :?:

OT: The script on this forum is eating my ?> signs in php code
Post Reply