visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: PHP Exit command


By: faesk U.S.A.  Date: 12/05/2003 00:00:00  English  Points: 100 Status: Answered
Quality : Excellent
Hello!

I am using the php exit command in my signup form for my website.
The script is working fine.

Here is how I am using the exit command

// CHECK FOR REQUIRED FIELDS
if (!trim($username))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Username field cannot be left blank!</b></font></p>";
exit;
}
if (!trim($password))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Password field cannot be left blank!</b></font></p>";
exit;
}
if (!trim($fname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>First Name field cannot be left blank!</b></font></p>";
exit;
}
if (!trim($lname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Last Name field cannot be left blank!</b></font></p>";
exit;
}
if (!trim($email))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Email field cannot be left blank!</b></font></p>";
exit;
}

But using the exit command messes up the tables i am using to display these results and the pictures in tables become messy and the entire page doesnt get processed... due to the exit command.

now is there a way or alternative to the exit command that i can use which will only exit the php section "<? ?>" of my page and not the html section...

waiting for a reply

THank you
By: TheWebMonster Date: 12/05/2003 16:34:00 English  Type : Comment
You could try this. Replace YOURSIGNUPPAGE.php with the page name your form is on.

Hope this helps.

// CHECK FOR REQUIRED FIELDS
if (!trim($username))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Username field cannot be left blank!</b></font></p>";
include "YOURSIGNUPPAGE.php";
}
if (!trim($password))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Password field cannot be left blank!</b></font></p>";
include "YOURSIGNUPPAGE.php";
}
if (!trim($fname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>First Name field cannot be left blank!</b></font></p>";
include "YOURSIGNUPPAGE.php";
}
if (!trim($lname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Last Name field cannot be left blank!</b></font></p>";
include "YOURSIGNUPPAGE.php";
}
if (!trim($email))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Email field cannot be left blank!</b></font></p>";
include "YOURSIGNUPPAGE.php";
}
By: VGR Date: 12/05/2003 16:57:00 English  Type : Answer
ok, your problem is because you separated HTML and PHP. You even speak of different "sections". I don't like this.
You should use echo <<<EOS
HTML with PHP $variables in it
more HTML
</HTML>
EOS;

from inside the PHP code. All the page should be enclose in <?php and ,>, in fact and IMHO.

As for your problem now : you've two solutions.

Either you delay the exit until after the HTML output (so that it completes), or you prevent any output to start before you made the checks (so that output doesn't pose any problem)

Solution 1 :
$willexit=FALSE;
// CHECK FOR REQUIRED FIELDS
if (!trim($username))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Username field cannot be left blank!</b></font></p>";
$willexit=TRUE;
}
if (!trim($password))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Password field cannot be left blank!</b></font></p>";
$willexit=TRUE;
}
if (!trim($fname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>First Name field cannot be left blank!</b></font></p>";
$willexit=TRUE;
}
if (!trim($lname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Last Name field cannot be left blank!</b></font></p>";
$willexit=TRUE;
}
if (!trim($email))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Email field cannot be left blank!</b></font></p>";
$willexit=TRUE;
}
// here the rest of the HTML to complete it
// (1)
if ($willexit) exit;


(1) :
or a special block like this :

if ($willexit) {
echo <<<EOS
closing HTML : /TD, /TR, /TABLE, /BODY, /HTML
EOS;
exit;
} // if willexit is true
// here the rest of the HTML for the case when we didn't have to exit prematurely


Solution 1bis :
do your if(!$trim($email)) et al tests at the start of the script, building $errmsg='Last Name field cannot be left blank!'
and flag an $iserror variable to TRUE
repeat for all cases, as shown above in your code

then start HTML output as previously
and at some point write :
if ($isError) print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>$errmsg</b></font></p>";

and at the end of HTML processing
if ($isError) exit;


Solution 2 :
do your "if(!$trim($email))" etc tests at the start of the script and display message+exit; without being stuck in the middle of a TABLE or other visual elements

By: hostsol Date: 13/05/2003 18:18:00 English  Type : Comment
I guess you misuse the exit command.

In your case, you should use elseif statements instead of using exit in each statement. So that the code should be:

// CHECK FOR REQUIRED FIELDS
if (!trim($username))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Username field cannot be left blank!</b></font></p>";

}
elseif (!trim($password))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Password field cannot be left blank!</b></font></p>";

}
elseif (!trim($fname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>First Name field cannot be left blank!</b></font></p>";

}
elseif (!trim($lname))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Last Name field cannot be left blank!</b></font></p>";

}
elseif (!trim($email))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Email field cannot be left blank!</b></font></p>";
}

