2017-11-01 1 views
0

Derzeit auf meiner Website JorgeGoris.com Ich habe ein Formular, das auf eine PHP-Datei namens form_process.php zur Validierung verweist. Mein Problem ist, dass ich die Validierungs- oder Erfolgsmeldungen, die ich implementiert habe, nie sehe, weil ich, wenn ich auf submit stoße, zu form_process.php umgeleitet werde. Ich möchte, dass dies dynamisch auf derselben Seite geschieht. Wie kann ich das machen? Das ist meine Form und PHP-Code:Dynamische Formularüberprüfung zwischen einer HTML- und PHP-Datei. Ist das möglich?

<?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-->

Antwort

0

Die richtige Methode die gewünschte Funktionalität zu erreichen, ist das Formular-Daten an die PHP-Datei gesendet haben (oder Funktion), die für die Beladung verantwortlich ist die Form. Der PHP-Code, der das Formular lädt, sollte bedingt sein. Zum Beispiel können Sie eine Überprüfung auf Fehler in den Post-Daten haben und wenn es einen Fehler gibt, sollte HTML-Code, der den Fehler anzeigt, zusätzlich zum normalen HTML-Code des Formulars ausgegeben werden.

In Ihrem Fall, dass Sie nichts sehen, wenn Sie die Absenden-Button getroffen, weil der PHP-Skript, das Formular nicht ausgegeben den HTML-Code der Seite tut zeigt, die die Form enthält.

(Ich bin nicht in der Lage Ihnen ein Codebeispiel zu geben, weil die Frage zu allgemein ist.)

+0

Nun meine Form eines auf einer HTML-Seite. Ist das, was du erwähnt hast, noch möglich? – Jorge

Verwandte Themen