Luey
asked on
pass input array with jquery
I am not really sure how to explain my question. I am trying to pass the values of the inputs "payment_invoices[]" with jquery to my php page. It only passes the one with the value of 53.
<input type="hidden" name="payment_invoices[]" value="53">
<input type="hidden" name="payment_invoices[]" value="54">
<input type="hidden" name="payment_invoices[]" value="55">
$('.payment_button').click(function() { //admin pay affiliate
var payment_invoices = $("input[type=hidden][name='payment_invoices[]']").val(); //This is what I do cannot figure out
$('#affiliate_payment_box').show().html('<img src="/loading_25_black.gif"/>');
var url = "/admin_pay_affiliate.php";
$.post(url, {paymentInvoices: payment_invoices} ,function(data) {
$('#affiliate_payment_box').html(data).show();
})
});
var payment_invoices = $("[name='payment_invoices []']").ser ialize()
Your JavaScript looks good at a quick glance. Are you having trouble with the PHP side or are you getting an error on the JavaScript side?
ASKER
@quizwedge - I have never had to pass an array so far so I am kind of shooting in the dark. I am not getting a javascript error. On the php side i am only getting the first input. The one with the value of 53.
@Catal - I put in serialize() and now it returns this in my php page. payment_invoices%5B%5D=53& payment_in voices%5B% 5D=54&paym ent_invoic es%5B%5D=5 5
I am not sure how to get it into my php array. I am hoping to end up with an array that looks like this. Array ( [0] => 53 [1] => 54 [2] => 55 )
@Catal - I put in serialize() and now it returns this in my php page. payment_invoices%5B%5D=53&
I am not sure how to get it into my php array. I am hoping to end up with an array that looks like this. Array ( [0] => 53 [1] => 54 [2] => 55 )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Cathal
The only thing I am confused about now is extracting what I need with the foreach() loop.
I really just need each invoice number when I loop through the array but I am not having much luck.
When I look at the array with print_r() it looks like this.
Array ( [0] => Array ( [name] => payment_invoices[] [value] => 53 ) [1] => Array ( [name] => payment_invoices[] [value] => 54 ) [2] => Array ( [name] => payment_invoices[] [value] => 55 ) )
But I am still confused on how to extract my invoice number in the loop. Thanks for your help.
The only thing I am confused about now is extracting what I need with the foreach() loop.
I really just need each invoice number when I loop through the array but I am not having much luck.
When I look at the array with print_r() it looks like this.
Array ( [0] => Array ( [name] => payment_invoices[] [value] => 53 ) [1] => Array ( [name] => payment_invoices[] [value] => 54 ) [2] => Array ( [name] => payment_invoices[] [value] => 55 ) )
But I am still confused on how to extract my invoice number in the loop. Thanks for your help.
I'm confused why you are confused
To get the invoice numbers all you need to do is
foreach($_POST['payment_in voices'] as $item){
// value is stored in $item['value'];
}
In the loop $item['value'] will give each value of the posted payment_invoices array - that is 53, 54 and 55
When you do print_r() that is showing the array. The foreach is giving you the values.
To get the invoice numbers all you need to do is
foreach($_POST['payment_in
// value is stored in $item['value'];
}
In the loop $item['value'] will give each value of the posted payment_invoices array - that is 53, 54 and 55
When you do print_r() that is showing the array. The foreach is giving you the values.
You don't need to pass an array to your $.post() function - you pass the data:
var paymentInvoices = $("[name='payment_invoices[]']").serializeArray();
var url = "/admin_pay_affiliate.php";
$.post(url, paymentInvoices ,function(data) {
$('#affiliate_payment_box').html(data).show();
});
Then in PHP, to get the info, you just loop through $_POST['payment_invoices']foreach($_POST['payment_invoices'] as $info):
echo $info;
endforeach;
ASKER
Got it now Cathal,
Thanks for the help.
Duh
<?php
foreach($_POST['paymentInv oices'] as $item){
// value is stored in $item['value'];
echo $item['value'];
}
?>
Thanks for the help.
Duh
<?php
foreach($_POST['paymentInv
// value is stored in $item['value'];
echo $item['value'];
}
?>