Page 1 of 1

Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 11:19 am
by edawson003
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>
 

Re: Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 11:35 am
by Mark Baker
Wrap them in
if (isset($_POST['subjoin']))

Re: Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 12:03 pm
by edawson003
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"

Re: Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 12:22 pm
by edawson003
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!!

Re: Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 1:37 pm
by Mark Baker

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

Re: Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 1:58 pm
by edawson003
Oh I see. Thank you much!

Re: Want conditional statements to fire only after {submit}

Posted: Sat Sep 12, 2009 3:14 pm
by Mark Baker
Glad you worked it out