0

This is my AJAX request

$(document).ready(function(){
        $("#register-submit").click(function(){
        var formdata = {hotelname: $('#hotelName').val(), contactType: $('#contactType').val(), contactNumber: $('#contactNumber').val(), addrOne: $('#addrLineOne').val(), addrTwo: $('#addrLineTwo').val(),cityName: $('#cityName').val(), stateName: $('#stateName').val(),localityName: $('#localityName').val(), pincode: $('#pincode').val(), managerName: $('#mngrName').val(), managerEmail: $('#mngrEmail').val(), managerPhone: $('#mngrPhone').val()}
        jQuery.ajax({
                url: "../api/v1/admin/ch_partialBusinessRegister.php",
                data: JSON.stringify(formdata),
                type: "POST",
                async: false,
                success: function(res) {
                    var result = res.Result;
                    if(result.success === true)
                        {
                            alert("Random Password is Generated : "+res.Result.password);
                            window.location.reload();
                        }
                        else
                            alert("Registration Failed. "+res.Result.msg);
                }
            });

    });
});

In my ch_partialBusinessRegister.php

$inputJson = file_get_contents('php://input');
$post_vars = json_decode($inputJson, true);
$businessName = $post_vars['hotelname'];
$address1 = $post_vars['addrOne'];
$address2 = $post_vars['addrTwo'];
$locality = $post_vars['localityName'];
$city = $post_vars['cityName'];
$state = $post_vars['stateName'];
$zip = $post_vars['pincode'];
$mName = $post_vars['managerName'];
$mEmail = $post_vars['managerEmail'];
$mPhone = $post_vars['managerPhone'];

Am able to get the data and pass it in the POST parameters successfully. But when I change the formdata as

$(document).ready(function(){
            $("#register-submit").click(function(){
            var formdata = $('form').serializeArray();
            jQuery.ajax({
                    url: "../api/v1/admin/ch_partialBusinessRegister.php",
                    data: JSON.stringify(formdata),
                    type: "POST",
                    async: false,
                    success: function(res) {
                        var result = res.Result;
                        if(result.success === true)
                            {
                                alert("Random Password is Generated : "+res.Result.password);
                                window.location.reload();
                            }
                            else
                                alert("Registration Failed. "+res.Result.msg);
                    }
                });

        });
    });

and then JSON.stringify it, it is sending the variables through POST parameters like this

[{"name":"hotelName","value":"test"},{"name":"contactType","value":"LandLine"},{"name":"contactNumber[]","value":""},{"name":"AddrOne","value":"test"},{"name":"addrTwo","value":"test"},{"name":"pincode","value":"test"},{"name":"mngrName","value":"test"},{"name":"mngrEmail","value":"test"},{"name":"mngrPhone","value":"test"}]

But in PHP it is not reading the values.

Same way if I use

var mydata = $('form').serialize();
 data: JSON.stringify(mydata)

It Posts this data, but not read by PHP

"hotelName=&contactType=LandLine&contactNumber%5B%5D=&AddrOne=&addrTwo=&pincode=&mngrName=test&mngrEmail=test&mngrPhone=test"
7
  • $('form').serialize(); ??? Commented May 11, 2014 at 13:14
  • Yes, I am just serializing the HTML form through jQuery Commented May 11, 2014 at 13:15
  • But here you are using serializeArray() so why not instead use serialize() and use relevant name attribute for each input? Commented May 11, 2014 at 13:18
  • @A.Wolff updated question with serialize() and its POST data. Serialize and serializeArray both are not working for me Commented May 11, 2014 at 13:21
  • hotelName != hotelname start to fix it and the same for all your attributes name Commented May 11, 2014 at 13:24

2 Answers 2

1

you can just add this

$(document).ready(function(){
        $("#register-submit").click(function(){
           jQuery.ajax({
           url: "../api/v1/admin/ch_partialBusinessRegister.php",
           type: 'post',
          data: {'inputJson': $('form').serialize()},
          dataType: 'json',
          success: function(res) {
                    var result = res.Result;
                    if(result.success === true)
                        {
                            alert("Random Password is Generated "+res.Result.password);
                            window.location.reload();
                        }
                        else
                            alert("Registration Failed. "+res.Result.msg);
                }
            });

    });
});

and in your php file you can just decode the array by typing

$inputJson=$_POST['inputJson'];
$post_vars = json_decode($inputJson, true);

and if you want to get the value of form tags

you can simply write

$tagValue=$post_vars['tagName']; // tag name inside the form which is sent using ajax
Sign up to request clarification or add additional context in comments.

2 Comments

Even if I directly use $_POST to get the value from form tags, It says Undefined Index in PHP. Its passing the Post values, but PHP is not reading it.
sorry , i have made a mistake , there is no post like this $tagValue=$_POST['tagName']; instead you should write $tagValue=$post_vars['tagName']; i will edit the code right now
0

just pass your 'myData' object as it is. don't stringify it.

2 Comments

In that case how to parse it in PHP? I have did a json_decode in php too
I don't know PHP, but jquery takes this object, and send it as a [key=value] pairs to the server, so my guess is that you should handle it as you would usually read request's parameters in PHP... nothing special here...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.