Unable to get data back from PHP to AJAX

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Fri Oct 30, 2015 4:25 am

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: Select all

<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: Select all


$(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: Select all

	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.

element121
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Fri Oct 30, 2015 1:26 pm

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: Select all

exit;
to exit the function.

Jon

publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Fri Oct 30, 2015 2:38 pm

element121 wrote: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: Select all

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: Select all

		
		$.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
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Fri Oct 30, 2015 5:19 pm

How are you running this, in a PHP framework?

publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Fri Oct 30, 2015 5:49 pm

element121 wrote: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.

element121
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Sat Oct 31, 2015 4:18 am

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.

publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Sat Oct 31, 2015 4:46 am

Hi Jon,
element121 wrote: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 wrote: If you are using MVC, do you have a PHP Framework like CodeIgniter, Symfony or ?
Nothing, plain folders.
element121 wrote: 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

publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Wed Nov 04, 2015 7:46 am

A gentle bump to this post

element121
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Wed Nov 04, 2015 2:09 pm

Did you try the last option? What result did you get?

publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Wed Nov 04, 2015 2:18 pm

element121 wrote: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.

element121
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Wed Nov 04, 2015 3:01 pm

Ah, yeah that makes sense.

publicGenome
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Oct 30, 2015 4:13 am

Mon Nov 09, 2015 3:12 pm

@element
Thanks for your support. :)

Post Reply