Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form that have several inputs. Some of them are stored in an array. When I use the ajax function to send them to the PHP script, I can get all the values from the inputs but the array object will echo 'Array'.

    $('#Save').click(function(){
        var Name = $('#Name').val();
        var Type = $('select#Type selected:option').attr('value');
        var Values = new Array(1, 2, 4);
        $.ajax({
            url: 'GetValues.php',
            type: 'POST',
            data: {Name: Name, Type: Type, Values: Values}
        });
    });

PHP Script:

$Name = $_POST['Name'];  //echo the names.
$Type = $_POST['Type'];  //echo the type.
$Values = $_POST['Values']; // echo 'Array' ?

I have tried to use JSON_decode but still can't get the values from the form. Any ideas? Thank you

share|improve this question
    
instead of echo, try print_r($Values) and let me know if that prints your array or not.. –  asifrc Jan 17 at 23:41
    
selected:option should be option:selected. But you really should just use $("#Type").val(). –  Barmar Jan 17 at 23:45
add comment

1 Answer

up vote 1 down vote accepted

try

echo var_dump($Values);

you will see the posted values in your array.

share|improve this answer
    
Thank you, with this I can see the array. But how can I now get the values. Should I use a foreach cycle? –  user3163404 Jan 18 at 0:01
    
it depends on what you want to do with your array (inserting into a database, ...). with foreach statement you can access your values. –  أنيس بوهاشم Jan 18 at 0:07
    
Ok it works! Should I use the serialize function in jquery? –  user3163404 Jan 18 at 0:10
    
I see that you are identifying your form fields with ids, if you want to seriliaze the form, you have to distribute names on your fields –  أنيس بوهاشم Jan 18 at 0:16
    
Thank you very much! I added names to my fields an manage to use the serialize() function. But Can I send trough ajax the serialize variable and an an array? –  user3163404 Jan 18 at 0:23
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.