0

我对 PHP 不是很熟悉。在过去,我能够阅读和修复使用 PHP 的小问题,但这给我带来了很多麻烦。

我有一个带有两个输入框的表单,一个用于电子邮件,一个用于消息。

现在,我尝试在表单中添加另一个框,以验证人工访问以达到反垃圾邮件的目的。

这是我无法通过验证过程的代码。

//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>

} 

?>

请注意“你是人类吗”消息和我在脚本中的条件句,这是我认为我做错了什么的地方。

4

1 回答 1

0

您忘记从表单中提取验证值......并且您还有一些其他简单的错误,我已经解决了这些错误。

//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
} 
?>
于 2013-09-30T15:17:08.763 回答