-1

I'm trying to check if a Name already exists in the database. But my Ajax isn't returning the value, honestly not even sure if the Ajax is running at all.

Here's my code on my php page.

<input class="form-control" id="name" name="name" onblur="checkNameStatus()" />

<script>    
    function checkNameStatus(){
    //alert('Start of funtion');
    var name=$("#name").val();
    //alert("Name Input value is: "+name);
    $.ajax({
        type:'post',
            url:'<?php echo base_url() ?>assets/ajax/checkName.php?name='+name,
            success:function(msg){
            alert(msg);     
            }
     });
     //alert("End of function");
    }
</script>

Using alerts I can tell that the function is indeed running and recieving the name input's value.

And then my Ajax

<?php

$hostname   = 'localhost';
$username   = 'root';
$password   = '123';
$dbname     = 'test_db';

function clean($string) {
   return preg_replace('/[^A-Za-z0-9\- .!@#$%&]/', '', $string);
}

$name= clean($_GET['name']);
$msg = "";

$conn=mysqli_connect($hostname,$username,$password,$dbname);

if (mysqli_connect_errno()){
    echo ("Failed to connect to MySQL: " . mysqli_connect_error());
}

$sql = "SELECT * FROM pattern where name = '$name';";

$check=mysqli_query($conn, $sql);
$count=mysqli_num_rows($check);

if($count!=0)
   {
     $msg = "A Schedule Pattern with the Name of $name already exists. If you continue to use this Name it will overwrite $name's current Data.";
     return $msg;
   } else {
    $msg = "Count check Failed"; 
    echo $msg;
   }


mysqli_close($con);

 ?>

How do I receive the $msg variable from my ajax and return it to the script, and then subsequently show it in an alert?

Chris
  • 15
  • 7
  • `$msg = "A Schedule Pattern with the Name of $name already exists. If you continue to use this Name it will overwrite $name's current Data."; echo $msg;` just echo your message instead of returning . – Manu Varghese Feb 17 '20 at 08:41
  • Ajax doesnt have return value. Use echo to return values – Marty1452 Feb 17 '20 at 08:45
  • I've replaced the **return $msg** with **echo "$msg";** but it still doesn't seem to work. – Chris Feb 17 '20 at 08:47
  • @Chris what was the exact error message? – Manu Varghese Feb 17 '20 at 08:49
  • Why are you using mysql_query not mysqli_query? – Ebrahim Mohammed Feb 17 '20 at 08:50
  • Same with num rows – Ebrahim Mohammed Feb 17 '20 at 08:50
  • 1
    See: [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) and [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860046) – CD001 Feb 17 '20 at 08:55
  • For starters: You create a POST query and then try to retrieve via GET. Then, as several people have pointed out, you need to echo an error message. But you also need to echo something if you succeed. – Ben Hillier Feb 17 '20 at 08:55
  • Upon further inspection, it would seem that the ajax wasn't called. I had to replace **$check=mysql_query($sql); $count=mysql_num_rows($check);** with **$check=mysqli_query($sql); $count=mysqli_num_rows($check);** – Chris Feb 17 '20 at 09:01
  • However, now my $msg does return, but empty. – Chris Feb 17 '20 at 09:02
  • You should change this $check=mysqli_query($sql); to this $check=mysqli_query($conn, $sql); – Ebrahim Mohammed Feb 17 '20 at 09:03
  • I've added an else to the count checker and it seems the count check returns the else value. – Chris Feb 17 '20 at 09:10

1 Answers1

0

I think you have to change this

$check=mysql_query($sql);
$count=mysql_num_rows($check);

To this

$check=mysqli_query($conn, $sql);
$count=mysqli_num_rows($check);

You are using both mysql_ and mysqli_ together which is wrong, and it is probably why it failed to check for a count, I also recommend you emplement error messages incase no name has been found for example.

Edit, full code should be: (PHP):

<?php

$hostname   = 'localhost';
$username   = 'root';
$password   = '123';
$dbname     = 'test_db';


function clean($string) {
   return preg_replace('/[^A-Za-z0-9\- .!@#$%&]/', '', $string);
}

if(isset($_GET['name'])){
$name= clean($_GET['name']);
$msg = "";

$conn=mysqli_connect($hostname,$username,$password,$dbname);

if (mysqli_connect_errno()){
    echo ("Failed to connect to MySQL: " . mysqli_connect_error());
}

$sql = "SELECT * FROM pattern where `name` = '$name';";

$check=mysqli_query($conn, $sql);
$count=mysqli_num_rows($check);

if($count!=0)
   {
     $msg = "A Schedule Pattern with the Name of $name already exists. If you continue to use this Name it will overwrite $name's current Data.";
     echo $msg;
   } else {
    $msg = "Count check Failed"; 
    echo $msg;
   }    
mysqli_close($conn);   
}  
?>
  • You do you think so? Any arguments? – Aksen P Feb 17 '20 at 08:54
  • Both mysql and mysqli are different systems, I don't think he should mix mysql with mysqli because it wouldn't work, but correct me if I am wrong – Ebrahim Mohammed Feb 17 '20 at 08:56
  • If you've decided to let here answer, then you should be sure in it. Any *I think* should be in comments. – Aksen P Feb 17 '20 at 09:01
  • Thanks, I've done this and the ajax now runs. But the $msg var is empty for some reason. – Chris Feb 17 '20 at 09:04
  • It might be that the count is 0, can you try doing an echo just above the mysqli_close? Example : echo "test" ; – Ebrahim Mohammed Feb 17 '20 at 09:05
  • I've added an else to the count checker and it seems the count check returns the else value. i.e like you suggested, the count isn't actually 0. There is however a DB entry with the name of "Test".. so is there something off with the logic here? – Chris Feb 17 '20 at 09:15
  • Are you sure you added the $conn to the mysqli query? – Ebrahim Mohammed Feb 17 '20 at 09:19
  • Yup, **$conn=mysqli_connect($hostname,$username,$password,$dbname);** is definately in there. Underneath **$msg = "";** – Chris Feb 17 '20 at 09:22
  • Nope, I meant: $check=mysqli_query($conn, $sql); – Ebrahim Mohammed Feb 17 '20 at 09:24
  • Because remember, for mysqli_query you need both: $conn, and $sql, the code you edited still missing the "$conn" at the first arguement of mysqli_query – Ebrahim Mohammed Feb 17 '20 at 09:25
  • I've edited my question, is that what you meant? If so, it still returns a false value. – Chris Feb 17 '20 at 09:29
  • I just edited my answer to include the full file! check it out, you were using return $msg; it should have been echo $msg; and you also closed the connection using mysqli_close($con); it should have been mysqli_close($conn); (extra n) – Ebrahim Mohammed Feb 17 '20 at 09:32