Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it my jquery or php?
    primarykey
    data
    text
    <p>Edit: for some reason it seems like its refreshing the page upon hitting the submit button... any ideas?</p> <p>working on this simple chatroom script using ajax and kohana php framework. I can't seem to get my script working for creating a new user though and I have terrible debugging skills in php and javascript. Anyway I don't know if there's anything else to say.. here's my code:</p> <p>heres the code for the user class:</p> <pre><code>class Model_User extends Kohana_Model { var $_loaded = false; var $session = false; var $username = false; var $loginkey = false; function __construct() { $this-&gt;session = Session::instance(); } function new_user($username) { if($this-&gt;username_exists($username)) return false; $this-&gt;username = $username; $this-&gt;generate_key(); return $this-&gt;update_db(); } function username_exists($username) { if(DB::select()-&gt;from('users')-&gt;where('username', '=', $username)-&gt;execute()-&gt;count()&lt;1); return false; return true; } //helper methods private function generate_key() { if( ! $username) return false; $this-&gt;loginkey = md5($username.((int)mt_srand()*1000)); $this-&gt;session-&gt;set('loginkey',$this-&gt;loginkey); return true; } private function update_db() { if(DB::insert('users', array('username'=&gt;$this-&gt;username, 'loginkey'=&gt;$this-&gt;loginkey))-&gt;execute()) return true; return false; } } </code></pre> <p>and heres the code to handle the json request:</p> <pre><code>public function action_new_user(){ $user = new Model_User(); $json['username_exists'] = "true"; $username = $_POST['username']; if(isset($username)) { if($user-&gt;username_exists($username)) { echo json_encode($json); return; } $user-&gt;new_user($_POST['username']); $json['username_exists'] = "false"; } $this-&gt;request-&gt;response = json_encode($json); } </code></pre> <p>and heres the jquery and html</p> <pre><code>&lt;div id="chat_entry_errors"&gt; &lt;/div&gt; &lt;form id="new_chat_entry" name="posttest"&gt; &lt;input type="text" name="username" /&gt; &lt;input type="submit" name="submit" value="New User" /&gt; &lt;/form&gt; $(document).ready(function(){ $('#new_chat_entry').submit(function(){ $.post('json/new_user', $('#new_chat_entry').serialize(), function(data){ if(data.username_exists=='true'){ $('#chat_entry_errors').html('&lt;div class=\"error\"&gt;Sorry that username is currently unavailable. Please choose another.&lt;/div&gt;'); } else { $('#chat_entry_errors').html('&lt;div class=\"error\"&gt;Success!&lt;/div&gt;'); } } ); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload