2013-12-10 4 views
0

여기에 내 코드 : 그것은 오류를 표시하지 않는 경우 나는 지점에 도착했지만 아마 너무 명확하게 자신을 설명하지 않았다PHP 문의 양식 유효성 검사

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    if (empty($_POST["yourname"])) { 
     $yournameErr = "Name is required"; 
    } else { 
     $yourname = test_input($_POST["yourname"]); 
    } 

    if (empty($_POST["email"])) { 
     $emailErr = "Email is required"; 
    } else { 
     $email = test_input($_POST["email"]); 
     // check if e-mail address syntax is valid 
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { 
      $emailErr = "Invalid email format"; 
     } 
    } 

    if (empty($_POST["message"])) { 
     $messageErr = "Message is required"; 
    } else { 
     $message = test_input($_POST["message"]); 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

. 더 이상 오류 메시지가 표시되지 않는 지점에서 양식이 더 이상 나타나지 않으면 "성공"과 같은 항목을 넣을 수 있습니다. 그러나 나는 이것을 달성 할 수 없습니다.

내 양식은 다음과 같습니다

<form action="contact.php" name="Form1" id="Form1" method="post"> 
<div> 
<label>Your Name:</label> 
<br /> 
<input type="text" name="yourname" id="yourname" placeholder="Full Name" 
    style="border:1; border-color:#000000; " /> 
<span class="error">* <?php echo $yournameErr;?></span> 
</div> 
    <br /> 
<br /> 
<div> 
<label> Email :</label> <br /> 
<input name="email" type="text" id="email" size="20" placeholder="Email" 
    style="border:1; border-color:#000000; " /> 
<span class="error">* <?php echo $emailErr;?></span> 
</div> 
<br /> 
<br /> 
<div> 
<label> Subject : </label><br /> 
<input name="subject" type="text" id="subject" size="20" placeholder="Subject" 
    style="border:1; border-color:#000000; " /> 
</div> 
<br /> 
<br /> 
<div> 
<label> Message :<br /> </label> 
<textarea rows="5" cols="40" name="message" type="text" id="message" 
    placeholder="The message you want to send to us." style="border:1; border- 
    color:#000000 " > 
</textarea> 
<span class="error">* <?php echo $messageErr;?></span> 
</div> 
<br /> 
<br /> 
<div> 
<input type="submit" name="button" id="button" style="border:1; border- 
    color:#999999; " value="SEND"/> 
</div> 
</form> 
+0

처음에는 $ _POST 배열에 var_dump를 사용하고 검사가 끝나면 var_dump를 사용하십시오. 이 코드 부분에서 오류를 인쇄하지 않습니다. 예를 들어 반향을 넣는 것을 잊어 버리시겠습니까? – BaBL86

답변

0

당신이 배열에 오류를 넣고 그 배열의 크기를 확인하는 조건을 넣어하고 있는지 0 (오류가) 성공 메시지를 에코하지 않을 경우 다른 사람이 오류를 조인하고 그것을 출력해라. 어쩌면이 같은

:

contact.php 코드에 따르면

<?php 
$error = array(); 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["yourname"])) { 
     $error['name'] = "Name is required"; 
    } else { 
     $yourname = test_input($_POST["yourname"]); 
    } 

    if (empty($_POST["email"])) { 
     $error['email'] = "Email is required"; 
    } else { 
     $email = test_input($_POST["email"]); 
     // check if e-mail address syntax is valid 
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { 
      $error['email'] = "Invalid email format"; 
     } 
    } 

    if (empty($_POST["message"])) { 
     $error['message'] = "Message is required"; 
    } else { 
     $message = test_input($_POST["message"]); 
    } 

    if (!count($error)) { 
     $noError = true; 
    } 
} 

$successMessage = isset($noError) ? 'Successful.' : ''; 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

function getErrorMessage($type, $error) 
{ 
    return isset($error[$type]) ? $error[$type] : ''; 
} 

if ($successMessage) { 
    echo $successMessage; 
} else { 
?> 
<form action="contact.php" name="Form1" id="Form1" method="post"> 
    <div> 
     <label>Your Name:</label> 
     <br/> 
     <input type="text" name="yourname" id="yourname" placeholder="Full Name" 
       style="border:1px; border-color:#000000; "/> 
     <span class="error">* <?php echo getErrorMessage('name', $error); ?></span> 
    </div> 
    <br/> 
    <br/> 

    <div> 
     <label> Email :</label> <br/> 
     <input name="email" type="text" id="email" size="20" placeholder="Email" 
       style="border:1px; border-color:#000000; "/> 
     <span class="error">* <?php echo getErrorMessage('email', $error); ?></span> 
    </div> 
    <br/> 
    <br/> 

    <div> 
     <label> Subject : </label><br/> 
     <input name="subject" type="text" id="subject" size="20" placeholder="Subject" 
       style="border:1px; border-color:#000000; "/> 
    </div> 
    <br/> 
    <br/> 

    <div> 
     <label> Message :<br/> </label> 
     <textarea rows="5" cols="40" name="message" type="text" id="message" 
        placeholder="The message you want to send to us." style="border:1px; border- 
    color:#000000 "></textarea> 
     <span class="error">* <?php echo getErrorMessage('message', $error); ?></span> 
    </div> 
    <br/> 
    <br/> 

    <div> 
     <input type="submit" name="button" id="button" style="border:1px; border- 
    color:#999999; " value="SEND"/> 
    </div> 
</form> 
<?php } ?> 
+0

