1

I need send via AJAX data like the following:

<tr>
<td><input type="textbox" value="12" name="delay[256]"></td>
<td><input type="checkbox" value="256" name="attach_to[]"></td>
</tr>                       
<tr>
<td><input type="textbox" value="7" name="delay[653]"></td>
<td><input type="checkbox" value="653" name="attach_to[]"></td>
</tr>

Which is the best way to encode these 2 types of input?

$.post(
window.location.href,
{
    data_ajax: 1,
    attach_to : attach_to, // how encode?
    delay: delay // how encode?

},
function(array) {
},
'json'
);

OBS: I only want send these data and not the entire form. So .serialize() seems not adequate.

2 Answers 2

0

See this answer to learn how to encode the data as JSON: Encoding Javascript Object to Json string

And then read the posted JSON variables back into an array with PHP: http://php.net/manual/en/function.json-decode.php

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

1 Comment

I don't think is a good idea send json (JavaScript Object Notation) code to php. The opposite make more sense (echo json_encode($output); -> js). But thanks!
0

My solution:

js code: serialize() works over an entire form or given elements.

var attach_to = $("input[name='attach_to[]']").serialize();
var delay = $("input[name^='delay[']").serialize();
$.post(
    window.location.href,
    {
        attach_to : attach_to,
        delay: delay
    },
    function(array) {
        // ...
    },
    'json'
    );

php code: parse_str is the key to easily decode incoming vars.

parse_str($_REQUEST['delay'], $params);
$delay = $params['delay'];
parse_str($_REQUEST['attach_to'], $params);
$attach_to = $params['attach_to'];

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.