Unable to capture element in POST using AJAX
Posted: Wed Oct 28, 2015 7:55 am
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:
Javascript/AJAX looks like:
model for the dashboard looks like:
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.
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" />
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;
});
}
);
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>";
}
}
}
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.