...

View Full Version : php exit command within html



TinmanIII
06-04-2007, 02:50 AM
I have spent all day on this trying to isolate the problem and correct it.
I need help :)
I am running php code inside a table, all is well except I have an "if" statement followed by "exit" which prevents the rest of my page loading if there is an error. I am trying to get the rest of my html to finish loading regardless of the error. The "exit" prevents anything from being written to the database (mysql) which needs to be intact.
here is sample html with real php code (problem is in red)

<html>
<body>
<table>
<tr>
<td>
<?php
//Get External Resources
require_once('Connections/petitionscript.php');
require("phpmailer/class.phpmailer.php");

//GET config Record Set
mysql_select_db($database_petitionscript, $petitionscript);
$query_config = "SELECT * FROM config";
$config = mysql_query($query_config, $petitionscript) or die(mysql_error());
$row_config = mysql_fetch_assoc($config);
$totalRows_config = mysql_num_rows($config);

# Switch Type Function
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

# Check For Duplicate Signatures -------------- >

mysql_select_db($database_petitionscript, $petitionscript);

$query_rsEmailDupChk = 'SELECT count(signature.Email) as DupChk FROM signature WHERE signature.Email = ' . GetSQLValueString($_POST['Email'], "text");

$rsEmailDupChk = mysql_query($query_rsEmailDupChk, $petitionscript) or die(mysql_error());

$row_rsEmailDupChk = mysql_fetch_assoc($rsEmailDupChk);

$totalRows_rsEmailDupChk = mysql_num_rows($rsEmailDupChk);

if ($row_rsEmailDupChk['DupChk'] > 0 ) {
echo " Sorry you have already signed this petition <br />";

# No Duplicate Signatures... Begin on screen confirmation and send email ----------->
} ELSE { ?>Your Petition Signature was received on
<?php
echo date('l, F d')?>
at <?php echo date('g:i:s A')?><br />
<br />
<?php echo $_POST['FirstName'] ?> , by law, for your electronic signature to count, we have to verify that you really signed and that someone did not use your email address fraudulently<br />
<br />
We have sent you an email, please click on the link in the email to verify your signature

<?php

$text = str_replace("\n.", "\n..", $text);
$mail = new PHPMailer(); //Create PHPmailer class
$mail->From = $row_config['FromEmail']; //Sender address
$mail->FromName = $row_config['FromName']; //The name that you'll see as Sender
$mail->Host = $row_config['EmailServer']; //Your SMTP mail server
$mail->Mailer = "smtp"; //Protocol to use
$mail->AddAddress($_POST['Email']); //The address you are sending mail to
$mail->Subject = $row_config['Subject']; //Subject of the mail
$mail->Body = "Thanks for filling out our petition " . $_POST['FirstName']. ", you're almost done! \n" .
"Please confirm your signature by clicking on the link below \n \n" .
"http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) .
"/confirm.php?Email=" . urlencode($_POST['Email']) . "&FirstName=" . urlencode($_POST['FirstName']) ."\n \n" .
"Sincerely \n".
"Your Petition Support Team";

if($row_config['SMTPAuth']==1)
{
$mail->SMTPAuth = "true";
$mail->Username = $row_config['Username'];
$mail->Password = $row_config['Password'];
}
elseif($row_config['SMTPAuth']==0)
{
$mail->SMTPAuth = "false";
}

if(!$mail->Send()){ //Check for result of sending mail
echo "OOPS... There was an error sending the message"; //Write an error message if mail isn't sent
exit; } //Exit the script without executing the rest of the code
//CLEAR CLEAN UP DB CONNECTION AND RS BEFORE WRITING
mysql_free_result($rsEmailDupChk);
mysql_free_result($config);



// Record The Signature ------------------>


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO signature (FirstName, LastName, Email, Address, City, Province, `TSDate`, IP) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['FirstName'], "text"),
GetSQLValueString($_POST['LastName'], "text"),
GetSQLValueString($_POST['Email'], "text"),
GetSQLValueString($_POST['Address'], "text"),
GetSQLValueString($_POST['City'], "text"),
GetSQLValueString($_POST['Province'], "text"),
GetSQLValueString($_POST['Date'], "date"),
GetSQLValueString($_POST['IP'], "text"));

mysql_select_db($database_petitionscript, $petitionscript);
$Result1 = mysql_query($insertSQL, $petitionscript) or die(mysql_error());
}
}
?>
</td>
</tr>
</body>
</html>

I am thinking I need an else{} statement but do not know enough to know if this will prevent any writing to the database.
if the else statement will suffice, can I simply substitute the problem code with this:
if(!$mail->Send()){ //Check for result of sending mail
echo "OOPS... There was an error sending the message"; //Write an error message if mail isn't sent
} //Exit the script without executing the rest of the code
else{}
//CLEAR CLEAN UP DB CONNECTION AND RS BEFORE WRITING
mysql_free_result($rsEmailDupChk);
mysql_free_result($config);

I would greatly appreciate some help,
Thanks in advance.
Tin

rafiki
06-04-2007, 02:58 AM
instead of using exit, you could use die("There was an error sending mail");


<html>
<body>
<table>
<tr>
<td>
<?php
//Get External Resources
require_once('Connections/petitionscript.php');
require("phpmailer/class.phpmailer.php");

