2

the html

<input type="hidden" name="test[]" id="test" value= "" />

the js

jQuery(document).on('click', '#target', function () {
    var jsArr = ["val1", "val2", "val3"];
    jQuery('input[name^="test[]"]').val(JSON.stringify(jsArr));

});

the php

$json = $_POST['test'];
var_dump($json); 
//array(1) { [0]=> string(43) "[\"val1\",\"val2\",\"val3\"]" } 
var_dump($json[0]);
//string(43) "[\"val1\",\"val2\",\"val3\"]"
var_dump(json_decode($json[0]));
//return NULL

my json string is a valid json format so I don't know why that does not work. Any idea?

3
  • echo json_last_error_msg(); and let's see what PHP doesn't like. Commented Jul 29, 2015 at 22:25
  • var_dump(json_decode($json[0] , true)); Commented Jul 29, 2015 at 22:27
  • or try : $json[0] = str_replace('\"' , '"' , $json[0]); maybe because of " escapes this happens Commented Jul 29, 2015 at 22:28

1 Answer 1

4

You're trying to use stringify on an array. You can't.

It's used for objects. Thus, you should do this :

jQuery(document).on('click', '#target', function () {
    var jsObj = {
        val1: "val1", 
        val2: "val2", 
        val3: "val3"
    };

    jQuery('input[name^="test[]"]').val(JSON.stringify(jsObj));

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

5 Comments

If the point is to send an array to PHP, then there's no point using JSON in that way. The point of using objects is to have name associated properties, while arrays are anonymous in JS. Arrays can be sent to PHP without any form of encoding. It just works as is.
You can use JSON.stringify on arrays. An array can be an array of objects.
@SteveChamaillard if I only use: jQuery('input[name^="test[]"]').val(jsArr); the var_dump give me: array(1) { [0]=> string(14) "val1,val2,val3" }. I can't work with val1,val2,val3 because it's a string.
If this is a simple AJAX POST request where you're trying to send an array to PHP, then you should simply use an AJAX code and you don't need any hidden input for this. If you actually need the hidden input, it shouldn't be filled by JS, but by the client. The only point of using 'name="test[]"' is when you have multiple 'name="test[]"'. Then they'll fill an array and will be sent as an array to PHP. And if you can't do it either way, and still need to do it the way you did, you can simply use php.net/manual/en/function.explode.php
I need the hidden input. I have multiple values for test[]. I am using this code for a wordpress plugin and the hidden input is in a metabox. The js change the value of the hidden input and after I grab this value and save it with the post. I was using explode before but I find more clean to work with arrays. I think you can stringily an array...look at the accepted solution stackoverflow.com/questions/10939840/…

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.