Control Not going to the Controller - php

i am using codeigniter. I have set a form validation via jquery and after validation the data /control is not moving on to the Controller.
Here is my jquery code
var data1 = {
username:$("#username").val(),
password:$("#password").val()
}
$.ajax({
type:'POST',
url:base_url+"site/chk_info",
data:data1,
success: function (response){
alert (response);
},
error:function (){
alert ("Sorry we are getting problems plz try again latter");
}
}); // end ajax
Here is my controller method.
public function chk_info(){
return true;
}
The problem is in jquery the controll in not entering into the success function it always getting into the error function.

chk_info() is only returning true IF PHP was listening and acting upon it.
You should be echo-ing true like this instead:
public function chk_info(){
echo 'true';
}

try this i think its url problem
$.ajax({
type:'POST',
url:'<?php echo base_url() ?>site/chk_info',
data:data1,
success: function (response){
alert (response);
},
error:function (){
alert ("Sorry we are getting problems plz try again latter");
}
});

Related

pass the data-id attribute value to a controller method using ajax in codeigniter

I have a button element which has a data-id attribute.I need to to send the data-id to a controller method in codeigniter using jquery ajax post method.I don't have any form .But the ajax request i made can not enter into the controller method .What might be the reason and how i can solve this issue?in my config file i set the base url and rewrite the index.php using htaccess file :
I do get alert('success') after clicking the button
$config['base_url'] = 'http://localhost/mycodeigniter/ci/'
admin controller:
class Admin extends CI_Controller{
public function processReq(){
$this->load->view('admin/processReq');
}
}
button( no form element) :
<button type="button" data-id='approved' class="approved buttons">Approve</button>
ajax method:
$(".buttons").click(function(event){
var status=$(this).data('id');
$.ajax({
url:"<?php echo base_url('admin/processReq') ;?>",
type: "POST",
data: {
status: status
},
success: function(data) {
alert('success');
},
error: function(xhr, status, error) {
var error=xhr.responseText;
alert(error);
}
});
});
You get the alert because the controller sent the browser a reply with a server code of 200. That header response code tells AJAX to call the success function. $this->load->view('admin/processReq'); is where the OK (code 200) came from.
This controller shows another way to handle this so that you can react based on the reply from processReq().
class Admin extends CI_Controller
{
public function index()
{
$this->load->view('admin/processReq'); //if this really is the view file
}
public function processReq()
{
$status = this->input->post('status');
//I am assuming that data('id') is '1' or '0' since you
//don't describe what it actually is I had to make something up.
if($status === '1'){
echo json_encode(array('results' => 'success')) ;
}else{
echo json_encode(array('results' => 'fail')) ;
}
}
}
You need to add one ajax option - dataType: 'json' - for this to work reliably
$.ajax({
url:"<?php echo base_url('admin/processReq') ;?>",
type: "POST",
dataType: 'json',
data: {
status: status
},
Your ajax success function could do this
success: function(data) {
if(data.results === 'success'){
alert('success');
} else {
alert('success');
}
},
An alternative way to indicate a failure would be to output a header with a server code in the 400 or 500 (error) range. If you do that then the ajax error function will be called. SO has lots of examples of how to accomplish that.

FUELPHP Ajax request without jquery

I have looked everywhere, I found, that FUELPHP not handle Ajax requests, native and easily, as does RubyOnRails for example.
There must be done manually through jquery, unless I'm missing something I see is this: you have to use preventDefault () for the submit event of the form to create a post, product or whatever, and use the $ function post () to send the relevant parameters, which I think is ridiculous for a framework that claims to be based on the best ideas from other frameworks.
Please tell me if I'm wrong, I like FUELPHP, and I'm thinking about choosing it as PHP framework, but I want to be clear about this.
why not you can handle ajax in fuelphp like this.
.01. create ajax request in your view or public/assets/ create javascript file,
<script> $('.login').on('click',function(){
$.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (e) {
alert("loading");
}, false);
return xhr;
},
url: "<?php echo \Uri::create('auth/login'); ?>",
type: "POST",
dataType: 'json'
data: {'username':$('#username').val(), 'password':$('#password').val()},
success: function (data) {
alert(data.status);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error");
}
});
});
</script>
.02. after that you can handle post data in classes/controller/ create auth.php and login method like this,
<?php
class Controller_Auth extends \Controller_Template {
function action_login() {
if (\Input::is_ajax()) {
if (\Input::param('username') and \Input::param('password')) {
$username = \Input::param('username');
$password = md5(\Input::param('password'));
//check password and username or anything
$msg = 'fail';
return json_encode(array('status' => $msg));
}
}
...
}
}
?>
you can handle data like this. i think you got something. and this is helpful.

