【问题标题】:php POST metod not working with jquery Ajax or without Ajaxphp POST 方法不适用于 jquery Ajax 或没有 Ajax
【发布时间】:2017-02-07 12:37:44
【问题描述】:

我在 stackoverflow 中搜索了这个问题,发现的答案很少,但没有一个对我有帮助。所以我再次发布它。我在本地主机中使用 post 方法的 php 表单工作正常,但相同的代码在服务器(linux 服务器)中不起作用我不知道实际问题是什么。请帮助

  1. 我要提交的表格
<form class="forma" id="forma" method="POST" action="contact-form.php" name="myForm" autocomplete="off">
  <div class="formWrapper">

    <div class="group">
      <input type="text" name="name" id="name">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Name</label>
      <p class="error-red"></p>

    </div>

    <div class="group">
      <input type="text" name="eMail" id="eMail">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Email</label>
      <p class="error-red"></p>
    </div>

    <div class="group">
      <input type="text" name="mobileNo" id="mobileNo">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Mobile no.</label>
      <p class="error-red"></p>
    </div>

    <div class="group">
      <input type="text" name="message" id="message">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Message</label>

    </div>

    <div class="captchaWrapper">
      <img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimgCont" style="margin-right: 14px;" alt="captcha">


      <a href="javascript: refreshCaptcha();" class="clickTorefreshAnchor">
        <i class="fa fa-refresh clickTorefresh" id="caprefresh">

                                        </i>
      </a>

      <div class="captchaInputWrapper group">
        <input type="text" id="6_letters_code" name="captcha">
        <span class="highlight"></span>
        <span class="bar"></span>
        <label>Enter the code here:</label>
        <p class="error-red"></p>

      </div>
    </div>


    <div class="submit submitBtn" id="consubmit">
      <button type="submit" name="sendmail" id="sendmail" class="sendMailBtn">Submit
      </button>
    </div>

  </div>

  <div class="clear"></div>

</form>
  1. 我的 jquery ajax 代码
jQuery(document).ready(function() {
  jQuery("#forma").submit(function(e) {

    e.preventDefault();
    e.stopImmediatePropagation();

    jQuery('#sendmail').html('Please wait...');

    jQuery.ajax({
      type: "post",
      url: './contact-form.php',
      data: jQuery("#forma").serialize(),
      dataType: "json",
      success: function(data) {

        jQuery('p.error-red').html('');


        if ('success' == data.result) {


          //jQuery("#forma")[0].reset();

          //jQuery('#response').append('<p class="successMessage">' + data.msg + '</p>');
          //  jQuery('#sendmail').html('Submit');
          window.location.href = "https://www.mrpmediaworks.com/thanks.php";

        }

        if ('error' == data.result) {

          var arr = data.msg;
          var element_arr = data.fieldname;
          count = '0';
          jQuery.each(arr, function(index, value) {
            if (value.length != 0) {

              jQuery('input[name=' + element_arr[count] + ']').parent('.group').find('p.error-red').html(value);
            }
            count++;
          });
          jQuery('#sendmail').html('Submit');

        }

      }
    });

    return false;
  });
});
  1. 我的php代码

<?php
      session_start();
      require_once('config.php');

      $email = '';
      $fieldname = array();
      $msg = array();
      $result = '';
      $count = '';
      $name =  $_Post["name"];
      $contact =  $_Post["mobileNo"];
      $email =  $_Post["eMail"];
      $msg_query =  $_Post["message"];
      $captcha =  $_Post["captcha"];

      echo $name;
      die();
?>

只是测试代码

相同的代码适用于 get

我已尝试从表单中删除操作,并可能进行组合,但注意到有效。 编辑后

      4. .htaccess file
                # browser requests PHP
            RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
            RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

           # check to see if the request is for a PHP file:
           RewriteCond %{REQUEST_FILENAME}\.php -f
           RewriteRule ^/?(.*)$ /$1.php [L]

这是我的 htaccess 文件中唯一的内容

【问题讨论】:

  • 你的表单在哪里,我在你的代码中看不到。
  • @AnkitShah 并不是真正的重复。他需要返回具有 data.result 属性的 json
  • 我没有发布完整的 php 代码,但它有 echo json_encode($result);但它不工作。我想说我无法将数据从表单传递到此处命名为 contact-form.php 的 php 页面。

标签: php jquery ajax post server


【解决方案1】:

只需创建一个函数来处理 ajax 请求。然后在函数内部处理请求。为此,

从表单中删除操作。并将请求发送到 on submit 上的函数,请参见以下示例。

 <form class="forma" id="forma" method="POST"  action="javascript:void(0);" name="myForm" autocomplete="off"  onsubmit="sendContactRequest();">

然后 ~sendContactRequest()~ 将处理您的 ajax 请求。

function sendContactRequest(){
        jQuery('#sendmail').html('Please wait...');
        $.post("contact-form.php",jQuery("#forma").serialize(),function(result,status,xhr) { 
            if( status.toLowerCase()=="error".toLowerCase() ) { 
                alert("An Error Occurred.."); // or do something that you can handle the error request.
            }else {  //success response
            }
        });
    }

请试试这个。在我的情况下,它工作得很好。

【讨论】:

    【解决方案2】:

    我认为你搞砸了你的 htaccess 文件。如果可能,请在此处粘贴您的 .htaccess 文件代码。

    编辑我的答案

    Bingo 发现您正在从文件中删除 .php 扩展名的问题,但在 ajax 请求中您已将数据发布到 contact-form.php 。尝试从您的 ajax 请求中删除 .php 扩展名。尝试一下,希望对您有所帮助。

    【讨论】:

    • 我已经添加了我的 htaccess 代码,请查看并告诉我其中有什么问题。希望你能帮忙
    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2015-06-03
    • 2015-01-11
    相关资源
    最近更新 更多