PDA

View Full Version : Mail form help


Dean B
03-28-1999, 08:49 AM
I'm writing a php mail form but am a little unsure as to how to write the code for required fields (devshed tutorials don't mention this http://www.aota.net/ubb/wink.gif). My brain is thinking along these lines -

REQUIRE $winningPlayer else errmessage 'You must supply your name. Please use your browsers back button and submit the form again'

I'd like to be able to check a number of fields and display a different error message depending on the missing field. Also I don't want them submitting the default 'Winning Player' from a drop down menu of names.

Thx.



------------------
Regards, Dean.
**DMCity Web Board**
www.dmcity.com/cgi-bin/ubb/Ultimate.cgi (http://www.dmcity.com/cgi-bin/ubb/Ultimate.cgi)

Justin
03-28-1999, 08:58 AM
The way you would do this is:

if ($WinningPlayer == "") {
print "I can't figure your score unless you tell me who won!!!";
} elseif ($Email == "") {
print "I need your email address, dude";
} elseif ($Username == "") {
print "Well, just who in the heck are you?";
} else {

# rest of code here

}

That should about take care of it. Just add more elseif's for each field. Here's another way I was just thinking:

if ($UserName == "") {
$Error = "name";
} elseif ($Email == "") {
$Error = "email";
}

....

if ($Error != "") {
print "You forgot to fill in your " . $Error . ". Please use your back button.";
} else {

# good code here

}

<update>
Oh, if you want to exclude a certain choice, like a default, you can set that default's value to be "" - for example:

<select name="Winner">
<option value="">default
<option>Player 1
</select>

Another way would be to give it a value of, say, "default". Then in the php, do this:

if (($Winner == "") | | ($Winner == "default")) {
error stuff...

</update>

Hope this helps http://www.aota.net/ubb/smile.gif

------------------
Justin Nelson
FutureQuest Support



[This message has been edited by Justin (edited 03-28-99).]

Dean B
03-28-1999, 04:54 PM
Justin,
Thx for the guidance above however... If one of the elseif statement returns true it prints the error message ok but then continues to run the rest of the script. Eg you input a score of 6, the error message shows but then displays 'whoever lost to whoever 6 up'
I'm guessing it needs some kind of die routine ?
Here's the code -


<?
if (($Winning_player =="") | | ($Winning_player =="default")) {
PRINT "You need to enter the winning player !";
}
elseif (($Losing_player =="") | | ($Losing_player =="default")) {
PRINT "How about telling me who you are !";
}
elseif (($Score <1) | | ($Score >5)) {
PRINT "Your score in a 9 hole match must be between 1 and 5";
}
elseif ($Password =="") {
PRINT "Please enter your password";
}

print "<font face=Verdana size=3 color=#FFFFFF>$Losing_player</font><font face=Arial size=3> lost to</font><font face=Verdana size=3 color=#FFFFFF> $Winning_player</font><font face=Arial color=#FF0000 size=4><strong> $Score</strong></font><font face=Arial size=3> up</font>";
print "




";
print "<font face=Arial size=3>Thanks for posting</font><font face=Verdana size=3 color=#FFFFFF> $Losing_player</font><font face=Arial size=3>, better luck next time !</font>";

?>


Dean.
ps. And yes, it works without escaping characters with a \ Don't ask me how .... http://www.aota.net/ubb/wink.gif

[This message has been edited by Dean B (edited 03-28-99).]

Justin
03-28-1999, 11:26 PM
Maybe in php you don't have to escape the forward slashes...

You need to add one last else to the end there - meaning that if none of the other if's return true, it will execute the final code:

<?
if (($Winning_player =="") | | ($Winning_player =="default")) {
PRINT "You need to enter the winning player !";
}
elseif (($Losing_player =="") | | ($Losing_player =="default")) {
PRINT "How about telling me who you are !";
}
elseif (($Score <1) | | ($Score >5)) {
PRINT "Your score in a 9 hole match must be between 1 and 5";
}
elseif ($Password =="") {
PRINT "Please enter your password";
}
else {

print "<font face=Verdana size=3 color=#FFFFFF>$Losing_player</font><font face=Arial size=3> lost to</font><font face=Verdana size=3 color=#FFFFFF> $Winning_player</font><font face=Arial color=#FF0000 size=4><strong> $Score</strong></font><font face=Arial size=3> up</font>";
print "




";
print "<font face=Arial size=3>Thanks for posting</font><font face=Verdana size=3 color=#FFFFFF> $Losing_player</font><font face=Arial size=3>, better luck next time !</font>";
}
?>

Try that http://www.aota.net/ubb/smile.gif

------------------
Justin Nelson
FutureQuest Support



[This message has been edited by Justin (edited 03-28-99).]

Dean B
03-29-1999, 09:45 AM
TYVM for your help Justin. I finished up with the code below. Everything seems to work hunky-dory http://www.aota.net/ubb/smile.gif

Very satisfying.


<?
if (($Winning_player =="") | | ($Winning_player =="default")) {
PRINT "<font face=Arial size=3>You need to enter the winning player ;-)</font> !";
PRINT "

";
PRINT "<font face=Arial size=2 color=#FDA02>Use your browsers back button to correct this</font>";
}
elseif (($Losing_player =="") | | ($Losing_player =="default")) {
PRINT "<font face=Arial size=3>How about telling me who you are !</font>";
PRINT "

";
PRINT "<font face=Arial size=2 color=#FDA02>Use your browsers back button to correct this</font>";
}
elseif (($Score <1) | | ($Score >5)) {
PRINT "<font face=Arial size=3>Your score in a 9 hole match must be between 1 and 5</font>";
PRINT "

";
PRINT "<font face=Arial size=2 color=#FDA02>Use your browsers back button to correct this</font>";
}
elseif ($Password =="") {
PRINT "<font face=Arial size=3>Please enter your password</font>";
PRINT "
";
PRINT "<font face=Arial size=2>Note: This form does not check your password, merely that you have entered one</font>";
PRINT "

";
PRINT "<font face=Arial size=2 color=#FDA02>Use your browsers back button to correct this</font>";
}
else {

print "<font face=Verdana size=3 color=#FDA02>$Losing_player</font><font face=Arial size=3> lost to</font><font face=Verdana size=3 color=#FDA02> $Winning_player</font><font face=Arial color=#FF0000 size=4><strong> $Score</strong></font><font face=Arial size=3> up</font>";
print "



";
print "<font face=Arial size=3>Thanks for posting</font><font face=Verdana size=3 color=#FDA02> $Losing_player</font><font face=Arial size=3>, better luck next time !</font>";
print "


";
print "<font face=Arial size=1>This result will be added on the next ladder update</font>";

mail ("dmc@dmcity.com", "2 Club Match Report",
"------------------------------ \n\n
Losing Player = $Losing_player \n\n
Winning Player = $Winning_player \n\n
Score = $Score \n\n
Password = $Password \n\n
------------------------------");
}
?>

Dean.

Terra
03-29-1999, 11:48 AM
Just a sidenote, if you really want to clean this up (dumping all the print statements) and mix PHP3 conditionals with pure html, I invite you to view:
http://www.php3.net/manual/html/construct.elseif.html

and read the section: Alternative syntax for IF statements: IF(): ... ENDIF;

Mixing HTML with progmatic statements is never fun, that's why Perl CGI can be such a chore by 'print'(ing) all of the HTML out from the script... http://www.aota.net/ubb/frown.gif

This may enhance the readability and allow much easier code editing without always worrying about whats needed to be backslash escaped and what doesn't...

--
Terra
--More than 1 way to heat up an electron--
FutureQuest

Justin
03-29-1999, 12:34 PM
That's how I do it:

if ($HTML == "a lot") {
?>
<html>
<head>

...

<? } else { ?>

<more html>
...

<?
}
?>

http://www.aota.net/ubb/smile.gif

------------------
Justin Nelson
FutureQuest Support

Dean B
03-29-1999, 02:49 PM
Thx Terra for that link. I've printed it out and will study over coffee http://www.aota.net/ubb/smile.gif
ATM Justin I can't quite grasp your code above. Hopefully reading said page will make it clearer.

Dean.
--- Never mind the electrons, which colour's the live wire again ? ---

Justin
03-29-1999, 03:16 PM
Basically, you can include normal HTML in an if block (or a for loop, while loop, do loop, etc) simply by closing the php tag:

<?
if ($email == "") {
?>
[b]Oops! Need your email, dude!
<?
} else {
?>
Thanks for signing up with <? echo $email ?>, dude!
<?
}
?>

Hope that clears it up a little... I may go read that link myself, as I'm playing a bit with php3 lately http://www.aota.net/ubb/smile.gif


------------------
Justin Nelson
FutureQuest Support



[This message has been edited by Justin (edited 03-29-99).]

Justin
03-29-1999, 03:45 PM
Well what do you know - IF...ENDIF - a little bit of BASIC has snuck into php http://www.aota.net/ubb/smile.gif

PS - if only there was a THEN...

[This message has been edited by Justin (edited 03-29-99).]

Terra
03-29-1999, 06:43 PM
--- Never mind the electrons, which colour's the live wire again ? ---

ROTFLMAO

That brightened up an otherwise intense day... http://www.aota.net/ubb/wink.gif

--Terra

[This message has been edited by ccTech (edited 03-29-99).]