Welcome Guest, Not a member yet? Register   Sign In
How to pass PHP value to JS. under CI
#1

[eluser]Unknown[/eluser]
Hi Guys,

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

Code:
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

Code:
&lt;?php
$activityCode = array(
          'name'    => 'activityCode',
          'id'      => 'activityCode'
        );

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

Here is my model

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

       return $query->num_rows();
}


Here is my Javascript

Code:
(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="&lt;?php echo base_url();?&gt;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").addClass("green"); // add green color
                    msgbox.html('<img src="&lt;?php echo base_url();?&gt;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
#2

[eluser]toopay[/eluser]
You need to echoing the result instead 'return', in your controller function (activityCode_availability)
#3

[eluser]Unknown[/eluser]
Yeah, I've done that. echoing $a and $b this is my first code but it doesn't worked so I try returning it but its the same error. I can't pass the value from php to my js. I'm sorry this is my firstime using CI. thanks toopay




Theme © iAndrew 2016 - Forum software by © MyBB