By: VGR Date: 13/05/2003 18:29:00 English  Type : Comment
I think you misunderstood what he wants do to : exit; after each error
By: Oliver_Dornauf Date: 13/05/2003 18:29:00 English  Type : Comment
$errorMsg = "";
if (!trim($username))
{ // append the errorstring after each if
$errorMsg = $errorMsg + "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Username field cannot be left blank!</b></font></p>";
}
.
.
.
.

after each check
if (strlen ($errorMsg) > 0) {
exit;
}
By: VGR Date: 13/05/2003 18:38:00 English  Type : Comment
well, that's already been proposed as a solution 8-)))

except that strlen($errorMsg)>0 can become ($errorMsg<>"") or just ignored and issued whatever : an empty string doesn't harm the display.

see solution 1bis above
By: hostsol Date: 13/05/2003 19:19:00 English  Type : Comment
I don't see in this case why you need to exit after each error. Once you use exit(), you cannot output anything.

faesk, you should use elseif instead of exit if you want to output the html after the if/elseif statement so that the remaining html tags can be correctly outputed.
By: VGR Date: 13/05/2003 19:33:00 English  Type : Comment
it's exactly the same

doing

if (test1) {
// action 1
exit;
}
if (test2) {
// action 2
exit;
}
if (test3) {
// action 3
exit;
}


or

if (test1) {
// action 1
exit;
} else if (test2) {
// action 2
exit;
} else if (test3) {
// action 3
exit;
}


is exactly the same ; it's just a little bit more difficult to read and to brackets-match ;-)

exactly the same number of comparisons, of tests...

By: hostsol Date: 13/05/2003 19:42:00 English  Type : Comment
VGR, did you read my post. I didn't have used exit in my post.

And one more thing, I don't see Oliver_Dornauf's comment can solve the problem as once exit is used, the remaining html tags(eg. </table>) will still not be outputed. I am sure faesk's question is that he wants to output the remaining html codes.


By: VGR Date: 13/05/2003 19:57:00 English  Type : Comment
I was answering to the people suggesting to use "elsif" as if it were a panacea :D

Anyway, i you look in my first answer, I think you'll find a solution you'll like.
By: hostsol Date: 13/05/2003 20:05:00 English  Type : Comment
No I am not saying it is a panacea. Please stop being ironic.

I was just guessing faesk seems even didn't know he can use elseif statements in php because he said "only exit the php section <? ?>".

P.S. Sorry it is "elseif" not "elsif"(in Perl or what)
By: elbuentrovas Date: 13/05/2003 20:20:00 English  Type : Comment
Hi man,

if you only want to "exit the php section <? ?>" why dont you use a flag

$flag=0

// CHECK FOR REQUIRED FIELDS
if (!trim($username))
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Username field cannot be left blank!</b></font></p>";
$flag=1;
}
if (!trim($password)&&$flag!=1)
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Password field cannot be left blank!</b></font></p>";
$flag=1;
}
if (!trim($fname)&&$flag!=1)
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>First Name field cannot be left blank!</b></font></p>";
$flag=1;
}
if (!trim($lname)&&$flag!=1)
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Last Name field cannot be left blank!</b></font></p>";
$flag=1;
}
if (!trim($email)&&$flag!=1)
{
print "<p><font size=\"3\" face=\"Verdana, Arial\" color=\"#FF0000\"><b>Email field cannot be left blank!</b></font></p>";
$flag=1;
}

whatever else you don´t want to have executed between <? ?> if any error occured put it between curly braces of the next conditional

if($flag!=1){

}

a nice and very simple solution.

Hope it helps.
Rodrigo(Mexico)

By: VGR Date: 14/05/2003 05:11:00 English  Type : Comment
thanks Rodrigo ; please read my solution # 1 ;-)

Do register to be able to answer

EContact
browser fav
page generated in 336.030010 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page