0

I have a jQuery object which is appended to form field like,

var params = {
    'product_id': productId,
    'width': width,
    'drop': drop
};

$("#sample-form input[name='params']").val(params);

Form looks like,

<form id="sample-form" action="url" method="post">
    <input type="hidden" name="params"/>
    <button type="submit">Submit</button>
</form>

On receiving this "params" using POST data in PHP, I get string with value "[object Object]"

How do I convert this into array?

2
  • 2
    You could use JSON. ...val(JSON.stringify(params)). Then deserialise that value when sent to your PHP Commented Jun 24, 2019 at 13:32
  • 1
    Store it as stringified json val(JSON.stringify(params));. Then in PHP, you do: $params = json_decode($_POST['params'], true); to get it as a PHP array. Commented Jun 24, 2019 at 13:38

1 Answer 1

1

I converted jQuery object into stringified JSON as,

$("#sample-form input[name='params']").val(JSON.stringify(params));

In PHP, convert received JSON into PHP array as,

$params = json_decode($_POST['params'], true);
Sign up to request clarification or add additional context in comments.

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.