php - jQuery check if username is taken -
ok, driving me nuts. have tried ton of other answers here, no avail , hate having post has been beaten death, cant work.
i checking db see if username exists , no matter type in (whether existing or not), return says name available. if run check , dump screen, correct returns printed.
<?php $query = new application_model_queries(); $false = $query->usernames('vikingblooded'); print_r( $false); //prints 1 $true = $query->usernames('someonespecial'); print_r($true); prints nothing ?>
javascript:
$(document).ready(function () { $("#username").keyup(function () { $("#message").html("<img src='<?php echo $this->baseurl('images/ajax-loader.gif'); ?>' class='status' /> checking..."); var username = $("#username").val(); $.ajax({ method: "post", url: "<?php echo $this->baseurl('users/check') ?>", data: {username: username}, success: function (data) { if (data === 1) { $("#message").html("<img src='<?php echo $this->baseurl('images/cross.png') ?>' /> username taken"); } else { $("#message").html("<img src='<?php echo $this->baseurl('images/tick.png') ?>' /> username available"); } } }); }); });
php
public function usernames($uname) { $select = $this->_dbtableusers->select() ->from($this->_dbtableusers, array('user_name')) ->where('user_name = ?', $uname); $rows = $this->_dbtableusers->fetchrow($select); if ($rows) { return true; } }
html
<table> <tr> <td>username</td> <td>:</td> <td><input type="text" name="username" id="username"/><td> <td id="message"><td> </tr> <tr> <td>password</td> <td>:</td> <td><input type="text" name="password" id="password" /><td> </tr> </table>
you're not formatting data js php correctly nor defining method proper way:
$.ajax({ method: "post", // changed type: "post" url:"<?php echo $this->baseurl('users/check')?>", data: { username: username }, // changed "username="+username success:function(data){ if(data == 1){ $("#message").html("<img src='<?php echo $this->baseurl('images/cross.png')?>' /> username taken"); } else { $("#message").html("<img src='<?php echo $this->baseurl('images/tick.png')?>' /> username available"); } } });
you should define type of data you're expecting , try use json communicate data php js:
php:
<?php echo json_encode(array('somevalue' => false)); ?>
js:
$.ajax({ // other stuff datatype: 'json' // other stuff });
Comments
Post a Comment