Page 1 of 1

Unable to capture element in POST using AJAX

Posted: Wed Oct 28, 2015 7:55 am
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.

Re: Unable to capture element in POST using AJAX

Posted: Thu Oct 29, 2015 7:30 am
by publicGenome
A __gentle__ bump to this post for help.

Re: Unable to capture element in POST using AJAX

Posted: Thu Oct 29, 2015 7:58 am
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?

Re: Unable to capture element in POST using AJAX

Posted: Thu Oct 29, 2015 8:35 am
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.

Re: Unable to capture element in POST using AJAX

Posted: Thu Oct 29, 2015 8:45 am
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 ( )

Re: Unable to capture element in POST using AJAX

Posted: Thu Oct 29, 2015 8:58 am
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){                  
                        });
:)

Re: Unable to capture element in POST using AJAX

Posted: Mon Nov 09, 2015 7:22 am
by ValeriaB
I am also suffering from same problem, I will be obliged if I get the answer too.

Re: Unable to capture element in POST using AJAX

Posted: Mon Nov 09, 2015 7:28 am
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.