ajaxStart and ajaxStop for a specific event

How can I bind an ajaxStart function for a specific event, using :
$(document).ajaxStart(function () {
alert("started");
});
$(document).ajaxStop(function () {
alert("Ended");
});
Tried this code but it runs whenever autocomplete starts.
Scenario must be like this : whenever I submit a form, that function will be called.
But when I'm just fetching values using autocomplete via ajax, ajaxStart and ajaxStop shouldn't be called.
But when I'm just fetching values using autocomplete via ajax,
ajaxStart and ajaxStop shouldn't be called.
You can create a boolean variable to keep track of whether user is typing or not something like:
<script>
var isTyping = false;
// inside your autocomplete handler set isTyping to true
$(document).ajaxStart(function(){
if (! isTyping) alert("started");
});
$(document).ajaxStop(function(){
if (! isTyping) alert("Ended");
});
</script>
It is better if you uses the main function and it's sub-functions.
$.ajax({
url: 'url_to_page'
beforeSend: function(req){ //Before the request is taking off},
error: function(req){ //If there were a error},
success: function(req){ //When it all was done}
});
$.ajax({
url :'your url',
data: {
//data to send if any
},
type: 'POST',
success:function(msg){
//eqv to ajaxstop if OK
},
beforeSend:function(){
//before ajax starts
},
error:function(){
//failure in ajax
}
});

Jquery: Only fill in data from .load if there is content?

Im using the jquery .load function to query a php file that will output some data. Now sometimes the script will return nothing. In this case, can I have the load function not put any data into my specified div? (right now it clears out the div and just puts a blank white area.
Thanks!
try using $.get;
$.get('<url>',{param1:true},function(result){
if(result) {
$('selector').html(result);
}
else {
//code to handle if no results
}
});
Use $.get
http://api.jquery.com/jQuery.get/
in addition to #jerjer's post, you can also use this:
var paramData= 'param=' + param1 + '&user=<?echo $user;?>';
$.ajax({
type: "GET",
data:paramData,
url: "myUrl.php",
dataType: "json", // this line is optional
success: function(result) {
// do you code here
alert(result); // this can be an any value returned from myUrl.php
},
statusCode: {
404: function() {
alert('page not found');
}
}
});

Using jQuery Ajax methods display a response from a PHP page?

I'm trying to implement the Jquery .ajax method to simplify the ajax in my website.
Here is the function I'm working with:
function autoComplete(q, succ)
{
$.ajax({type:"GET",
url: "search.php",
data: "q="+q,
success: succ
});
}
$('input#auto_results').live('keyup', function() {
var text = $('input#auto_results').val();
autoComplete(text,
function(data)
{
alert(data);
});
});
The response on the PHP page is simply:
echo "response";
So I figure that it should alert the response when the function is called, on 'keyup'.
Sadly, nothing occurs. I must be doing something wrong, I am just not sure what it is.
is "keyup" event firing?
do following.
$('input#auto_results').live('keyup', function() {
var text = $('input#auto_results').val();
alert("Keyup event is firing");
autoComplete(text,
function(data)
{
alert(data);
});
});
if event is firing. then see firebug console tab
or put error function callback on your code:
function autoComplete(q, succ)
{
$.ajax({type:"GET",
url: "search.php",
data: "q="+q,
error:function(request, textStatus, err){
alert(request.statusText);
},
success: succ
});
}
you may get near to error.

Categories