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
edawson003
Forum Contributor
Posts: 133 Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA
Post
by edawson003 » Sat Sep 12, 2009 11:19 am
How can I set it up so the conditional statements to fire only after {submit} button is clicked? Right now the condition checking whether the field is empty or not is firing upon landing prior to submit button being clicked.
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?
/* unsetting variables for email and the email error messages */
unset($email);
unset($emailerror);
$email = $_POST['email'];
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email) && $email != ""){
$emailerror = "\t<font color=red> Invalid email</font>\n";
}
if ($email == ""){
$emailerror = "\t<font color=red> No email address entered</font>\n";
}
?>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<input type="text" name="email" maxlength="30">
<input type="submit" name="subjoin" value="Submit!">
<? echo "\t<br>\n";
echo $emailerror; ?>
</form>
</body>
</html>
Mark Baker
Forum Regular
Posts: 710 Joined: Thu Oct 30, 2008 6:24 pm
Post
by Mark Baker » Sat Sep 12, 2009 11:35 am
Wrap them in
if (isset($_POST['subjoin']))
edawson003
Forum Contributor
Posts: 133 Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA
Post
by edawson003 » Sat Sep 12, 2009 12:03 pm
Like this?
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?
/* unsetting variables for email and the email error messages */
unset($email);
unset($emailerror);
$email = $_POST['email'];
if (isset($_POST['subjoin'])) {
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email) && $email != ""){
$emailerror = "\t<font color=red> Invalid email</font>\n";
}
if ($email == ""){
$emailerror = "\t<font color=red> No email address entered</font>\n";
}
}
?>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<input type="text" name="email" maxlength="30">
<input type="submit" name="subjoin" value="Submit!">
<? echo "\t<br>\n";
echo $emailerror; ?>
</form>
</body>
</html>
Still getting conditional statement fire prior to submit, "No email address entered"
edawson003
Forum Contributor
Posts: 133 Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA
Post
by edawson003 » Sat Sep 12, 2009 12:22 pm
Actually, now I see what my problem was. I had this condition statement
Code: Select all
if (isset($_POST['subjoin']) && [color=#FF0000]$email [/color]== ""){
$emailerror = "\t<font color=red> No email address entered</font>\n";
}
this part which would always be true prior to form submission because I unset the $email variable in the beginning
Code: Select all
[color=#FF0000]$email [/color]== ""), so I changed it something more logical like
$_POST['email'] == "")
...and now it works fine
- - - Sorry, I can't help it...I'm a newb
Thanks again!!
Mark Baker
Forum Regular
Posts: 710 Joined: Thu Oct 30, 2008 6:24 pm
Post
by Mark Baker » Sat Sep 12, 2009 1:37 pm
Code: Select all
if (isset($_POST['subjoin'])) {
$email = trim($_POST['email']);
if (strlen($email) == 0){
$emailerror = "\t<font color=red> No email address entered</font>\n";
} elseif (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email)) {
$emailerror = "\t<font color=red> Invalid email</font>\n";
}
}
You should look at using preg_match() instead of eregi(), because eregi() won't be a PHP function for much longer
edawson003
Forum Contributor
Posts: 133 Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA
Post
by edawson003 » Sat Sep 12, 2009 1:58 pm
Oh I see. Thank you much!
Mark Baker
Forum Regular
Posts: 710 Joined: Thu Oct 30, 2008 6:24 pm
Post
by Mark Baker » Sat Sep 12, 2009 3:14 pm
Glad you worked it out