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
asked Jan 17, 2014 at 23:37
user3163404
3591 gold badge4 silver badges16 bronze badges
1 Answer 1
try
echo var_dump($Values);
you will see the posted values in your array.
answered Jan 17, 2014 at 23:43
أنيس بوهاشم
9407 silver badges6 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
user3163404
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
user3163404
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?
lang-js
print_r($Values)and let me know if that prints your array or not..selected:optionshould beoption:selected. But you really should just use$("#Type").val().