Asked  7 Months ago    Answers:  5   Viewed   29 times

I want to know how i can post a multi-dimensional array?

Basically i want to select a user and selected user will have email and name to sent to post.

So selecting 100 users, will have email and name. I want to get in PHP like following

$_POST['users'] = array(
  array(name, email),
  array(name2, email2),
  array(name3, email3)
);

Any ideas?

 Answers

19

You can name your form elements like this:

<input name="users[1][name]" />
<input name="users[1][email]" />
<input name="users[2][name]" />
<input name="users[2][email]" />
...

You get the idea...

Wednesday, March 31, 2021
 
Lorav
answered 7 Months ago
55

This should be something like this:

function rec($arr, $prefix ="") {
    if ($prefix != "") $prefix .= "/";
    foreach($arr as $e) {
        echo $prefix . $e['name'];
        if (!empty($e['children']))
            rec($e['children'], $prefix . $e['name']);
    }
}

I not on computer so this pseudo code only...

Saturday, May 29, 2021
 
hakre
answered 5 Months ago
80

I agree with @jjclarkson. Just to add, instead of pushing your ids to an array, you can use $.map:

$(".db").live("change", function() {
    $(this).add($(this).next("label")).add($(this).next().next("br")).remove().insertAfter(".db:last + label + br"); 
    var url = "myurl.php";

    var db = $('.db:checked').map(function(i,n) {
        return $(n).val();
    }).get(); //get converts it to an array

    if(db.length == 0) { 
        db = "none"; 
    }       
    $.post(url, {'db[]': db}, function(response) {
        $("#dbdisplay").html(response); 
    });
    return true;
});
Thursday, July 29, 2021
 
Troncoso
answered 3 Months ago
42

If you want $some_array['array_key'] to be an array of values, you have to initialize it as an array, like this:

$some_array['array_key'] = array('some string');

Only then can you use array_push() or the [] = notation:

$some_array['array_key'][] = 'another string';
Tuesday, August 17, 2021
 
pamelus
answered 2 Months ago
92

I believe you're looking for the success in the .ajax options parameter.

$.ajax({
  ...
  success: function(d){
    alert(d);
  }
});
Wednesday, September 1, 2021
 
Raef
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :