2

I am sending data from jquery via AJAX Post with all the form fields serialized (In WordPress).

$('#SendEmail').click(function () {
    jQuery.post(
        the_ajax_script.ajaxurl,
        {
            'action': 'the_ajax_hook_SendEmailWithCalculatorContent',
            'calculatorFormContent': $("#CalculatorForm input").serialize()
        },
        function (response_from_the_action_function) {
            $("#G1").val(response_from_the_action_function);                
        }
    );
});

Here are the post paramters from firebug POST:

action  the_ajax_hook_SendEmailWithCalculatorContent
calculatorFormContent   ZIPCodeTextBox=01915&G1=PhoneNumber+%3A++ZipCode+%3A+&G2=&G3=&G4=&G5=&G6=&G7=&G8=&G9=&G10=&G11=&G12=&G13=&G14=&G15=&G16=&G17=&G18=&G19=&G20=&G21=&G22=&G23=&G24=&G25=&G26=&G27=&fullname=&email=&phone=7324211531

Need to access these parameters in PHP, when I try to access them

echo "PhoneNumber : ".$_POST['phone']." ZipCode : ".$_POST['ZIPCodeTextBox']."     IndexValue : ".$_POST['0'];

I get the output as "PhoneNumber : ZipCode : IndexValue : ", so all the form data is coming as blanks.

When I try:

echo $_POST;

i see value "Array" come across. How do I access the array value?

Saw multiple posts here that answer by using the parameter name, one can fetch data from $_POST['name']. (Similar link: jQuery AJAX form data serialize using PHP) But apparently it is not working in my case.

Community
  • 1
  • 1
  • 1
    You're posting `action` and `calculatorFormContent`. Also `echo $_POST` will only give you the type. Try `print_r($_POST)`. – William J. Mar 10 '15 at 19:45
  • 1
    Do a `var_dump($_POST)` you'd access your array with `$_POST['calculatorFormContent']` and `$_POST['action']` – acupofjose Mar 10 '15 at 19:45
  • Thanks. Now I see that I am posting action and calculatorFormContent. @Schultzie : When I echo $_POST['calculatorFormContent'], i get the entire string. Is there a way I can get the ZipCode or Phone value from the string? – Santosh Jami Mar 10 '15 at 20:00
  • @user3921458, yes, within your var_dump it should show you the name of the items you've posted. So if you have an input `` You would access it with (in your case), `$_POST['calculatorFormContent']['input1']`. You can look at this with `var_dump($_POST['calculatorFormContent']['input1'])` which, in the example's case should give you `answers` – acupofjose Mar 10 '15 at 20:14

1 Answers1

0

What about 'calculatorFormContent': JSON.stringify($("#CalculatorForm input").serializeArray()) and then in your PHP script use $array = json_decode($_POST['calculatorFormContent'], true);

Later access it with $array['phone'].

dojs
  • 497
  • 2
  • 11