0

I have the following code:

    var data_str = $('form').serialize();
    alert(data_str);
    $("#SerializeTXT").text(data_str).show();
    $.ajax( {
        type: 'POST',
        url: 'test.php',
        data: data_str,
        success: function(data) {
            $('#result').html(data);
        }           
   });

Here is my test.php and the result:

<?php print_r($_POST);?>

in the #result i get

Array ( [itemIDhidden] => 2640 [SelectQt] => 1 [Bread] => Black Bread_0 [Cheese] => American_0 [Toppings] => Bacon_0 [Description] => TWSTE 3 45 T4 )

In the SerializeTXT I get

itemIDhidden=2640&SelectQt=1&Bread=Black+Bread_0&Cheese=American_0&Toppings=Sauteed+Mushrooms_0&Toppings=Fried+Onions_0&Toppings=Bacon_0&Description=TWSTE+3+45+T4

You can see that the post gets only the last element of the multiple selected element. In SerializeTXT div i get exactly what is selected from the form.

Any ideas and how can I obtain all those parameters in the php file?

Thank you in advance.

0

2 Answers 2

3

Change your select element's name from Toppings to Toppings[]

<select name="Toppings" ...

to

<select name="Toppings[]" ...

Then $_POST['Toppings'] will be an array.

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

2 Comments

Thank you for your answer, but those select fields are created by the code and I do not know their names. Also, It did not work.
@Obichamte2 If the code generated a multiple select without using [] in name, then the code is wrong.
0
    var form = $('form');
    var data_str = '';
    form.find('input, select, textarea').each(function()
        {
            data_str += $(this).attr('name')+'='+$(this).val()+'&';
        });
    $.ajax( {
        type: 'POST',
        url: 'test.php',
        data: data_str,
        success: function(data) {
            $('#result').html(data);
        }           
   });
    };

test.php print_r($_POST); returns

Array ( [itemIDhidden] => 2643 [SelectQt] => 1 [Bread] => Multi-Grain Bun_0 [Cheese] => American_0,Swiss_0 [Toppings] => Fried Onions_0,Bacon_0,Raw Onion_0 [Description] => TEST TEST TEST [CancelItemForm] => Cancel [BasketItem] => Confirm ).

This array can be easily manipulated in the PHP file. I hope this helps to some of the people outside

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.