Jump to content

Recommended Posts

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();
    }
    }
?>  
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432300
Share on other sites

 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();
}
?>
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432394
Share on other sites

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 by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432399
Share on other sites

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 by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432427
Share on other sites

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 by cabbie
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432444
Share on other sites

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 by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432446
Share on other sites

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 by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432497
Share on other sites

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..

 

 

  <?php
include_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();
                            }
                        }
                    
    }*/
    ?>
Link to comment
https://forums.phpfreaks.com/topic/278306-ajax/#findComment-1432645
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.