weixin_33717117 2018-12-07 12:54 采纳率: 0%
浏览 15

如果成功声明ajax

I'm having trouble implementing if statement in ajax success function.

<?php  
  include('../Config/config.php');
  $myquery = "SELECT * FROM voters WHERE Precinct = '".$_POST['precinct']."'";
  $execute = mysqli_query($mysqli, $myquery);
  if (mysqli_num_rows($execute) >= 1)
  {
    echo "Precinct is full.
 Recheck precinct number.";
  }
?>
function checkerprecinct() {
  var precinct = $("#precinct").val();
  $.ajax({
    type: "POST",
    url: "precinctchecker.php",
    data: "precinct=" + precinct,
    success: function(data) {
      console.log(data);
      if (data === "") {
        alert("Data is empty!");
      } else {
        alert(data);
      }
    }
  });
}

I would like to use this as a validation.
I want to alert the user if the sent data contains similar data from the database.

  • 写回答

1条回答 默认 最新

  • weixin_33713350 2018-12-07 13:03
    关注

    try this code

    change with your code

    PHP Code:

    $data = array();
    if (mysqli_num_rows($execute) >= 1)
    {
        $data= array('code'=>100,'message'=>"Precinct is full.
     Recheck precinct number.");
        //echo "Precinct is full.
     Recheck precinct number.";
    }else{
        $data= array('code'=>101,'message'=>"Data is empty!");
    }
    
    echo json_encode($data);
    exit;
    

    ajax code:

    var data = JSON.parse(data);
    if (data['code'] == 100) {
        alert(data['message']);
    }
    
    评论

报告相同问题?