2017-11-01 6 views
0

現在、私のサイトにはJorgeGoris.com私は検証のためのform_process.phpと呼ばれるPHPファイルを指しているフォームを持っています。私の問題は、私が私が私がform_process.phpにリダイレクトされているので、私は同じページで動的に行われるようにしたいので、私が実装した検証や成功メッセージが決して現れないことです。これどうやってするの?これは私のフォームとPHPのコードです:HTMLとPHPファイル間の動的フォーム検証。これは可能ですか?

<?php 
 

 
// define variables and set to empty values 
 
$firstName_error = $email_error = $phone_error = ""; 
 
$firstName = $lastName = $email = $phone = $message = $success = ""; 
 

 
//form is submitted with POST method 
 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
 
    if (empty($_POST["firstname"])) { 
 
    $firstName_error = "First name is required"; 
 
    } else { 
 
    $firstName = test_input($_POST["firstname"]); 
 
    // check if name only contains letters and whitespace 
 
    if (!preg_match("/^[a-zA-Z ]*$/",$firstName)) { 
 
     $firstName_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 ($firstName_error == '' and $email_error == '' and $phone_error == ''){ 
 
     $message_body = ''; 
 
     unset($_POST['submit']); 
 
     foreach ($_POST as $key => $value){ 
 
      $message_body .= "$key: $value\n"; 
 
     } 
 
     
 
     $to = '[email protected]'; 
 
     $subject = 'Potential Client/Employer-JorgeGoris'; 
 
     if (mail($to, $subject, $message_body)){ 
 
      $success = "Message sent, I'll get back to you shortly!"; 
 
      $firstName = $email = $phone = $message = ''; 
 
      echo "<h1>Got it! I'll get back to you shortly!</h1>"; 
 
     } 
 
    } 
 
    
 
} 
 

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

 
?>
<form action="form_process.php" method="post" name="contact_form"> 
 
\t \t \t \t <div class="form-row"> 
 
\t \t \t \t \t <div class="col-lg-3 offset-lg-3 col-sm-12"> 
 
\t \t \t \t \t \t <input type="text" class="form-control" placeholder="First Name *" name="firstname"> 
 
\t \t \t \t \t \t <span class="error"><?php= $firstName_error ?></span> 
 
\t \t \t \t \t </div><!--end col--> 
 
\t \t \t \t \t <div class="col-lg-3 col-sm-12"> 
 
\t \t \t \t \t \t <input type="text" class="form-control" placeholder="Last Name" name="lastname"> 
 
\t \t \t \t \t </div><!--end col--> 
 
\t \t \t \t </div><!--end row--> 
 
\t \t \t \t <div class="form-row"> 
 
\t \t \t \t \t <div class="col-lg-3 offset-lg-3 col-sm-12"> 
 
\t \t \t \t \t \t <input type="text" class="form-control" placeholder="Phone Number" name="phone"> 
 
\t \t \t \t \t \t <span class="error"><?php= $phone_error ?></span> 
 
\t \t \t \t \t </div><!--end col--> 
 
\t \t \t \t \t <div class="col-lg-3 col-sm-12"> 
 
\t \t \t \t \t \t <input type="email" class="form-control" placeholder="Email *" name="email"> 
 
\t \t \t \t \t \t <span class="error"><?php= $email_error ?></span> 
 
\t \t \t \t \t </div><!--end col--> 
 
\t \t \t \t </div><!--end row--> 
 
\t \t \t \t <div class="row"> 
 
\t \t \t \t \t <div class="col-lg-5 offset-lg-3 col-sm-12"> 
 
\t \t \t \t \t \t <textarea class="form-control" rows="5" placeholder="Tell me a little about your project. Budget details are also appreciated. *" name="message"></textarea> 
 
\t \t \t \t \t </div><!--end col--> 
 
\t \t \t \t </div><!--end row--> 
 
\t \t \t \t <div class="success"><?php= $success; ?></div> 
 
\t \t \t \t <button type="submit" class="button" name="submit">Submit</button> 
 
\t \t \t </form><!--end form-->

答えて

0

所望の機能を達成するために適切な方法は、PHPファイル(または関数)に送信されたフォームデータを持つことであるロードを担当していますフォーム。フォームをロードするPHPコードは条件付きでなければなりません。たとえば、送信されたポストデータのエラーをチェックすることができます。エラーがある場合は、フォームの通常のHTMLコードに加えてエラーを表示するHTMLコードを出力する必要があります。

フォームが指しているPHPスクリプトがフォームを含むページのHTMLを出力しないため、送信ボタンを押すと何も表示されません。

(私は質問があまりにも一般的であるので、あなたのコード例を与えることができないよ。)

+0

まあ、私のフォームはHTMLページです。あなたが言ったことはまだ可能ですか? – Jorge

関連する問題