Question

I'm not very familiar with PHP. In the past I have been able to read and fix small problems using PHP but this one is giving me quite lot of trouble.

I have a form with two entry boxes, one for email and one for message.

Now, Im trying to add another box to the form to verify human access for anti-spam purposes.

This is the code which I can't make the verification process go through.

//create ramdom numbers
<?php
$num1 = rand(0,9);
$num2 = rand(0,9);
?>

<?php       
        $error    = '';
        $email    = ''; 
        $comments = ''; 
        $verify = ''; 


if(isset($_POST['contactus'])) {

    $email    = $_POST['email'];
    $comments = $_POST['comments'];
    $app =  $_SERVER["REQUEST_URI"];;


    if(trim($comments) == '') {
    $error = '<div class="error_message">Attention! Please enter your message.</div>';
    } else if(trim($email) == '') {
    $error = '<div class="error_message">Attention! Please enter a valid email address.</div>';
    } else if(!isEmail($email)) {
    $error = '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
    }

//This is where Im having problem. From this point the form doesn't go on.

if(trim($verify) == '') {
                error( '<div class="error_message">Attention! Please enter the verification number.</div>');
            } else if(trim($verify) != $verify_result) {
                error( '<div class="error_message">Attention! The number you entered is incorrect.</div>');
    }

    if($error == '') {

    if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
    }

    $address = "info@myaddress.com";
    $e_subject = 'You\'ve been contacted from an app web page ' . $name . '.';
    $e_body = "You have been contacted using the app comments box on the above app web page, their additional message is as follows.\r\n\n";
    $e_content = "\"$comments\"\r\n\n";
    $e_reply = "$name $email";          
    $msg = $e_body . $e_content . $e_reply;
    mail($address, $e_subject, $msg, $app, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

    // Email has sent successfully, echo a success page.
    echo "<div id='success_page_apps'>"; 
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you, your message has been submitted to us.</p>";
    echo "</div>";
    echo '<input type="button" value="Send Another" onClick="history.go(-1); return (true); ">';
    }
}

if(!isset($_POST['contactus']) || $error != '') {

    <?php echo $error; ?>

    <fieldset id="contact_apps">
        <form  method="post" action="#ContactForm">
            <label for="email" accesskey="E"><span class="required"></span> Email</label>

            <input name="email" type="text" id="email" size="33" value="<?php echo$email;?>"/>

            <textarea name="comments" cols="50" rows="15"  id="comments"><?php echo$comments;?></textarea>

//This is the 'Are you human?' message
            <p><span class="required">*</span> Are you human?</p>
            <label class="numbersq" for='verify' accesskey='V'><?php echo $num1; ?> + <?php echo $num2; ?> =</label>
            <input class="numbersa" name="verify" type="text" id="verify" size="4" value=""/>
            <input name="verify_result" type="hidden" size="4" value="<?php echo $num1+$num2; ?>" /><br />
            <input name="contactus" type="submit" class="send" id="contactus" >

        </form>
    </fieldset>

} 

?>

Please note the 'Are you human' message and the conditionals I have in the script which is where I think I'm doing something wrong.

Was it helpful?

Solution

You've forgotten to pull the verify values from the form ... and you have a few other simple errors in there too which I've addressed.

//create ramdom numbers
<?php
$num1 = rand(0,9);
$num2 = rand(0,9);
?>

<?php       
    $error    = '';
    $email    = ''; 
    $comments = ''; 
    $verify = ''; 


if(isset($_POST['contactus'])) {

    $email    = $_POST['email'];
    $comments = $_POST['comments'];
    $app =  $_SERVER["REQUEST_URI"];;


    if(trim($comments) == '') {
       $error = '<div class="error_message">Attention! Please enter your message.
  </div>';
  } else if(trim($email) == '') {
   $error = '<div class="error_message">Attention! Please enter a valid email address.  
</div>';
} else if(!isEmail($email)) {
$error = '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
}

//This is where Im having problem. From this point the form doesn't go on.
$verify_result= $_POST['verify_result'];
$verify         = $_POST["verify"];
if(trim($verify) == '') {
            $error =  '<div class="error_message">Attention! Please enter the verification number.</div>';
        } else if(trim($verify) != $verify_result) {
           $error =  '<div class="error_message">Attention! The number you entered is incorrect.</div>';
}

if($error == '') {

if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}

$address = "info@myaddress.com";
$e_subject = 'You\'ve been contacted from an app web page ' . $name . '.';
$e_body = "You have been contacted using the app comments box on the above app web page, their additional message is as follows.\r\n\n";
$e_content = "\"$comments\"\r\n\n";
$e_reply = "$name $email";          
$msg = $e_body . $e_content . $e_reply;
// mail($address, $e_subject, $msg, $app, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

// Email has sent successfully, echo a success page.
echo "<div id='success_page_apps'>"; 
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you, your message has been submitted to us.</p>";
echo "</div>";
echo '<input type="button" value="Send Another" onClick="history.go(-1); return (true); ">';
}
}

if(!isset($_POST['contactus']) || $error != '') {

echo $error; ?>

<fieldset id="contact_apps">
    <form  method="post" action="#ContactForm">
        <label for="email" accesskey="E"><span class="required"></span> Email</label>

        <input name="email" type="text" id="email" size="33" value="<?php echo$email;?>"/>

        <textarea name="comments" cols="50" rows="15"  id="comments"><?php echo$comments;?></textarea>

 //This is the 'Are you human?' message
        <p><span class="required">*</span> Are you human?</p>
        <label class="numbersq" for='verify' accesskey='V'><?php echo $num1; ?> +     <?php echo $num2; ?> =</label>
        <input class="numbersa" name="verify" type="text" id="verify" size="4" value=""/>
        <input name="verify_result" type="hidden" size="4" value="<?php echo $num1+$num2; ?>" /><br />
        <input name="contactus" type="submit" class="send" id="contactus" >

    </form>
</fieldset>
<?PHP
} 
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top