0

My PHP class:

class myclass
{
    function connection()
    {
        $con = mysqli_connect("localhost", "root", "", "clinic management system");
        return $con;
    }
    function getusername()
    {
        $q = mysqli_query($this->connection(), "select * from clients");
        return $q;
    }
}

php code:

<?php
$obj = new myclass();
$user = $obj->getalluser();
$username = array();
foreach($user as $suser){
    $username[] = $suser['client_name'];

    $data = array( "username" => "");
    if( isset($_POST["naam"]) ) {
        if( in_array( $_POST["naam"], $username ) ) {
            $data["username"] = "inuse";
        }
    }
}

echo json_encode( $data );
?>

jquery:

$(document).ready(function() {
    $("form.register").change(function() {
        $.post("check.php", $("form.register").serialize(), function(data) {
            if (data.username == "inuse")
                $("p#username_error").slideDown();
            else
                $("p#username_error").hide();
        }, "json");
    });
});

html:

<div id="myModal2" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
           <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Registeration</h4>
           </div>
           <div class="modal-body">
              <form method="post" action="index.php" name="myform" id="myform" class="register">
                 <label class="">Username:</label>
                 <input type="text" name="naam" class="form-control">
                 <p id="username_error" style="display: none;" class="error">That Username is unavailable</p>
                 <label class="">Age:</label>
                 <input type="text"  name="age" class="form-control">
                 <label class="">Gender:</label>
                 <select name="gender" class="form-control">
                    <option value="0">select gender</option>
                    <option>male</option>
                    <option>female</option>
                 </select>
                 <label class="">Email:</label>
                 <input type="text" name="email" class="form-control">
                 <label class="">phone:</label>
                 <input type="text" name="phone" class="form-control">
                 <label class="">password:</label>
                 <input type="password" name="pass" class="form-control">
                 <label class="">city:</label>
                 <input type="text" name="city" class="form-control">
                 <label class="">Address:</label>
                 <input type="text" name="address" class="form-control">
                 <input type="hidden" name="status" value="1" class="form-control">
                 <br>
                 <button class="btn btn-primary" type="submit" name="reg">register</button>
              </form>
           </div>
           <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
           </div>
        </div>

When user type his name he get in paragraph that this user is not available if that be already exist in my database so far I have wrote this code since I am new to PHP have no idea where I am doing wrong. Why is my jQuery code not working? I have briefly define all my PHP code I written for that process also my jQuery with HTML code which I used in modal.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chai Wala
  • 41
  • 1
  • 8
  • Are u searchinng from db? – Masivuye Cokile Jul 31 '17 at 15:03
  • Yes about database I am quite sure I will get data since function I used is already used in other places too for fetch. – Chai Wala Jul 31 '17 at 15:05
  • can you please post the query that u run on the db – Masivuye Cokile Jul 31 '17 at 15:07
  • function getalluser(){ $q = mysqli_query($this->connection(),"select * from clients"); return $q; } – Chai Wala Jul 31 '17 at 15:09
  • Can you update your answer with the appropriate class calls so we can see if it's an issue with the class? – GrumpyCrouton Jul 31 '17 at 15:15
  • I agree, but when I copied code from my editor and pasted here it all changes it form as it was there, sorry for bad code I will be careful next time. for now I am worried about this I have to show to client next day. ;/ @GrumpyCrouton I am 100% sure with class it is working. – Chai Wala Jul 31 '17 at 15:16
  • @ChaiWala the problem is with your query why are you selecting everything? u should only selected the row that matches what the user type – Masivuye Cokile Jul 31 '17 at 15:19
  • @ChaiWala whether you believe your class is all good or not you should post it that way there is no doubt in anyone's mind. I've thought the same thing before but it's easy to miss something when looking at your own code. – GrumpyCrouton Jul 31 '17 at 15:19
  • Edited post with class I used. after reading @MasivuyeCokile comment done same as he mentioned still no result. oops what is happening wrong. – Chai Wala Jul 31 '17 at 15:26
  • @ChaiWala check the server error log you should get something like `Too few arguments to function myclass::getuser(), 0 passed in C: and exactly 1 expected` – Masivuye Cokile Jul 31 '17 at 15:30
  • @ChaiWala is that the whole class? Forgive me but I don't see a function named `getalluser()` – GrumpyCrouton Jul 31 '17 at 15:31
  • change `$user = $obj->getalluser();` to `$user = $obj->getuser($_POST['naam']);` and see if it works... u are calling an undifined function – Masivuye Cokile Jul 31 '17 at 15:32
  • sorry for this but after suggestions I changed function for just getting username. so I have written class I am currently using and tested with echo for result. it is fetching data. but not when I type anything in my registration form. – Chai Wala Jul 31 '17 at 15:33
  • [Little Bobby](http://bobby-tables.com/) says **[your script is at risk for SQL Injection Attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)**. Learn about [Prepared Statements](http://en.wikipedia.org/wiki/Prepared_statement) for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even **[escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)** is not safe! – GrumpyCrouton Jul 31 '17 at 15:35
  • edited main post with current function using for fetch data. sorry for this kind of mistakes I am little bit thinking a lot where are things getting wrong – Chai Wala Jul 31 '17 at 15:39
  • @ChaiWala in your code you are still calling an undefined function. – GrumpyCrouton Jul 31 '17 at 15:39
  • there is no variable declared in function I think so. – Chai Wala Jul 31 '17 at 15:45
  • Also, before plugging the jQuery script with the backend I would try and see if the php code is correct. Please check that the getalluser() function is correct and returns something. Then check if you can iterate that correctly and only then try it with jQuery. – Warrior Jul 31 '17 at 16:04
  • function is returning names as I want. but after getting result now I seems jquery is not working but to be honest I have no specialization with jquery how far I know what I wrote should work I think so still identify me if I am doing wrong with jqeury – Chai Wala Jul 31 '17 at 16:07

2 Answers2

1

Try to update your php code like this:

PHP Code

<?php
//Include class file first
include("myclass.php");

$obj = new myclass();
$user = $obj->getusername();
$username = array();
foreach($user as $suser){
    $username[] = $suser['client_name'];
}
$data = array();
if( isset($_POST["naam"])) {
    if(in_array($_POST["naam"], $username)) {
        $data["username"] = "inuse";
    }
    else{
        $data["username"] = "not in use";
    }
}
echo json_encode($data);
?>

jQuery Code:

//callback handler for form submit
$("#myform").on('submit', function(e){
    e.preventDefault();
    var postData = $(this).serializeArray();
    var formURL = 'check.php'
    $.ajax(
    {
        url : formURL,
        type: "POST",
        dataType : "json",
        data : postData,
        success:function(data, textStatus, jqXHR) 
        {
             console.log(data);
             if (data.username == "inuse"){
                 $("p#username_error").slideDown();
             }
             else{
                 $("p#username_error").hide();
             }     
        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            console.log("Error found"); 
        }
    });
});
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
0

PHP Solved:

<?php
include("admin/function/kamran.php");
$obj = new myclass();
$user = $obj->getuser($_POST['a']); 

if(mysqli_num_rows($user) > 0){
echo 'inuse';
}
else{
echo 'Valid User Name';

 }

I forgot where clouse for username to get on type.

JQUERY Changes:

     $(document).ready(function(){
$("#txt").blur(function() {

    $.post("check.php", { a : $("#txt").val() } , function( data ) {
        alert(data);    
         });

     });
});

Instead sending data serialized I tried to send it with array. Now it work perfectly. Thanks for everyone contribution. Answered my own question if this code can help anyone else. html is same as posted in main post.

Chai Wala
  • 41
  • 1
  • 8