나는 초보자입니다. 그래서 코드가 필요합니다. –

+0

그것의 작동하지만 그것은 내 페이지의 헤더 맨 위에 성공 메시지를 보여 주며, 나는 그것을 폼의 맨 위에두고 싶습니다 .... ?? –

+0

전체 코드 게시 – FoPi

0

, 나는 당신의 contact.php 자체에 게시되어 있다고 가정하겠습니다. 다시 말해, PHP 코드가 HTML 위에 위치하므로 요청 후 더 이상 연락처 양식이 렌더링되지 않는다는 질문의 결과입니다. 즉, 서버가 페이지를 렌더링하면 제출에 오류가 없거나 super global $ _POST가 설정되어 양식 태그가 표시되지 않습니다.

가독성을 위해 코드가 약간 변경되었습니다. 양식 유효성 검사를 통해 모든 오류 메시지를 보관할 배열이 포함되었습니다. 오류가 없다면 우리는 성공적인 제출을 시뮬레이션 할 수 있으며 따라서이 결과를 응답에 반영 할 수 있습니다. 오류가 있거나 게시물이 제출되지 않은 경우에만 양식이 표시됩니다.

양식 안에 입력 태그 submit이 필요합니다. 또한 오류가 실제로 설정된 경우에만 오류 관련 메시지를 표시하려고합니다. 그래서 span 태그에 조건이 설정됩니다. 마지막으로, 귀하의 가치에 동일한 조건을 포함 시켰습니다 - 귀하가 입력 한 것을 기억하기위한 추가 기능.

양식이 성공적으로 제출 된 경우 양식 태그를 렌더링하지 말고 대신 모든 POST 데이터와 함께 성공 메시지를 표시하십시오!

<?php 

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

    $error_message = array(); 

    if (empty($_POST["yourname"])) { 
     $error_message['yournameErr'] = "Name is required"; 
    } else { 
     $yourname = test_input($_POST["yourname"]); 
    } 

    if (empty($_POST["email"])) { 
     $error_message['emailErr'] = "Email is required"; 
    } elseif (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $_POST["email"])) { 
     $error_message['emailErr'] = "Invalid email format"; 
    } else { 
     $email = test_input($_POST["email"]); 
    } 

    if (empty($_POST["message"])) { 
     $error_message['messageErr'] = "Message is required"; 
    } else { 
     $message = test_input($_POST["message"]); 
    } 

    if(empty($error_message)) { 
     // process data from post 
     $successful = true; 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

?> 
<?php if(!empty($error_message) || !isset($_POST['submit'])) : ?> 
    <form action="contact.php" name="Form1" id="Form1" method="post"> 
    <div> 
    <label>Your Name:</label> 
    <br /> 
    <input type="text" name="yourname" id="yourname" placeholder="Full Name" 
     style="border:1; border-color:#000000; " value="<?php if(isset($yourname)) {echo $yourname; }?>" /> 
    <span class="error">* <?php if(isset($error_message['yournameErr'])){echo $error_message['yournameErr']; }?></span> 
    </div> 
     <br /> 
    <br /> 
    <div> 
    <label> Email :</label> <br /> 
    <input name="email" type="text" id="email" size="20" placeholder="Email" 
     style="border:1; border-color:#000000; " value="<?php if(isset($email)) { echo $email;}?>" /> 
    <span class="error">* <?php if(isset($error_message['emailErr'])) {echo $error_message['emailErr'];}?></span> 
    </div> 
    <br /> 
    <br /> 
    <div> 
    <label> Subject : </label><br /> 
    <input name="subject" type="text" id="subject" size="20" placeholder="Subject" 
     style="border:1; border-color:#000000; " /> 
    </div> 
    <br /> 
    <br /> 
    <div> 
    <label> Message :<br /> </label> 
    <textarea rows="5" cols="40" name="message" type="text" id="message" placeholder="The message you want to send to us." style="border:1; border-color:#000000"></textarea> 
    <span class="error">* <?php if(isset($error_message['messageErr'])) {echo $error_message['messageErr']; }?></span> 
    <br> 
    <input type="submit" name="submit" value="Submit"> 
    </div> 
<?php endif; ?> 
<?php if(isset($successful)) : ?> 
    <p>Successful</p> 
    <p>Your name: <?=$yourname;?></p> 
    <p>Your email: <?=$email;?></p> 
    <p>Your subject: <?=$_POST['subject']?></p> 
    <p>Your message: <?=$message;?></p> 
    <a href="contact.php">Back to form</a> 
<?php endif; ?>