javascript - jquery serializeArray() wordpress -
i trying submit form wordpress page php script resides in same directory index.php using custom html option on page generate form , using jquery serializearray() send data php script.
in console, when submit jquery("#formid").serializearray();
returns object stores name , value of each of inputs.
however, when use jquery("resultdiv").load("phpscript.php", jquery("#formid").serializearray());
php $_post
variable empty.
i've used method in past , has worked. reason, it's not working in case. else have issue? here's script looks like:
<script type="text/javascript"> //need validate 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 not match. please re-enter."); jquery("#sub_email").focus(); jquery("#sub_email").select(); } else { if (type_reseller=="") { alert("please enter reseller type"); } else { if (province=="--") { alert("please select province or state"); } else { if (country_from == "") { alert("please select country from"); jquery("#sub_country").focus(); jquery("#sub_country").select(); } else { if ((password!=pw_verify) || (password=="")) { alert("your password entry not match. please re-enter"); jquery("#password").focus(); jquery("#password").select(); } else { jquery("#submitresult").html("<h2>sending form...</h2>"); //submit form database jquery("#submitresult").load("../subscribesubmit.php", jquery("#subscribe").serializearray()); } } } } } }); //--> //need submit form using ajax </script>
console entry of json.stringify(jquery("#subscribe").serializearray())
produces output...
"[{"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 comment"},{"name":"form_submitted","value":"true"}]"
when used print_r($_post)
on destination script see script receiving returned array() (empty array)
.
the network response looks this:
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, gecko) chrome/59.0.3071.115 safari/537.36 x-requested-with:xmlhttprequest
any insight helpful.
the short answer: use jquery.serialize()
instead of jquery.serializearray()
.
i think problem serialized data not in form load
function expecting. request therefore not sending data (note line content-length:0
in request headers). documentation load
not format of data passed in until end. relevant section reads:
$( "#objectid" ).load( "test.php", { "choices[]": [ "jon", "susan" ] } );
a better section of documentation page jquery.ajax. section there under sending data server reads:
the data option can contain either query string of form
key1=value1&key2=value2
, or object of form{key1: 'value1', key2: 'value2'}
. if latter form used, data converted query string usingjquery.param()
before sent.
note data in form [{name: 'key1', value: 'value1'}]
instead of form {key1: 'value1'}
. re-map array of values object of proper form, seems simpler use direct method of serialize()
key1=value1
string.
Comments
Post a Comment