Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone help with PHP Error I don't understand... 1

Status
Not open for further replies.

hneal98

Programmer
Aug 13, 2002
1,637
US
Hi,

I am trying find out why an piece of code will not execute in PHP. I am just learning, so I don't understand why this is happening.

I installed Firebug for Firefox on my PC so I can debug the PHP code. I have an include that calls another file, but it does not seem to actually get called.

Here is the code:

if ((isset($_SESSION['ordersent']))&&($_SESSION['ordersent']=="Sent"))
{
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head></head><body>';
echo "<center><br /><br /><br /><br /><br /><br /><div style='width: 500px; font-size: 26px; font-weight: bold;'>";
echo "Your order has been sent. You may now close this browser window.</div>";
echo "</center></body></html>";
}
else
{
$v=true;
$v=mail($to, $subject, $HTMLmsg, $headers);
$g=mail("hneal2009@gmail.com",$subject,$HTMLmsg2,$headers2);



if($v)
{

echo $v;
$_SESSION['ordersent']="Sent";
$ErrorMsg="";

//show confirmation page
include("email.php");

if (isset($_GET['clear']))
{
$shield=$_GET['clear'];

if ($shield==$_SESSION['shield'])
{
foreach($_SESSION as $key => $value)
{
unset($_SESSION[$key]);
}
}
$_SESSION['cart_empty']=1;
$cart=array("desc" => "FootLog","qty" => "0", "price" => "19.95");
$_SESSION["Item1"]=$cart;
$cart=array("desc" => "BodyLog","qty" => "0", "price" => "19.95");
$_SESSION["Item2"]=$cart;
$cart=array("desc" => "FootMag","qty" => "0", "price" => "15.95");
$_SESSION["Item3"]=$cart;
$cart=array("desc" => "Foot Cream","qty" => "0", "price" => "29.95");
$_SESSION["Item4"]=$cart;
$cart=array("desc" => "ToesEase®","qty" => "0", "price" => "19.95");
$_SESSION["Item5"]=$cart;
$cart=array("desc" => "ToeSoak®","qty" => "0", "price" => "19.95");
$_SESSION["Item6"]=$cart;
$shield=md5(time());
$_SESSION['shield']=$shield;
}

}
else
{

$ErrorMsg="Email not sent, there was a problem";
}
}
}
else
{

$Item1=$_SESSION[Item1];
$Item2=$_SESSION[Item2];
$Item3=$_SESSION[Item3];
$Item4=$_SESSION[Item4];
$Item5=$_SESSION[Item5];
$Item6=$_SESSION[Item6];

The echo statement shows that the if statement is being executed; however, the include statement does not seem to be executed.

I get this message from Firebug:

No Javascript on this page

If <script> tags have a "type" attribute it should equal "text/javascript" or "application/javascript"

But there is not Javascript. It is PHP.

Thanks
 
I installed Firebug for Firefox on my PC so I can debug the PHP code

how would this help? not sure but i thought firebug was uniquely client side? There are some php extensions for firebug but i have never had any success with them (in terms of usability). It is far far far better to footprint your code with print_r statements and similar. use debug backtraces to see what the functional tree looks like. Use your own error handlers if you want. But it is very rarely true errors that give coders problems. It is usually working out where the business logic has been incorrectly implemented in control structures within complex scripts. for this, only code footprinting will work.

Read the FAQ faq434-2999

At all times when you are debugging ensure that you have error_reporting switched to something like E_ALL, display_errors turned ON and startup errors also turned on. You do this in php.ini. You can turn the first two on in your code aswell (but this is useless if you have a parse error).
Code:
error_reporting(E_ALL); ini_set('display_errors', true);

style tip:
do not use quotes to shroud long blobs of html. instead use heredoc, includes or breakouts. this is for readability rather than speed.

Code:
<?php
if ((isset($_SESSION['ordersent']))&&($_SESSION['ordersent']=="Sent")): ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
	<html>
		<head></head>
		<body>
			<div style='width: 500px; margin-top: 50px auto; text-align:center; 26px; font-weight: bold;'>
			Your order has been sent.  You may now close this browser window.</div>
		</body>
	</html>

<?php exit; else:
style tip two:
consider using the alternative syntax for control structures. when debugging it is a lot easier to track if ... while ... endwhile ... endif than a set of embedded braces.

with all session manipulation remember that you MUST call session_start() first.
Code:
is(session_id() == '') session_start();

or, if you're lazy
Code:
@session_start();

ALWAYS enquote array keys unless they are numeric. This is wrong (even though sometimes it will work)
Code:
$Item1=$_SESSION[Item1];

it should be
Code:
$Item1=$_SESSION[[red]'[/red]Item1[red]'[/red]];

this code block
Code:
$Item1=$_SESSION[Item1];
    $Item2=$_SESSION[Item2];
    $Item3=$_SESSION[Item3];
    $Item4=$_SESSION[Item4];
    $Item5=$_SESSION[Item5];
    $Item6=$_SESSION[Item6];
can be compacted
Code:
for($i = 1; $i<=6; $i++): $Item{$i} = $_SESSION['Item'.$i]; endfor;

on to the functionality of your code:

this line
Code:
$v=mail($to, $subject, $HTMLmsg, $headers);
will not work as the variables are not set.

this is not necessary
Code:
foreach($_SESSION as $key => $value) 
                {
                    unset($_SESSION[$key]);
                }
just do this
Code:
unset($_SESSION);

or
Code:
$_SESSION = array();

hope that helps.
 
Thank you for the tips, I inherited the code from someone who, unfortunately, passed away. Anyway, I will go back and look at the changes you suggested.

Thanks,

Harvard
 
I would also recommend you show us what's inside email.php just for thoroughness sake. And also make sure email.php is located in the same path/directory/folder as the current script.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks, but I found that the problem was a piece of code where I had put <?php ?> with other code in it. Since the whole file was PHP, that was not necessary and caused it not to display.

Thanks again everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top