Unable to get data back from PHP to AJAX

A

Anonymous

Guest
Hi Members,
This is my first post to this forum kindly bear with me if I've violated any posting rules.

In my application, I've a form. User enters, and hit submit - data is inserted into table. Insertion is being done using AJAX.
Next I'd like to show the inserted data using AJAX GET function. However, I do not get anything back. Neither alert inside it works.

My form code:

Code:
<form id="randomInsert" action="<?php echo URL;?>dashboard/xhrInsert" method="post">
<input type="text" id="name_form" name="name_text"/>
<input type="submit" />

</form>

My AJAX code:
Code:
$(function(){
		
	$.get('dashboard/xhrGetListings',function(o){ //data gets pulled in model
		alert("Back here"); // no alert..
	},'json');
	
	$('#randomInsert').submit(function(){ //this works
			
			var url=$(this).attr('action');
			var data=$(this).serialize();
			
			$.post(url,data, function(o){	

			});

			return false;
			});
	}
);

Code to pull rows entered:
Code:
	public function xhrGetListings(){

		$sth=$this->db->prepare('select * from data');
		$sth->setFetchMode(PDO::FETCH_ASSOC);
		$sth->execute();
		
		echo json_encode($sth->fetchAll()) ;
		exit ; //to see if this would help.. this doesn't though
//		return $sth->fetchAll();
	}
jQuery JavaScript Library v1.11.3
I can see data is being pulled in my model (JSON encoded), but for some mysterious reason I do not get any alert inside dashboard/xhrGetListings function in java script.
Please help.
 
Hi,

Are you getting an error message? Is the AJAX firing?
If you use the inspector in Chrome, that will show you errors and the network activity for the AJAX call, that will really help in debugging the code.

Yeah, you don't need the
Code:
exit;
to exit the function.

Jon
 
element121 said:
Hi,

Are you getting an error message? Is the AJAX firing?
If you use the inspector in Chrome, that will show you errors and the network activity for the AJAX call, that will really help in debugging the code.

Yeah, you don't need the
Code:
exit;
to exit the function.

Jon
Hi Jon,
Thanks for your reply.
I'm able to get something back from model. But now, I get the entire page in console.log

Code:
		$.get('dashboard/xhrGetListings',function(data){
			
			console.log(data);
			alert(JSON.stringify(data)); // this doesn't help though..
		});

Complete page is shown in console or alert..
 
element121 said:
How are you running this, in a PHP framework?
Hi Jon,
localhost. My application redirects me to index page.
I'm learning MVC, AJAX basic stuff.
 
Your code is in a function, so the URL needs to map to the function, I don't see any code to do that.

If you are using MVC, do you have a PHP Framework like CodeIgniter, Symfony or ?

If not, you can try and put the code from the function xhrGetListings() into a new php page, without the function wrapper and then directly call the URL of that page in the AJAX call.
 
Hi Jon,

element121 said:
Your code is in a function, so the URL needs to map to the function, I don't see any code to do that.
I'm able to get through the function via bootstrap->controller->model. But annoyingly failing to get the end result. :(

element121 said:
If you are using MVC, do you have a PHP Framework like CodeIgniter, Symfony or ?
Nothing, plain folders.

element121 said:
If not, you can try and put the code from the function xhrGetListings() into a new php page, without the function wrapper and then directly call the URL of that page in the AJAX call.
Thanks. This I shall try. The only reason I've been avoiding this is to not to disturb existing architecture.
Best,
pG
 
Did you try the last option? What result did you get?
 
element121 said:
Did you try the last option? What result did you get?

Hi,
I just got it fixed ~30 mins back. I was including index/header/footer page if the method is present, and called.

Due to that I was getting HTML along with my JSON, thus the format wasn't JSON.
Thanks for your support.
 
Back
Top