Solved

pass input array with jquery

Posted on 2014-02-25
8
654 Views
Last Modified: 2014-02-26
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();        
	     })

   	
});

Open in new window

0
Comment
Question by:Luey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39887557
var payment_invoices = $("[name='payment_invoices[]']").serialize()
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 39887575
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?
0
 

Author Comment

by:Luey
ID: 39887616
@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_invoices%5B%5D=54&payment_invoices%5B%5D=55

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 )
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39887642
Ok use

payment_invoices = $("[name='payment_invoices']").serializeArray()


On your php page loop thru the array like so.

foreach($_POST['payment_invoices'] as $item){
      // value is stored in $item['value'];
}
0
 

Author Comment

by:Luey
ID: 39887675
@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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39887825
I'm confused why you are confused
To get the invoice numbers all you need to do is

foreach($_POST['payment_invoices'] 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.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39888600
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();
});

Open in new window

Then in PHP, to get the info, you just loop through $_POST['payment_invoices']

foreach($_POST['payment_invoices'] as $info):
	echo $info;
endforeach;

Open in new window

0
 

Author Closing Comment

by:Luey
ID: 39889261
Got it now Cathal,
Thanks for the help.

Duh
<?php
 foreach($_POST['paymentInvoices'] as $item){
      // value is stored in $item['value'];
        echo $item['value'];
}
?>
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question