Reputation: 199
I'm getting this error in php:
Invalid argument supplied for foreach()
I am tring to create an array in jquery for the input values
<input type="text" class="passenger">
<input type="text" class="passenger">
<input type="text" class="passenger">
Then the js is :
var passengers = new Array();
$('.passenger').each(function(){
if($(this).val() == " "){
passengers.push(JSON.stringify($(this).val()));
}
});
Then after i post it to php, i am wanting it so i displays all the passengers
at the moment, this is not working. Why not
foreach($passengers as $pass){
$each_passengers = json_decode($pass);
}
Upvotes: 1
Views: 1777
Reputation: 41885
Actually you don't need to stringify each input. What you need to do first, is create a grouping name attribute in your form inputs:
<input type="text" name="input[]" class="passenger" value="test1" />
<input type="text" name="input[]" class="passenger" value="test2" />
<input type="text" name="input[]" class="passenger" value="test3" />
Then, in your $.post()
. Just feed them as serialized using $(form).serialize()
:
$.post('myphp_process.php', $('#form_values').serialize(), function(response){
Then of course the PHP that will process it, just treat it as normal POST:
$inputs = $_POST['input']; // this will return an array of inputs and to access this you need a loop.
// for clarity print_r($_POST['input']);
Check this out. This should give you the basic idea.
Upvotes: 2