duanliu6083 2017-03-28 14:53
浏览 60

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

I am trying to process a form with ajax, jquery and php and I am getting a 404 error, searched before I posted but I did not find the answer to help me fix my problem, this is the .js file :

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

    });
  }

 });
});

and this is the .php file :

<?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 . "
" . $adress1;
        $headers = "From  {$full_name}  " . $dbirth;

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

?>
  • 写回答

1条回答 默认 最新

  • dqj29136 2017-03-28 15:31
    关注

    I suggest you phpmailer. You can download it at 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 . "
    " . $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');
              }
            }
          }   
    ?>  
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题