troubleshooting Question

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

Avatar of evibesmusic
evibesmusicFlag for United States of America asked on
JavaScriptAJAXjQuery
3 Comments1 Solution454 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Mukesh Yadav
Full Stack Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros