Ajax Jquery和PHP表单验证,404错误

时间:2017-03-28 14:53:37

标签: javascript php jquery ajax

我正在尝试使用ajax,jquery和php处理表单,我收到404错误,在我发布之前搜索但是我找不到帮助我解决问题的答案,这是.js文件:< / p>

$(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');
}

?>

1 个答案:

答案 0 :(得分:0)

我建议你phpmailer。您可以在this link下载。

<?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');
          }
        }
      }   
?>