//GET config Record Set
mysql_select_db($database_petitionscript, $petitionscript);
$query_config = "SELECT * FROM config";
$config = mysql_query($query_config, $petitionscript) or die(mysql_error());
$row_config = mysql_fetch_assoc($config);
$totalRows_config = mysql_num_rows($config);

# Switch Type Function
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

# Check For Duplicate Signatures -------------- >

mysql_select_db($database_petitionscript, $petitionscript);

$query_rsEmailDupChk = 'SELECT count(signature.Email) as DupChk FROM signature WHERE signature.Email = ' . GetSQLValueString($_POST['Email'], "text");

$rsEmailDupChk = mysql_query($query_rsEmailDupChk, $petitionscript) or die(mysql_error());

$row_rsEmailDupChk = mysql_fetch_assoc($rsEmailDupChk);

$totalRows_rsEmailDupChk = mysql_num_rows($rsEmailDupChk);

if ($row_rsEmailDupChk['DupChk'] > 0 ) {
echo " Sorry you have already signed this petition <br />";

# No Duplicate Signatures... Begin on screen confirmation and send email ----------->
} ELSE { ?>Your Petition Signature was received on
<?php
echo date('l, F d')?>
at <?php echo date('g:i:s A')?><br />
<br />
<?php echo $_POST['FirstName'] ?> , by law, for your electronic signature to count, we have to verify that you really signed and that someone did not use your email address fraudulently<br />
<br />
We have sent you an email, please click on the link in the email to verify your signature

<?php

$text = str_replace("\n.", "\n..", $text);
$mail = new PHPMailer(); //Create PHPmailer class
$mail->From = $row_config['FromEmail']; //Sender address
$mail->FromName = $row_config['FromName']; //The name that you'll see as Sender
$mail->Host = $row_config['EmailServer']; //Your SMTP mail server
$mail->Mailer = "smtp"; //Protocol to use
$mail->AddAddress($_POST['Email']); //The address you are sending mail to
$mail->Subject = $row_config['Subject']; //Subject of the mail
$mail->Body = "Thanks for filling out our petition " . $_POST['FirstName']. ", you're almost done! \n" .
"Please confirm your signature by clicking on the link below \n \n" .
"http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) .
"/confirm.php?Email=" . urlencode($_POST['Email']) . "&FirstName=" . urlencode($_POST['FirstName']) ."\n \n" .
"Sincerely \n".
"Your Petition Support Team";

if($row_config['SMTPAuth']==1)
{
$mail->SMTPAuth = "true";
$mail->Username = $row_config['Username'];
$mail->Password = $row_config['Password'];
}
elseif($row_config['SMTPAuth']==0)
{
$mail->SMTPAuth = "false";
}

if(!$mail->Send()){ //Check for result of sending mail
echo "OOPS... There was an error sending the message";
}else{ //see the else here? there wasnt one before!
//CLEAR CLEAN UP DB CONNECTION AND RS BEFORE WRITING
mysql_free_result($rsEmailDupChk);
mysql_free_result($config);



// Record The Signature ------------------>


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO signature (FirstName, LastName, Email, Address, City, Province, `TSDate`, IP) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['FirstName'], "text"),
GetSQLValueString($_POST['LastName'], "text"),
GetSQLValueString($_POST['Email'], "text"),
GetSQLValueString($_POST['Address'], "text"),
GetSQLValueString($_POST['City'], "text"),
GetSQLValueString($_POST['Province'], "text"),
GetSQLValueString($_POST['Date'], "date"),
GetSQLValueString($_POST['IP'], "text"));

mysql_select_db($database_petitionscript, $petitionscript);
$Result1 = mysql_query($insertSQL, $petitionscript) or die(mysql_error());
}
}
}// end the else
?>
</td>
</tr>
</body>
</html>

i also added an else statement so that the code will execute only when the sending of mail goes threw smoothley :)

TinmanIII
06-04-2007, 03:14 AM
Thank you rafiki,
I suspected as much, but do not know enough (yet) to feel safe doing it. Thank you again.
Tin

aedrin
06-04-2007, 05:53 PM
instead of using exit, you could use die("There was an error sending mail");

Did you look up what die() does?



die
(PHP 3, PHP 4, PHP 5)

die -- Equivalent to exit()
Description
This language construct is equivalent to exit().


You rarely (if ever) need to use exit() during normal program execution. Just properly structure your if/else statements.

TinmanIII
06-05-2007, 02:02 AM
Yes I did look up die, I went with the if else statement for this one.
Thanks for your input, I appreciate it.
Tin

rafiki
06-05-2007, 02:45 AM
Did you look up what die() does?
i knew it was basically exit but i wasnt sure if you could use exit("Failed: $error"); where i knew you could with die. i couldnt look up as i was trying to fix my problem whilst quickly browsing threw here

aedrin
06-05-2007, 05:23 PM
Yes I did look up die, I went with the if else statement for this one.

It was mostly aimed at Rafiki.

And I think I've told you this before, Rafiki.


i couldnt look up as i was trying to fix my problem whilst quickly browsing threw here

Don't give advice if you're not sure if it even does anything. It doesn't take much more than 10 seconds to look it up.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum