jquery serializeArray()wordpress

时间:2017-07-25 17:51:41

标签: javascript php jquery wordpress

我正在尝试将一个表单从Wordpress页面提交到一个php脚本,该脚本与index.php位于同一目录中,使用页面上的自定义html选项生成表单并使用jQuery serializeArray()将数据发送到php脚本。

在控制台中,当我提交jQuery("#formID").serializeArray();时,它返回一个对象,该对象存储每个输入的名称和值。

但是,当我使用jQuery("resultDiv").load("phpScript.php", jQuery("#formID").serializeArray());时,PHP $_POST变量为空。

我过去曾使用过这种方法,但它已经奏效了。出于某种原因,它在这种情况下不起作用。其他人有这个问题吗?这是脚本的样子:

<script type="text/javascript">
//need to validate the form
jQuery("#sendForm").click(function() {
  var email = jQuery("#sub_email").val();
  var email2 = jQuery("#sub_verify_email").val();
  var type_reseller = jQuery("#sub_type").val();
  var province = jQuery("#sub_prov").val();
  var country_from = jQuery("#sub_country").val();
  var password = jQuery("#password").val();
  var pw_verify = jQuery("#pw_verify").val();

  if ( (email!=email2) || (email=="") )

  {
    alert("Email addresses do not match. Please re-enter.");
    jQuery("#sub_email").focus();
    jQuery("#sub_email").select();
  }


  else
  {

    if (type_reseller=="")
    {
      alert("Please enter a Reseller Type");
    }

    else
    {
      if  (province=="--")
      {
        alert("Please select a Province or State");
      }
      else
      {
        if  (country_from == "")
        {
          alert("Please select which country you are from");
          jQuery("#sub_country").focus();
          jQuery("#sub_country").select();
        }

        else
        {
          if  ((password!=pw_verify) || (password==""))
          {
            alert("Your Password entry does not match. Please re-enter");
            jQuery("#password").focus();
            jQuery("#password").select();
          }

          else
          {
            jQuery("#submitResult").html("<h2>Sending Form...</h2>");
            //submit the form to the database
            jQuery("#submitResult").load("../subscribeSubmit.php", jQuery("#Subscribe").serializeArray());
          }
        }
      }
    }
  }
});
//-->

//need to submit the form using ajax


</script>

JSON.stringify(jQuery("#Subscribe").serializeArray())的控制台条目会生成此输出...

   "[{"name":"sub_conf","value":"Yes"},{"name":"sub_conf2","value":"Yes"},{"name":"sub_type","value":"Service Provider"},{"name":"sub_firstname","value":"hello"},{"name":"sub_lastname","value":"world"},{"name":"sub_company","value":"anycompany"},{"name":"sub_city","value":"anycity"},{"name":"sub_prov","value":"ON"},{"name":"sub_country","value":"Canada"},{"name":"sub_country","value":""},{"name":"sub_email","value":"anyone@anyplace.com"},{"name":"sub_verify_email","value":"anyone@anyplace.com"},{"name":"password","value":"hello"},{"name":"pw_verify","value":"hello"},{"name":"nl_version","value":"H"},{"name":"sub_comments","value":"this is a comment"},{"name":"form_submitted","value":"TRUE"}]"

当我在目标脚本上使用print_r($_POST)来查看脚本收到的内容时,它返回Array() (empty array)

网络响应如下:

General:
Request URL:http://example.com/subscribeSubmit.php
Request Method:POST
Status Code:200 OK
Remote Address:222.222.222.222:80
Referrer Policy:no-referrer-when-downgrade

Response Header:
Connection:keep-alive
Content-Type:text/html; charset=UTF-8
Date:Tue, 25 Jul 2017 18:04:51 GMT
Server:nginx
Transfer-Encoding:chunked
X-Powered-By:PHP/7.1.7
X-Powered-By:PleskLin

Request Headers
Accept:text/html, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Cookie:pum-7361=true
Host:example.com
Origin:http://example.com
Referer:http://example.com/subscribe/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
X-Requested-With:XMLHttpRequest

任何见解都会有所帮助。

2 个答案:

答案 0 :(得分:0)

简短回答:使用jQuery.serialize()代替jQuery.serializeArray()

我认为问题是序列化数据不是load函数所期望的形式。因此,请求不会发送任何数据(请注意请求标头中的行Content-Length:0)。 documentation for load并没有说明直到结束时传入的数据格式。相关部分如下:

$( "#objectID" ).load( "test.php", { "choices[]": [ "Jon", "Susan" ] } );

文档的更好部分是jQuery.ajax的页面。 向服务器发送数据下的部分为:

  

数据选项可以包含key1=value1&key2=value2形式的查询字符串,也可以包含{key1: 'value1', key2: 'value2'}形式的对象。如果使用后一种形式,则在发送之前使用jQuery.param()将数据转换为查询字符串。

请注意,您的数据采用[{name: 'key1', value: 'value1'}]形式,而不是{key1: 'value1'}形式。您可以将值数组重新映射到正确形式的对象中,但使用serialize()的直接方法获取key1=value1字符串似乎要简单得多。

答案 1 :(得分:0)

我想出了问题...当我用'正在提交表单'替换表单时,表单的ID已被清除,因此,没有表单数据要发送...我删除了该行,一切正常。

相关问题