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

Open in new window

ASKER CERTIFIED SOLUTION
Mukesh Yadav
Full Stack Developer

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

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 3 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 3 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004