开发者

How to pass PHP value to JS. under CI

I'm using codeigniter framework, my problem is I can't pass the right value from php to js. This code is for availability check from user input to database.

here is my controller

class Ajax extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->output->set_output("This is an AJAX endpoint!");
    }

    function activityCode_availability()
    {
        $activityCode = trim($this->input->post("activityCode"));
        $activityCode = mysql_escape_string($activityCode);

        $msg = $this->Activity_model->check_activityCode($activityCode);


        if($msg == 1)
        {
            $a= '<font color="#cc0000"><b>'.$activityCode.'</b> is already in use.</font>';
            return $a;
        }
        else
        {
            $b = 'OK';
            return $b;
        }
    }
}

Here is my view

<?php
 $activityCode = array(
          'name'    => 'activityCode',
          'id'      => 'activityCode'
        );

 echo form_input($activityCode);
?>
<span id="availability_status"></span>

Here is my model

 function check_activityCode($activityCode)
 {  
      $query = $this->db->query("SELECT activityCode
            FROM ami_activity
            WHERE activityCode = '$activityCode'");

       return $query->num_rows();
 }

Here is my Javascript

(document).ready(function(){
   $('#activityCode').change(activity_check);
 });


 function activity_check()
 {
   var activityCode = $('#activityCode').val();
   var msgbox = $("#availability_status");

   if(activityCode.length > 2)
   {
       $("#availability_status").html('<img src="<?php echo base_url();?>img/loader.gif">&nbsp;Checking availability.');

       $.ajax({ 
          type: "POST", 
          url: "/index.php/ajax/activityCode_availability", 
          data: "activityCode="+ activityCode, 
          success: function(msg){ 
            $("#availability_status").ajaxComplete(function(event, request){ 
                if(msg == 'OK')
                { 
                    $("#activityCode").removeClass("red"); // remove red color
                    $("#activityCode").add开发者_StackOverflow社区Class("green"); // add green color
                    msgbox.html('<img src="<?php echo base_url();?>img/green_check.gif"> <font color="Green"> Available </font>');
                } 
                else 
                { 
                    $("#activityCode").removeClass("green"); // remove green color
                    $("#activityCode").addClass("red"); // add red  color
                    msgbox.html(msg);
                } 
            });
        } 
    }); 

}
else
{
    $("#activityCode").addClass("red"); // add red color
    $("#availability_status").html('<font color="#cc0000">Enter valid User Name</font>');
}
}

Any idea is very much appreciated. thanks in advanced.

Thanks,

Jams


You should echo the $a/$b variables not return them. Currently the ajax request doesn't have any output. You can use Firebug for Firefox or the Inspector Console in webkit browsers (Chrome, Safari) to see the actual response you're getting from the ajax request.


Thanks to your help, it is working right now. I figure out my error using firebug. There are two errors on my code 1 in my JS and 1 in my MySQL syntax

here are my errors

1.) url: "/index.php/ajax/activityCode_availability"
2.) on my model I included single quote on my condition
    $query = $this->db->query("SELECT activityCode
        FROM survey_activity
        WHERE activityCode = '$activityCode'");

here are my solutions

1.) url: "my_folder/index.php/ajax/activityCode_availability"
2.) on my model I removed the single quote on my query condition
    $query = $this->db->query("SELECT activityCode
        FROM survey_activity
        WHERE activityCode = $activityCode");

My ajax request is sending properly but in different url. it is sending on localhost/index.php/activityCode_availability instead of localhost/my_folder/index.php/activityCode_availability so it is sending on the default page of my localhost server. And also I implemented echoing $a and $b instead of returning it. Thanks to your advice. :)

Thanks,

Jams

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