Avatar of evibesmusic
evibesmusicFlag for United States of America

asked on 

How to send a javascript array via AJAX (jquery)?

Experts,

I feel like a dummy but, I can't figure out the correct syntax to send an array of email addresses via AJAX?

My current script sends all of the email address in one long text string vs. a format that I can actually use on the back end?

What syntax do I need to use to have the values sent in an array vs. a long string?

String being sent by my script:
PARAMS
addresses: joe.blow@mysite.comjohn.doe@mysite.comsally.smith@kp.org

I'm expecting the array to be sent like this:
PARAMS
addresses[]:
joe.blow@mysite.com
john.doe@mysite.com
sally.smith@kp.org

//SEND EMAIL WHEN SEND BUTTON IS PRESSED
$("#send_email").click(function(){
	if(confirm('Press "OK" to send email.\n\nPress "Cancel" to continue without sending email.')){
		var clinician_array = [];
		var clinician_text = '';
		$(".clinician_checkbox").each(function(){
			if($(this).prop('checked')==true){
				clinician_array.push($(this).attr('id'));
			}
		});
		var clinician_array_length = clinician_array.length;
		for(i=0; i < clinician_array_length; i++){
			clinician_text+= clinician_array[i];
		}//END FOR LOOP
		//SEND EMAIL VIA AJAX
		$.ajax({
			method: 'POST',
			url: 'scripts/email.php?args=email',
			data: {
				addresses: clinician_text	
			}
		}).done(function(){
			alert('Data sent');
		}).fail(function(){
			alert('Error');
		});//END AJAX
	}//END IF
});//END #send_email FUNCTION

Open in new window

JavaScriptAJAXjQuery

Avatar of undefined
Last Comment
Julian Hansen
ASKER CERTIFIED SOLUTION
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of evibesmusic
evibesmusic
Flag of United States of America image

ASKER

Thanks...that was painfully obvious...if I don't want the values as a string...don't turn them into a string.

Cheers.
Interested to know - if you are getting values from a form - why not just serialize the form?

Also if you want the checked checkboxes your selector could be

$(".clinician_checkbox:checked").each(function(){
    clinician_array.push(this.id);
});

Open in new window


Serialise array

$clinician_array = $('formselector').serialize();
$.ajax({
   url: 'scripts/email.php?args=email',
   method: 'POST',
   data: $clinician_array
}).done(...

Open in new window

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo