Jump to content

Archived

This topic is now archived and is closed to further replies.

CaptainChainsaw

[SOLVED] Kohana framework with JQuery issue

Recommended Posts

Hi all,

 

I'm new to Kohana and JQuery but I think my problem should be easy to fix.

 

What I want to do is dynamically check the database for usernames as soon as a user starts inputting a character, so I'm using the JQuery method .keyup for that.  The data is getting passed to the javascript as I can pass it to the alert() function and it is displayed as I expect it.  However the data doesn't appear on the webpage as I type it, its only printed on the page when the submit button is clicked.  I want it to display when it is submitted and on keyup.

 

Any ideas?

 

Here's the "view":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   
<html>   
<head>   

  	 <script src="/application/views/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="/application/views/jquery.form.js"></script> 
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
            $('#username').keyup(function(){
                sendValue($(this).val());  
               
            });
        });

        function sendValue(str){
	//	alert('sending value: '+str);
         //   $.post("/index.php/user/create.php",{ sendValue: str },
	   $.post("/application/controllers/user.php",{ sendValue: str },
		//$.post("create.php",{ sendValue: str },
            function(data){
                $('#username').html(data.returnValue);
            }, "json");
        }
</script>
<?php   
     echo html::stylesheet(array  
     (   
         'assets/css/style'  
     ),   
     array  
     (   
         'screen'  
     ), FALSE);   
?>   
<title>Registration</title>   
</head>   
<body>   
<?php echo form::open('user/create'); ?>   

<table class='editor'>   
<tr>   
     <td colspan='2' class='editor_title'>Register</td>   
</tr>   
<tr>
<td></td>
</tr>
<?php   
     echo "<tr>";   
	echo "<td>".form::label('username', 'Username: ')."</td>";   
	//echo "<td>".form::input('username', '')."</td>";   
	echo '<td><input type="text" name="username" id="username" /></td>';
	echo "<td> <div id=\"username\"></div></td>";
     echo "</tr>";   
        
        
     echo "<tr>";   
     echo "<td colspan='2' align='left'>".form::submit('submit', 'Register')."</td>";   
     echo "</tr>";   
?>   
</table>   
<?php echo form::close(); ?>   
</body>   
</html>  

 

Here's the "controller":

 


<?php defined('SYSPATH') OR die('No direct access allowed.');   
       
class User_Controller extends Controller   
{   
     private $user_model;    
     private $createuser_view;   
    
    public function __construct()      
    {    
        parent::__construct();   
        $this->user_model   = new User_Model;   
        $this->createuser_view   = new View('createuser');   
    }   
  
   public function index()   
   {   
    $this->createuser_view->render(TRUE);    
   }   

    public function create(){    
        $user_data=array(   
		'username'  => $this->input->post('username')   
        );   

	$db_users_list  = $this->user_model->findByUsername($user_data['username']);    
        if(sizeof($db_users_list) == 0){   
		echo json_encode(array("returnValue"=>"This is returned from PHP : ".$user_data['username'])); 
        }elseif(sizeof($db_users_list) == 1){
		$this->user_model->create($user_data);  
		url::redirect('user');  
	}		
    }   

}  


Share this post


Link to post
Share on other sites

$('#username').html(data.returnValue);

 

Would replace the entire form element with your value, you need to use the val() method. eg;

 

$('#username').val(data.returnValue);

 

Also, I'm not a kohuna user, but it looks to me like your uri ought to be....

 

/application/controllers/user/create

 

And within that method you should be looking for your data within...

 

'username'  => $this->input->post('sendValue')

Share this post


Link to post
Share on other sites

Hi thorpe,

 

Thanks again for your help, much appreciated!

 

Turns out that the following line was actually ok:

 

$('#username').html(data.returnValue);

 

 

Anyway I've got it working now, my first proper AJAX! 

 

Thanks again,

 

CaptainChainsaw  :)

 

 

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.