Form help-verifying email address was input

Discussion in 'PHP' started by ziplock122949, Jun 21, 2008.

  1. #1
    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:

     
    ziplock122949, Jun 21, 2008 IP
  2. aqlx86

    aqlx86 Peon

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you should add captcha verification for bots
     
    aqlx86, Jun 22, 2008 IP
  3. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    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)
     
    ziplock122949, Jun 22, 2008 IP
  4. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    anyone have an answer?
     
    ziplock122949, Jun 23, 2008 IP
  5. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #5
    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
     
    dannet, Jun 24, 2008 IP
  6. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    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
     
    ziplock122949, Jul 1, 2008 IP
  7. AliasXNeo

    AliasXNeo Banned

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?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.
     
    AliasXNeo, Jul 1, 2008 IP
  8. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #8
    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:
     
    clarky_y2k3, Jul 1, 2008 IP
  9. lightningzeus

    lightningzeus Peon

    Messages:
    188
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i would recommend javascript to do this
     
    lightningzeus, Jul 1, 2008 IP