2017-04-11 3 views
0
<?php 

$name_error = $email_error = $phone_error = $url_error = $last_name_error= ""; 
$firstName = $lastName = $email = $phone = $message = $url = $success = ""; 

if (isset($_POST["sendMessage"])) { 
    if (empty($_POST["firstName"])) { 
     $name_error = "Name is required"; 
    } else { 
     $firstName = test_input($_POST["firstName"]); 
     // check if name only contains letters and whitespace 
     if (!preg_match("/^[a-zA-Z ]*$/",$firstName)) { 
      $name_error = "Only letters and white space allowed"; 
     } 
    } 
    if (empty($_POST["lastName"])) { 
     $last_name_error = "Name is required"; 
    } else { 
     $lastName = test_input($_POST["lastName"]); 
     // check if name only contains letters and whitespace 
     if (!preg_match("/^[a-zA-Z ]*$/",$lastName)) { 
      $last_name_error = "Only letters and white space allowed"; 
     } 
    } 
    if (empty($_POST["email"])) { 
     $email_error = "Email is required"; 
    } else { 
     $email = test_input($_POST["email"]); 
     // check if e-mail address is well-formed 
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $email_error = "Invalid email format"; 
     } 
    } 

    if (empty($_POST["phone"])) { 
     $phone_error = "Phone is required"; 
    } else { 
     $phone = test_input($_POST["phone"]); 
     // check if e-mail address is well-formed 
     if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) { 
      $phone_error = "Invalid phone number"; 
     } 
    } 


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

    if ($name_error == '' and $last_name_error == '' and $email_error == '' and $phone_error == ''){ 
     $message_body = ''; 
     unset($_POST['sendMessage']); 
     foreach ($_POST as $key => $value){ 
      $message_body .= "$key: $value\n"; 
     } 

     $to = '[email protected]'; 
     $subject = '[email protected]'; 
     if (mail($to, $subject, $message)){ 
      $success = "Message sent, thank you for contacting us!"; 
      $firstName = $lastName = $email = $phone = $message = ''; 
     } 
    } 

} 

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

?> 

나는이 양식을 검증하려고 노력했다. 하지만 작동하지 않습니다. 때마다 나는 전자 메일 양식 라이브 서버 전용 메시지를 보낼 수 있지만 이름, 전화, 이메일 보내지 마십시오. 누군가 나를 도와주세요. 나는 지난 날부터 일하고 있지만 일하지 않고있다.문의 양식 유효성 확인

답변

1

전혀 보내지 않기 때문입니다. 잘 먹으 렴,

if (mail($to, $subject, $message)){ 

Yoou're 메시지와 변수 $ 메시지 전송 ..하지만,이 변수 안에 무슨 모양이 있습니다 : 코드 봐

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

당신은 할당하고를 $_POST["message"] 값 - 그 밖의 것은 없습니다.

foreach ($_POST as $key => $value){ 
      $message_body .= "$key: $value\n"; 
     } 

난 당신이 $ 메시지 대신 $의 MESSAGE_BODY를 보낼 것이라고 생각, 그것은 잘 작동합니다 :)

은 도움 바랍니다.

+0

나는 이걸 chg하려고하고있다. 하지만 잘못된 이름, 번호 또는 이메일을 보내면 메시지가 표시되지 않습니다. 이 문제에 대해 대답하십시오. – Farjana

+0

왜 그래야합니까? 모든 것이 좋으면 'IF'조건 만 있고, 전자 메일은 'else'조건을 보내지 않습니다. 'else'를 만든 다음 오류 메시지를 단순히'echo'합니다. – Twinfriends

+0

은'$ message_body'를 보내지 않습니다. –