Jump to content

Return an array to Ajax success with PHP


Solarpitch

Recommended Posts

Hi Guys,

 

I'm trying the following.. returning an array from a php function to Ajax success but the alert seems to only display undefined.

 

PHP


public function reply()
{
	$item_id = $this->uri->segment(3);
	$type = $this->uri->segment(4);

	if($type == 1){
	$data['get_news_item'] = $this->Newsletter_model->get_news_item($item_id);

	$test = array( "Paul", "Mike");

	return $test;
}
}

 

AJAX



$.ajax({
			type: "POST",
			url: "<?php echo site_url(); ?>newsletter/reply/"+id+"/"+type,
			cache: false,

			success: function (data){


				alert(data[0]); // So this should display "Paul"?



		});

 

[/code]

Link to comment
Share on other sites

Ah ok,

 

So I changed to echo and I seem to be getting something through now. The data is coming through as

 

"Paul, Mike" using alert(eval(data));

 

Is this correct? .. Should I not be able to go alert(data[0]) or similar to get the individual values?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.