2013-09-30 3 views
0

Я не очень хорошо знаком с PHP. Раньше я мог читать и исправлять небольшие проблемы с помощью 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 = "[email protected]"; 
    $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> 

} 

?> 

Пожалуйста, обратите внимание, что «Вы человек» сообщение и условные у меня есть в сценарии, который является, где я думаю, что я делаю что-то неправильно.

+0

Сколько вы заплатили? – djot

+1

Вы должны сохранить результат '$ num1 + $ num2' в сеансе или что-то подобное вместо отправки его вместе с формой. – insertusernamehere

+1

Возможный дубликат http://stackoverflow.com/questions/13945515/integrate-captcha-with-a-form – Ritesh

ответ

1

Вы забыли вывести значения проверки из формы ... и у вас есть еще несколько простых ошибок, которые я также рассмотрел.

//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 = "[email protected]"; 
$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 
} 
?> 
+0

Спасибо, что нашли время и помогли мне. Теперь это прекрасно. – Raul

Смежные вопросы