0

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

2
  • instead of echo, try print_r($Values) and let me know if that prints your array or not.. Commented Jan 17, 2014 at 23:41
  • selected:option should be option:selected. But you really should just use $("#Type").val(). Commented Jan 17, 2014 at 23:45

1 Answer 1

1

try

echo var_dump($Values);

you will see the posted values in your array.

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

4 Comments

Thank you, with this I can see the array. But how can I now get the values. Should I use a foreach cycle?
it depends on what you want to do with your array (inserting into a database, ...). with foreach statement you can access your values.
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
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?

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.