Unable to capture element in POST using AJAX

JavaScript and client side scripting.

Moderator: General Moderators

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

Unable to capture element in POST using AJAX

Post by publicGenome »

Hi Members,

I'm a newbie in AJAX, PHP, web development.

A background of what I'm doing:
user logs in (password, and id matched). Having logged in, session created, user is navigated to dashboard page. Dashboard page has a form.
Goal of the form is whatever user types in, will be shown on the page, without refreshing the page.

Form looks like:

Code: Select all



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

Javascript/AJAX looks like:

Code: Select all

$(function(){
	
	$('#randomInsert').submit(function(){
			
			var url=$(this).attr('action');
			var data=$(this).serialize();
					
			console.log(data);
			console.log(url); // to verfiy if things are going good.
	
			$.post(url,data, function(o){			
			});

			return false;
			});
	}
);

model for the dashboard looks like:

Code: Select all


<?php
class dashboard_model extends base_model{
	
	public function __construct(){
		//parent::__construct();
	}
	
	public function xhrInsert(){ //function to capture what is typed in

		if(isset($_POST["name_text"])){ //name_text is the form id in dashboard
			echo "done"."</br>";	
		}
		else{
			
			echo "not reaching "."</br>";
		}
	}	
}
For some reason, element isn't being stored or captured at $_POST["name_text"]
In the inspect element chrome, I'm able to view what is being sent by submit. On following the URL in inspect element, I see that $_POST['name_text'] isn't being set.

machine specs:
PHP 5.5.24
OSX - 10.9
Browser Google Chrome. Version 46.0

Please guide.
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to capture element in POST using AJAX

Post by publicGenome »

A __gentle__ bump to this post for help.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to capture element in POST using AJAX

Post by Celauran »

Hard to say for certain as I suspect there's a lot going on between the front controller and your controller's action. Have you confirmed the controller action is being reached? What parameters are available to it? Does using serializeArray instead of serialize change anything?
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to capture element in POST using AJAX

Post by publicGenome »

Celauran wrote:Hard to say for certain as I suspect there's a lot going on between the front controller and your controller's action. Have you confirmed the controller action is being reached? What parameters are available to it? Does using serializeArray instead of serialize change anything?
Hi Celauran,
Thanks for your reply.

I played with what you suggested. Looks like serializeArray doesn't help. Please see attached screen shot for both (serialize, and serializeArray).
serialize seems to have my object/variable..

The flow is good and working, but for some reason $_POST is not being Set.
Attachments
serializeArray.png
serialize.png
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to capture element in POST using AJAX

Post by publicGenome »

Celauran wrote:Hard to say for certain as I suspect there's a lot going on between the front controller and your controller's action. Have you confirmed the controller action is being reached? What parameters are available to it? Does using serializeArray instead of serialize change anything?
The control reaches model, and post submit to the actual method.
On printing:

Code: Select all

print_r($_POST);
I get an empty array:
Array ( )
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to capture element in POST using AJAX

Post by publicGenome »

Update:
Fixed it

In javascript, changed case of post to POST in

Code: Select all

                        $.post(url,data, function(o){                  
                        });
to

Code: Select all

                    $.POST(url,data, function(o){                  
                        });
:)
ValeriaB
Forum Newbie
Posts: 2
Joined: Wed Nov 04, 2015 12:02 am

Re: Unable to capture element in POST using AJAX

Post by ValeriaB »

I am also suffering from same problem, I will be obliged if I get the answer too.
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to capture element in POST using AJAX

Post by publicGenome »

ValeriaB wrote:I am also suffering from same problem, I will be obliged if I get the answer too.
Hi ValeriaB,

There couple of things here which hindered the output:
- The return result ain't in json format. ( this was the problem with me). I changed format to html (to play in AJAX, and figure out the problem, or didn't mention any format at first), and I was able to log, alert inside the ajax call.
- Since the result was HTML, that meant I wasn't calling the method from model properly. My model code was wrapped in HTML tags, due to which I was getting entire HTML page in return along with the SQL output. I had to fix this in the bootstrap file.

I hope this would help.
Post Reply