簡體   English   中英

Ajax Jquery和PHP表單驗證,404錯誤

[英]Ajax Jquery and PHP form validation, 404 error

我正在嘗試使用ajax,jquery和php處理表單,但出現了404錯誤,在發布之前進行了搜索,但沒有找到答案來幫助解決問題,這是.js文件:

$(document).ready(function() {

$('form #error-message').hide();

$('#submit').on('click' , function(e) {
  e.preventDefault();

  var valid = '';
  var required = " is required";
  var fname = $('form #fname').val();
  var lname = $('form #fname').val();
  var dbirth = $('form #dbirth').val();
  var adress1 = $('form #adress1').val();

  if (fname = '' || fname.lenght <= 2) {
    valid += '<p><span>X</span> Full Name ' + required + '</p>';
  }

  if (lname = '' || lname.lenght <= 2) {
    valid += '<p><span>X</span> Full Name ' + required + '</p>';
  }

  if (dbirth = '' || dbirth.lenght <= 2) {
    valid += '<p><span>X</span> Full Name ' + required + '</p>';
  }

  if (adress1 = '' || adress1.lenght <= 5) {
    valid += '<p><span>X</span> Full Name ' + required + '</p>';
  }

  if(valid != '') {
    $('form #error-message').removeClass().addClass('warning')
    .html('<h1>Please complete all the fields</h1>' + valid);
  } else {
    var contactData = $( 'form' ).serialize();
    console.log(contactData);
    $.ajax({
      type: 'POST',
      url: 'ajaxform/process.php',
      data: contactData,
      success: function() {
        $('#error-message').removeClass().addClass('success')
        .html("<h1>Thank you for contacting me. I will reply as soon as i can!</h1>");
      },
      error: function() {
        $('#error-message').removeClass().addClass('alert-error')
        .html("An error has occured. Please try again later")
      },
      complete: function() {
        $('form')[0].reset();
      }

    });
  }

 });
});

這是.php文件:

<?php

sleep(1);
$to = 'yourcompany@gmail.com';
$subject = 'My form contact';

if( isset($_POST['fname']) &&  isset($_POST['lname']) && isset($_POST['dbirth']) && isset($_POST['adress1'])) {
    $fname = trim($_POST['fname']);
    $lname = trim($_POST['lname']);
    $sdate = trim($_POST['dbirth']);
    $adress1 = trim($_POST['adress1']);

    if(!empty($fname) && !empty($lname) && !empty($dbirth) && !empty($adress1)) {

        $full_name = $fname . " " . $lname;
        $body = $full_name . "\n" . $adress1;
        $headers = "From  {$full_name}  " . $dbirth;

        mail($to, $subject, $body, $headers);
    }
} else {
    header('location: ../index.html');
}

?>

我建議你phpmailer。 您可以在此鏈接下載它。

<?php
   require_once('class.phpmailer.php');

    sleep(1);
    $to = 'yourcompany@gmail.com';
    $subject = 'My form contact';

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug = 1; // will send erros and messages
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; //Gmail smtp port
    $mail->Username = "youremail@gmail.com";
    $mail->Password = "yourpassword";
    $mail->SetFrom($headers);
    $mail->Subject = $subject;

    if( isset($_POST['fname']) &&  isset($_POST['lname']) &&  isset($_POST['dbirth']) && isset($_POST['adress1'])) {
      $fname = trim($_POST['fname']);
      $lname = trim($_POST['lname']);
      $sdate = trim($_POST['dbirth']);
      $adress1 = trim($_POST['adress1']);

      if(!empty($fname) && !empty($lname) && !empty($dbirth) && !empty($adress1)) {
          $full_name = $fname . " " . $lname;
          $body = $full_name . "\n" . $adress1;
          $headers = "From  {$full_name}  " . $dbirth;

          $mail->Body = $body;
          $mail->AddAddress($to);

          if(!$mail->Send()) {
           echo "Mailer Error: " . $mail->ErrorInfo;
          } else {
            header('location: ../index.html');
          }
        }
      }   
?>  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM