I am having a little trouble with a form. I would like to verify that an email address was input and if not display a error message. Bots are clicking trough the form and it is sending off blank emails at the moment. I just modified the code and it looks like the following. I am not sure how to embed the back and submit buttons into the if and else statements as well. <fieldset style="width:600px" align="center"><legend>Confirmation</legend><br /> <form method="post" action="http://www.example.com/join_newsletter2.php"> <?php $email_address = $_POST['email_address']; $errors=0; $error="<b>The following errors occured while processing your request.</b><br /><br />"; if($email_address=="") {// No sense in going any further if no email address entered $errors=1; $error.="<p>You did not enter a valid email address. Please go back and try again.</p>"; } if($errors==1) {echo $error;} else{ print "$email_address<br><br>Is this correct? If so, please press the submit button below.<br><br>"; } ?> <input type="hidden" name="email_address" value="<?=$email_address?>" /> <input type="button" onclick="history.go(-1);" value="Back" /> <input type="submit" name="submit" value="Submit" /> </form> <br /></fieldset> PHP:
Thought about that, but the way the site is setup, it can not be done. The entry box on the main page and the code i have posted is on part of the code on the 2nd page (verification page)
You can try this (Javascript verification): Basically, the JS function checks the email input before process the form, and if its empty, it stop the form process and show an "Error: You did not enter a valid email address." alert, and also it makes a focus in the email input. Good luck
I just noticed with that fix, if you click on the input box then hit enter (not pressing the button) it bypasses the input check
<?php if (sizeof($_POST) > 0) // Check if the form was submitted { $error = ""; if (empty($_POST['email_address'])) { $error = "<b>The following errors occured while processing your request.</b><br /><br />"; $error .= "<p>You did not enter a valid email address. Please go back and try again.</p>"; } if (!empty($error)) { echo $error; } else { // Redirect them to another page that has a form for verifying the email address // header("Location: check.php?email={$_POST['email_address']}"); } } ?> <fieldset style="width:600px" align="center"> <legend>Confirmation</legend><br /> <form method="post" action="http://www.example.com/join_newsletter2.php"> <input type="hidden" name="email_address" value="<?=$email_address?>" /> <input type="button" onclick="history.go(-1);" value="Back" /> <input type="submit" name="submit" value="Submit" /> </form> </fieldset> PHP: As for the submit button you'll need to create a separate page. You cannot use the same form for two different purposes. Basically once they submit the form check if the email was even given and if so then redirect them to a new page that has a form with a submit button they click to verify the email address was correct.
Here's a somewhat crude (but functional) example: <?php // If first form has been submitted if ( isset($_POST['submit']) ) { // If email has been supllied if ( empty($_POST['email']) ) { echo '<p>Please provide an e-mail address</p>'; } // If email has been supplied else { // Display second/confirmation form ?> <form method="post" action"<?php echo $_SERVER['PHP_SELF']; ?>"> <p>Please confirm that the e-mail address <?php echo $_POST['email']; ?> is correct by pressing the submit button below</p> <input type="submit" name="confirm" value="Submit"> </form> <?php } } // If second/confirmation form has been submitted elseif ( isset($_POST['confirm']) ) { echo 'Thanks for confirming!'; } // If form has not been submitted else { // Display first form ?> <form method="post" action"<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="email"> <input type="submit" name="submit" value="Submit"> </form> <?php } ?> PHP: