cabbie Posted May 23, 2013 Share Posted May 23, 2013 I am trying to check data base to see if the username is availible.. Partial code of js and php The echo $username works I do not get a row count or username database result. The database works with other queries..What do I have wrong? form line-> <div>Username: </div> <input id="username" type="text" onblur="checkusername()" onkeyup="restrict('username')" maxlength="16"> function checkusername(){ var u = _("username").value; if(u != ""){ _("unamestatus").innerHTML = 'checking ...'; var ajax = ajaxObj("POST", "php_includes/nameCheck.php"); ajax.onreadystatechange = function() { if(ajaxReturn(ajax) == true) { _("unamestatus").innerHTML = ajax.responseText; } } ajax.send("usernamecheck="+u); } } nameCheck.php <?php //include_once("classes/connect.php"); if(isset($_POST["usernamecheck"])){ $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); echo $username; //// query to check if the username is in the db already //// $unameSQL = $db->prepare("SELECT username FROM members WHERE username=:usernamecheck LIMIT 1"); $unameSQL->bindValue(':usernamecheck',$username,PDO::PARAM_STR); try{ $unameSQL->execute(); echo $unameSQL; //NOTHING// $unCount = $unameSQL->rowCount(); echo $uncount; //NOTHING// } catch(PDOException $e){ echo $e->getMessage(); $db = null; exit(); } if($unCount == 0){ echo $username, "is OK"; $db = null; exit(); } else { echo "Sorry, that username is already in use in the system"; exit(); } } ?> <?php include_once("classes/connect.php"); if(isset($_POST["usernamecheck"])){ $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); echo $username; //// query to check if the username is in the db already //// $unameSQL = $db->prepare("SELECT username FROM members WHERE username=:usernamecheck LIMIT 1"); $unameSQL->bindValue(':usernamecheck',$username,PDO::PARAM_STR); try{ $unameSQL->execute(); echo $unameSQL; $unCount = $unameSQL->rowCount(); echo $uncount; } catch(PDOException $e){ echo $e->getMessage(); $db = null; exit(); } if($unCount == 0){ echo $username, "is OK"; $db = null; exit(); } else { echo "Sorry, that username is already in use in the system"; exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/ Share on other sites More sharing options...
DaveyK Posted May 23, 2013 Share Posted May 23, 2013 Can you verify that the ajax is actually sending to the correct file? Any errors in the console? Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1431779 Share on other sites More sharing options...
cabbie Posted May 23, 2013 Author Share Posted May 23, 2013 $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); echo $username; This echo verifies the entered username. The echos below contain no data The error is in the query or binding.. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1431824 Share on other sites More sharing options...
jazzman1 Posted May 25, 2013 Share Posted May 25, 2013 Do you get the value of: function checkusername(){ var u = _("username").value; alert(u); If your answer is - no, you need to show us the restrict() function also. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432227 Share on other sites More sharing options...
cabbie Posted May 25, 2013 Author Share Posted May 25, 2013 Result was no.... The restrict() function works I'll provide it and url I think you can see the page source.. function restrict(elem){ var tf = _(elem); var rx = new RegExp; if(elem == "email"){ rx = /[' "]/gi; } else if(elem == "username"){ rx = /[^a-z0-9]/gi; } tf.value = tf.value.replace(rx, ""); } function emptyElement(x){ _(x).innerHTML = ""; } function checkusername(){ var u = _("username").value; alert(u); if(u != ""){ _("unamestatus").innerHTML = 'Checking to see if name is available ...'; var ajax = ajaxObj("POST", "includes/nameCheck.php"); ajax.onreadystatechange = function() { if(ajaxReturn(ajax) == true) { _("unamestatus").innerHTML = ajax.responseText; } } ajax.send("usernamecheck="+u); } } https://libertarian-forum.com/!!AA/register.php Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432300 Share on other sites More sharing options...
cabbie Posted May 26, 2013 Author Share Posted May 26, 2013 I did get the alert to work.... Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432324 Share on other sites More sharing options...
jazzman1 Posted May 26, 2013 Share Posted May 26, 2013 Good for you, next step is to change the html form method from GET to POST. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432350 Share on other sites More sharing options...
cabbie Posted May 26, 2013 Author Share Posted May 26, 2013 Let me reask the question . The php code is the original code that works. Then the connect.php Help me convert the query to pdo.. <?php // Ajax calls this NAME CHECK code to execute if(isset($_POST["usernamecheck"])){ include_once("includes/db_conx.php"); $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); $sql = "SELECT id FROM users WHERE username='$username' LIMIT 1"; $query = mysqli_query($db_conx, $sql); $uname_check = mysqli_num_rows($query); if (strlen($username) < 3 || strlen($username) > 16) { echo '<strong style="color:#F00;">3 - 16 characters please</strong>'; exit(); } if (is_numeric($username[0])) { echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>'; exit(); } if ($uname_check < 1) { echo '<strong style="color:#009900;">' . $username . ' is OK</strong>'; exit(); } else { echo '<strong style="color:#F00;">' . $username . ' is taken</strong>'; exit(); } } ?> connect.php <?php $db_host = "localhost"; $db_username = "libertc0_dobbin"; $db_pass = "*****"; $db_name = "libertc0_vfl"; try{ $db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ echo $e->getMessage(); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432394 Share on other sites More sharing options...
jazzman1 Posted May 26, 2013 Share Posted May 26, 2013 (edited) I only want to know whether js sends a proper value to the server. So, on the top of the nameCheck.php put down next: echo '<pre>'.print_r($_POST, true).'</pre>'; Don't forget to change the html method attribute: <form name="signupform" id="signupform" onsubmit="return false;" class="form" method="post"> Edited May 26, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432399 Share on other sites More sharing options...
cabbie Posted May 26, 2013 Author Share Posted May 26, 2013 Print out = Array( [usernamecheck] => jim) Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432414 Share on other sites More sharing options...
jazzman1 Posted May 26, 2013 Share Posted May 26, 2013 (edited) You have to fetch the resut. Try, if(isset($_POST["usernamecheck"])){ $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); //// query to check if the username is in the db already //// $unameSQL = $db->prepare("SELECT username FROM members WHERE username=:usernamecheck LIMIT 1"); $unameSQL->bindValue('usernamecheck',$username,PDO::PARAM_STR); $unameSQL->execute(); // execute the sql $row = $unameSQL->fetch(PDO::FETCH_ASSOC); // fetch the result $count = $unameSQL->rowCount(); // count rows try{ echo $row['usernamecheck']; } catch(PDOException $e){ echo $e->getMessage(); $db = null; exit(); } echo $count; // free pdo connection $unameSQL = null; Edited May 26, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432427 Share on other sites More sharing options...
cabbie Posted May 27, 2013 Author Share Posted May 27, 2013 (edited) Same thing ..I had to add a closing curly brace. Also added echo $username That is the only thing that echos... https://libertarian-forum.com/!!AA/register.php Revised code <?php if(isset($_POST["usernamecheck"])){ $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); echo $username; //// query to check if the username is in the db already //// $unameSQL = $db->prepare("SELECT username FROM members WHERE username=:usernamecheck LIMIT 1"); $unameSQL->bindValue('usernamecheck',$username,PDO::PARAM_STR); $unameSQL->execute(); // execute the sql $row = $unameSQL->fetch(PDO::FETCH_ASSOC); // fetch the result $count = $unameSQL->rowCount(); // count rows try{ echo $row['usernamecheck']; } catch(PDOException $e){ echo $e->getMessage(); $db = null; exit(); } } echo $count; // free pdo connection $unameSQL = null; ?> Edited May 27, 2013 by cabbie Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432444 Share on other sites More sharing options...
jazzman1 Posted May 27, 2013 Share Posted May 27, 2013 (edited) Ok, don't focus to count and try{}catch{} for now. <?php if(isset($_POST["usernamecheck"])){ $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); //// query to check if the username is in the db already //// $unameSQL = $db->prepare("SELECT username FROM members WHERE username=:usernamecheck LIMIT 1"); $unameSQL->bindValue('usernamecheck',$username,PDO::PARAM_STR); $unameSQL->execute(); // execute the sql $row = $unameSQL->fetch(PDO::FETCH_ASSOC); // fetch the result echo $row['usernamecheck']; Enter the proper name to the html form, which you're 100% sure exist in the database and see if the result comes back. You can put down alert in this part of script: if(ajaxReturn(ajax) == true) { alert(ajax); _("unamestatus").innerHTML = ajax.responseText; } EDIT: I modified the php script above! Edited May 27, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432446 Share on other sites More sharing options...
cabbie Posted May 27, 2013 Author Share Posted May 27, 2013 yes that works Using link above enter karyoker.. That is in the data base I am not sure what you meant Dont use focus to count.. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432448 Share on other sites More sharing options...
cabbie Posted May 27, 2013 Author Share Posted May 27, 2013 Im using onblur so that when you click in the email you get a response.. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432451 Share on other sites More sharing options...
cabbie Posted May 27, 2013 Author Share Posted May 27, 2013 No it doesnt either work I didnt have both files uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432455 Share on other sites More sharing options...
jazzman1 Posted May 27, 2013 Share Posted May 27, 2013 (edited) When I said - don't focus for now about something , I wanted to say just ignore for a moment the pdo count function and the try{}catch{} block because it is easy for debugging to get the result back. I don't have any idea what are you talking about by saying - the files' upload failed. Edited May 27, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432497 Share on other sites More sharing options...
cabbie Posted May 28, 2013 Author Share Posted May 28, 2013 Ok I found out I didnt even have the right dir path to connect.php I have this code working..Last half is commented out .. echo 'fired', $row['username']; is printing username.. <?phpinclude_once('connect.php');echo '<pre>'.print_r($_POST, true).'</pre>';if(isset($_POST["usernamecheck"])){ $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']); echo $username; //// query to check if the username is in the db already //// $unameSQL = $db->prepare("SELECT username FROM members WHERE username=:usernamecheck LIMIT 1"); $unameSQL->bindValue(':usernamecheck',$username,PDO::PARAM_STR); $unameSQL->execute(); // execute the sql $row = $unameSQL->fetch(PDO::FETCH_ASSOC); // fetch the result $count = $unameSQL->rowCount(); // count rows try{ echo 'fired', $row['username']; } catch(PDOException $e){ echo $e->getMessage(); $db = null; exit(); } } //// Check if username is in the db already //// /*if($unCount > 0){ echo "Sorry, that username is already in use in the system"; $db = null; exit(); } else{ echo $uncount; if (strlen($username) < 3 || strlen($username) > 16) { echo '<strong style="color:#F00;">3 - 16 characters please</strong>'; exit(); } if (is_numeric($username[0])) { echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>'; exit(); } if ($unCount < 1) { echo '<strong style="color:#009900;">' . $username . ' is OK</strong>'; exit(); } } }*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432645 Share on other sites More sharing options...
jazzman1 Posted May 28, 2013 Share Posted May 28, 2013 You need to check if that username does not exist in the db before to call try{}catch{} blocks not after. Or more professional, create a custom function, put your logic in that function and call it inside try{} block. Quote Link to comment https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432724 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.