-1

I wrote the ajax in the JavaScript function. That code is

function getValidate(checkID)
{
    alert(checkID);
    $.ajax({                  
        type: 'post',
        url: 'checkval.php',
        datatype: 'json',
        data: {checkID : checkID},
        success: function (response) {

          if (response === "OK"){
            alert("Validation Successed.");

        }else if(response === "NG"){
            alert("Check Already Exists.");
        }
    },
        error : function(err, req) {
        alert("Error Occurred");
    }
  });
 }

this code is outputs only "Error Occurred".

the connected php script is

<?php
        echo("welcome");
    $check          = $_POST['checkID'];
    $host       = 'localhost';
        $database   = 'database';
        $username   = 'root';
        $password   = 'root';

    $dbc = mysqli_connect($host,$username,$password,$database);

        $checkno = $check;
        $sql = "select claimno from check_details where checkno = $checkno";
        $result = mysqli_query($dbc,$sql);
        $rows = mysqli_num_rows($result);
        if($rows != 0)
        {
                        echo "NG";
        }
                else
                {
                        echo "OK";
                }   
?>

at a time of calling the JavaScript function php file not executed......

please give me the idea to success it...........

Sathish Kumar D
  • 274
  • 6
  • 20

4 Answers4

2

Try this :

if($rows != 0)
{
    $return =  "NG";
}
else
{
    $return = "OK";
}   


echo json_encode($return);
Happy Coding
  • 2,517
  • 1
  • 13
  • 24
1

You are getting error occurred, because of below code. Try logging something meaningful to triag this.

error : function(err, req) {
        alert("Error Occurred");
    }

Please try below code to get a clue of the error

error: function(xhr, status, error) {
  var err = eval("(" + xhr.responseText + ")");
  alert(err.Message);
}

Reference: Take a look at this query

Community
  • 1
  • 1
Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
1

Also you set datatype to json so response data must be json type

$.ajax({ dataType:"json"});

In php, store result in one variable and return json_encode

<?php
    echo("welcome");
$check          = $_POST['checkID'];
$host       = 'localhost';
    $database   = 'database';
    $username   = 'root';
    $password   = 'root';

$dbc = mysqli_connect($host,$username,$password,$database);

    $checkno = $check;
    $sql = "select claimno from check_details where checkno = '$checkno'";  //use single quote 
    $result = mysqli_query($dbc,$sql);
    $rows = mysqli_num_rows($result);
    if($rows != 0)
    {
                    $res = "NG";
    }
    else
    {
                    $res = "OK";
    }   
echo json_encode($res);  
?>
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
0

It seems that the php script is not working well.

Debug it with try and catch and see what output is comming.

Rachel Geller
  • 302
  • 1
  • 3
  • 12