2014-11-10 4 views
-2

모든 것이 정확할 때라도 필드가 비어 있다고 계속 말합니다. Heres는 내 코드 :연락처 양식이 제대로 작동하지 않습니다. 로직이 될 수 있습니다.

<p>*All Fields Required</p> 

    <?php 

    if(!empty($reply)){ 
echo "<p class='notify'>$reply</p>"; 
} 
unset($reply); 

    ?> 

    <form method="post" action="index.php" id='contact'> 

    <fieldset> 

    <label>Name</label> 
    <input type='text' id="name" name="name" placeholder="type here" required value='<?php echo      $name; ?>'> 

    <label>Email</label> 
    <input type='email' id="email" name="email" placeholder="type here" required value='<?php echo $email; ?>'> 

    <label>Message</label> 
    <textarea id="message" name="message" placeholder="type here" required><?php echo $message; ?> </textarea> 

    <p>Answer the following CAPTCHA question: </p> 

    <label for="captcha">What color is Snow Whites hair?</label> 

    <input type='text' name='captcha' id='captcha' size='5'required><br> 

    <label for='action'>&nbsp;</label> 

    <input id="submit" name="submit" type="submit" value="submit"><br> 

    </fieldset> 


    </form> 

을 heres 내 index.php를 문서 그러므로 당신은 action라는 이름의 양식 필드가없는

<?php 

    if($_POST['action']=='submit'){ 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $message=$_POST['message']; 
    $captcha=strtolower($_POST['captcha']); 
    } 

    if(empty($name)||empty($email)||empty($message)){ 
    $reply='Sorry, one or more fields are empty. All fields are required.'; 
    include 'contactform.php'; 
    exit; 
    } 

    if(empty($captcha)|| $captcha != 'black'){ 
    $reply='The captcha answer is incorrect.'; 
    include 'contactform.php'; 
    exit; 
    } 

    $finalmessage="Name:$name\n"; 

    $finalmessage .="Email: $email\n"; 

    $finalmessage .="Message: \n$message"; 

//sending the message 

    $to="[email protected]"; 

    $from="From: $email"; 

    $result= mail($to, $finalmessage, $from); 

//Letting visitors know what happened 

    if($result ==TRUE){ 
    $reply = "Thank you $name for contacting us."; 
    unset($name); 

    unset($email); 

    unset($message); 

    include 'contactform.php'; 
    exit; 
} else{ 
    $reply='Sorry $name. There was an error and the message could not be sent'; 

     unset($name); 

     unset($email); 

     unset($message); 

     include 'contactform.php'; 

     exit; 
     } 
?> 
+2

POST 배열에서 "조치"를 찾고있는 것처럼 보이지만 전송의 이름을 "제출"으로 설정하고 있습니다. 제출 이름을 변경해보십시오. –

+0

양식을 확인하십시오. 'name = "action"'이있는 요소가 없습니다. 그래서,'$ _POST [ 'action']'은 절대로 설정되지 않습니다. 추신 왜'

답변

1

이 양식을 제출하는 것입니다 :

*var_dump($_POST) 

    **array (size=5) 
     'name' => string 'John Doe' (length=6) 
     'email' => string '[email protected]' (length=13) 
     'message' => string 'Blah Blah Blah' (length=10) 
     'captcha' => string 'White' (length=5) 
     'submit' => string 'submit' (length=6)** 

대신 $ _POST [ "행동"] 당신은 [ "제출"] $ _POST를 사용할 수 있으며 작동합니다.

2

,

이 항상 모두를 떠나, false로 평가됩니다

if($_POST['action']=='submit'){ 
당신이 만들려고하는 vars는 정의되지 않았습니다.

대신 실제 submit 버튼의 이름 인 $_POST['submit']을 원할 수도 있습니다.

<input id="submit" name="submit" type="submit" value="submit"><br> 
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
관련 문제