Voora Tarun
Voora Tarun

Reputation: 1216

Square brackets adding when posting json data using ajax

When i try to post data using ajax , it is adding square brackets to the values. I debugged more than 1 day, but i didn't it. Every thing works fine, but in data base it is storing with brackets Ex: my posted value : jhon In database : [u'jhon']

This is my sample code

    function SignUp() {

                if($("#firstname").val()=="") {
                    alert("All fields are complusory.");
                    return;
                }
                if($("#lastname").val()=="") {
                    alert("All fields are complusory.");
                    return;
                }if($("#emailid").val()=="") {
                alert("All fields are complusory.");
                return;
            }
                if($("#mobileno").val()=="") {
                    alert("All fields are complusory.");
                    return;
                }

                console.log("firstName:  " + $("#firstname").val() );

                var ajax_data = {};
                ajax_data["firstname"]=$("#firstname").val();
                ajax_data["lastname"]=$("#lastname").val();
                ajax_data["emailid"]=$("#emailid").val();
                ajax_data["mobileno"]=$("#mobileno").val();

                $.ajax({
                    type: "POST",
                    url: "signup.php",
                    data :  jQuery.param( ajax_data, true ),
                    traditional: true,
                    dataType: "json",
                    success: function(response) {
                        console.log("response from sign up " + response.status  + " " + response.message);
                         if (response.status == true) {
                             if (response.data.user_id != "-1") {
                             // SignIn($("#mobileno").val());
                             return;
                             }
                         }
                         else
                         alert(response.message);
                    },
                    error:function(){
                        alert("failed");
                    }
                });
            }

 signup.php
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
require_once ("sessionstart.php");

    $service_url = 'http://52.77.213.61/kitchenvilla/v6/user/signup';
    $curl = curl_init ( $service_url );
    $curl_post_data = array (
            "first_name" => $_POST["firstname"],
            "last_name" => $_POST["lastname"],
            "email" => $_POST["emailid"],
            "mobile" => $_POST["mobileno"],
            "reg_id" => "xxxxx",
            "ref_code" => "xxxxx"
    );
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS,$curl_post_data);
    $curl_response = curl_exec ( $curl );
    curl_close ( $curl );
    echo json_encode(( array ) json_decode ( $curl_response, true ));
    ?>

Can any one help me please

Upvotes: 0

Views: 783

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Here are some "tips and hints":
1) While sending POST request via CURL an associative array of 'post_data' should be encoded with http_build_query function:

...
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data));
...

2) json_decode with true passed as second parameter already returns an array, you shouldn't cast it to array twice:

echo json_encode( json_decode( $curl_response, true ) );

Upvotes: 1

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Notice that you are sending JSON:

$.ajax({
    ...
    url: "signup.php", 
    ...
    dataType: "json",
    ...

On the server side (PHP), you need to decode the JSON value you are receiving, using the json_decode() function.

Upvotes: 1

Related Questions