I've been struggling for the entire day, I can't seem to get it right to send through a JSON object to a PHP file using the $.ajax function. I eventually got it right to send it through but now it refuses to decode.

//JQuery File

            var user = 
        {
            "email" : document.getElementById('email1').value,
            "password" : document.getElementById('pwd1').value,
            "fname" : document.getElementById('firstname').value,
            "lname" : document.getElementById('lastname').value,
            "gender" : Validator.getGender(),
            "dob" : document.getElementById('dob').value
        }; 
       JSON.stringify(user);

            user = {json:user};

            $.ajax({
                    type: "POST",
                    url: "register.php",
                    dataType: 'json',
                    data: user,
                    success: function(result)
                    {       

                        alert("It worked :D");
                        alert(result);
                    },
                    failure: function()
                    {

                        alert('whoops');
                    }
                });

//PHP File

<?php
    $json = $_REQUEST['json'];
    $json = stripslashes($json);

    $jsonobj = json_decode($json);

    $fname = $jsonobj->fname;
    $lname = $jsonobj->lname;
    $password = $jsonobj->password;
    $email = $jsonobj->email;
    $gender = $jsonobj->gender;
    $dob = $jsonobj->dob;
    echo $gender;

?>

Am I doing something completely silly ? I'm just echoing the gender variable back for now but am actually going to use this to enter it into a database.

Thanks in advance :)

EDIT:

How would it would it work if my php file looked like this:

    session_start();
$db = mysql_connect("localhost", "root");
if(!$db)
{
    die("DB connection failed: " . mysql_error());
}

$db_select = mysql_select_db("tinyspace", $db);
if(!$db_select)
{
    die("DB connection failed: " . mysql_error());
}

$json_string = $_REQUEST["json"];
$jsonobj = json_decode($_REQUEST["json"]);
$fname = $jsonobj -> fname;
$lname = $jsonobj -> lname;
$pwd = $jsonobj -> password;
$email = $jsonobj -> email;
$gender = $jsonobj -> gender;
$dob = $jsonobj -> dob;

$sql("INSERT INTO tinyspace.users (email, password, firstname, lastname, gender, dob) VALUES ('$email','$password','$fname', '$lname','$gender','$dob')");

if (!mysql_query($sql,$db))
{
    die('Error: ' . mysql_error());
}

mysql_close($db);
share|improve this question
Datatype parameter defines the type of data that you're expecting back and not the type you are sending to the server, just remove to let JQuery analyse the server response – sdespont Oct 16 '12 at 17:20
feedback

1 Answer

up vote 0 down vote accepted

The problem is that you are using datatype =json and trying to echo result.So it won't work it will accept only json data

you need to remove this

dataType: 'json',

or echo someting like this

$gender=array();
$gender['gender']=$jsonobj->gender
echo json_encode($gender);

You are also not creating the json

use this

var user = 
        {
            "email" : document.getElementById('email1').value,
            "password" : document.getElementById('pwd1').value,
            "fname" : document.getElementById('firstname').value,
            "lname" : document.getElementById('lastname').value,
            "gender" : Validator.getGender(),
            "dob" : document.getElementById('dob').value
        }; 
      var userjson= JSON.stringify(user);

            user = {json:userjson};

or

data:{'json' :userjson}
share|improve this answer
But in the case where for example I don't want to return anything and just want to add the data to a database, how would I let it know that it was successful ? – Crossman Oct 16 '12 at 17:22
Yeah, just don't specify the datatype; $.ajax() is good enough at determining the type itself. (This is why I usually use $.post(), which uses the right defaults for 99% of cases). – sgroves Oct 16 '12 at 17:23
@Crossman you can use dataType: 'html' or you can use proper encoding serverside. – jhonraymos Oct 16 '12 at 17:24
Sorry edited the question after the post was edited. – Crossman Oct 16 '12 at 17:26
You also shouldn't need the JSON.stringify(user); or user = {json:user};. IIRC $.ajax() can handle objects as parameter values just fine. – sgroves Oct 16 '12 at 17:26
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.