AJAX - GET data not being recieved from PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

AJAX - GET data not being recieved from PHP

Post by publicGenome »

Hi Members,
Brief about application:
- Enter anything in field. That gets feed into database using AJAX-post.
- I'd like to display what is being sent to the database using AJAX-get

# I've been able to send data correctly, it's inserted into database.
# When get call is made I'm able to see all the rows pulled up. But somehow I don't enter into the function. I tried with alert.
Have stuck here past couple of hours now.

Code: Select all


$(function(){
		
	$.get('dashboard/xhrGetListings',function(o){ 
//get the data backk..
	alert("hello"); //no messgae is being displayed..
	//
	
	},'json');
	
	$('#randomInsert').submit(function(){ //this function inserts into database
			
 			var url=$(this).attr('action');
			var data=$(this).serialize();
			
			$.post(url,data,  function(o){	
				
 			});

 			return false;
 			});
	}
);

Code: Select all

	
	public function xhrGetListings(){ //function called from AJAX

		$sth=$this->db->prepare('select * from data'); //pull data
		$sth->setFetchMode(PDO::FETCH_ASSOC);
		$sth->execute();
		
		echo json_encode($sth->fetchAll()) ;
//		return $sth->fetchAll();
	}
this prints all the rows pulled in json format. But for some reason I do not get flow back to the $.get in java script. :|

* jQuery JavaScript Library v1.11.3

Neither alert in the ajax-get request helps. :banghead: :banghead:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: AJAX - GET data not being recieved from PHP

Post by Celauran »

And if you exit after sending your JSON payload?
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: AJAX - GET data not being recieved from PHP

Post by publicGenome »

Celauran wrote:And if you exit after sending your JSON payload?
Hi,

Glad to see your reply.
How do I exit it?
In the echo at " select * "or in the java script?
My apologies for being so naïve here. :cry:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: AJAX - GET data not being recieved from PHP

Post by Celauran »

Code: Select all

echo json_encode($stuff);
exit;
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: AJAX - GET data not being recieved from PHP

Post by publicGenome »

Celauran wrote:

Code: Select all

echo json_encode($stuff);
exit;
Thanks for your reply. I added exit, but that didn't help.

My inspect element looks like the attached.

I wonder why I'm not getting an alert. That implies that I'm not being redirected back to the function after pulling the rows. :?
Attachments
inspect.png
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: AJAX - GET data not being recieved from PHP

Post by publicGenome »

Hi Celauran,
I got this function partially working. But in return I get the entire page. JS code looks like:

Code: Select all

		$.get('dashboard/xhrGetListings',function(data){
			
			console.log(data);
			alert(JSON.stringify(data));
		});
		
Model code:

Code: Select all

	public function xhrGetListings(){

		$sth=$this->db->prepare('select * from data');
		$sth->setFetchMode(PDO::FETCH_ASSOC);
		$sth->execute();
		
		echo json_encode($sth->fetchAll()) ;

//		return $sth->fetchAll();
	}
When I alert to show the data pulled, I get entire page apart from the rows selected.
Any help here would be highly appreciated.
Post Reply