Home » Php » php – Return value from Ajax to Javascript

php – Return value from Ajax to Javascript

Posted by: admin February 23, 2020 Leave a comment

Questions:

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\- [email protected]#$%&]/', '', $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?

How to&Answers:

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\- [email protected]#$%&]/', '', $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);   
}  
?>