3

I am new to JSON.

In JS, I create an array of values like so:

var arrFields = $("td>.frmInput").map(function(){
    return  {
        id: this.id,
        value: $(this).val()
    };
}).get();

I then AJAX them to the server like so:

$.ajax({
    type: "POST",
    url: "ajax/ax_all_ajax_fns.php",
    data: "Fields=" +JSON.stringify(arrFields),
    success: function(recd) {
        alert(recd);
    }
});

Note that there is a mixture of strings, plus the JSON.stringified (?) array. (There are additional string values sent, so data must remain as string.)

On the PHP side, I need to turn the received Fields string into an associative array.

Doing this:

$jsonAsStr_Fields = $_POST['Fields'];
die($jsonAsStr_Fields);

Returns this text string in the alert():

[{"id":"rateDriver","value":"Jacques Villeneuve"},{"id":"rateCar","value":"Chev"}]

Doing this:

$arrFields = json_decode($jsonAsStr_Fields, TRUE);
$driver = $arrFields['rateDriver'];
$car = $arrFields['rateCar'];
$tire = $arrFields['rateTire'];
die('Driver: [' .$driver. '] Car: [' .$car. ']  Tire: [' .$tire. ']');

Returns this:

Driver: [ ]  Car: [ ]  Tire: [ ]

How can I turn the $jsonAsStr_Fields string into an assoc array, and thereby output the correct values to my alert?

3 Answers 3

1

Do this instead for your creation of values:

var arrFields = {};
$("td>.frmInput").each(function(){
    arrFields[this.id] = $(this).val();
});

This will create an object, when JSON-stringified, that looks like this:

{"rateDriver":"Jacques Villeneuve", "rateCar":"Chev"}

Which seems to be the format you want to use in your PHP code.

Sign up to request clarification or add additional context in comments.

3 Comments

Very helpful. +1 I see now how I should have created the values. However, on the PHP side it is still not resulting in an assoc array. The line $arrFields = json_decode($jsonAsStr_Fields, TRUE); still outputs: Driver: [ ] Car: [ ] Tire: [ ]
The final problem was that the stringified data was arriving at the PHP side with all its quote marks escaped, like this: {\"rateDriver\":\"Jacques Villeneuve\", \"rateCar\":\"Chev\"} -- which I thought was happening on the way BACK (into the Alert). Adding a PHP str_replace $jsonAsStr_Fields = str_replace("\\", "", $_POST['Fields']); SOLVED THE PROBLEM. Your tip put me on the right track. Thanks.
What version of PHP are you running? Magic quotes has been deprecated since 5.3: php.net/manual/en/security.magicquotes.php
1

You have an array of associative arrays and your arrays don't have the specified props, rateDriver for example is the value of the first array's element's id:

$driver = $arrFields[0]['id'];
$car = $arrFields[1]['id'];

For seeing the array's contents you use the famous var_dump function.

1 Comment

Thanks. You have clearly explained my mistake. +1 Now to understand how to refactor the code to arrive at the solution.
0

From the Author:

For those who haven't fully understood what solved this problem.

The underlying problem was that the stringified JSON was being modified en route (immed after hit Submit button en route to the PHP side) by the AJAX. All quote marks were being escaped, which made it impossible for that string to work with json_encode.

This was discovered by grabbing the value of the received data once it hit the PHP side:

$jsonAsStr_Fields = $_POST['Fields'];
die($jsonAsStr_Fields);

And alerting the received data in the AJAX success function:

success: function(recd) {
    alert(recd);
}

Both of the above were described in the OP.

However, because I assumed this was an unrelated problem, I "fixed" the string displayed in the alert() box when I posted the question. Lesson to be learned: don't help - just post what you actually see.

It really displayed like this:

{\"id\":\"rateDriver\",\"value\":\"Jacques Villeneuve\"}

but I wrote that it displayed like this:

{"id":"rateDriver","value":"Jacques Villeneuve"}

Of course, the json_decode() PHP function had no idea what to do with the backslashes, so the string did not convert.

Solution was to use str_replace() on the received JSON string over on the PHP side, to resolve the problem, like this:

str_replace("\\", "", $_POST['Fields']);

Comments

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.