简体   繁体   English

Ajax代码在JavaScript中不起作用

[英]Ajax code is not working in JavaScript

I am using this code for my php MySql validation. 我正在使用此代码进行PHP MySql验证。

CheckID = document.getElementById('checkID_' + idCounter).value;
    $.ajax({
        type: "POST",
        url: "checkval.php",
        data: { check : CheckID }
    }).done(function( data ) {
    if(data !== "success");
    {
    var  val = confirm(data);
            if (val === false) {
               return false;
            }
        }
 });

This code is not working, Script wrote after that also not working. 这段代码不起作用,之后写的脚本也不起作用。 No idea about check the bug. 不知道要检查错误。 help me..... 帮我.....

this is my php script 这是我的PHP脚本

<?php

    $check          = $_POST['check'];

        $host       = 'localhost';
        $database   = 'database';
        $username   = 'user';
        $password   = 'password';

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

        $checkno = $check;
        $sql = "select claimno from check_details where checkno = $checkno";
        $result = mysqli_query($dbc,$sql);
        $rows = mysqli_num_rows($result);
        if($rows != 0)
        {
                        $claim = mysqli_fetch_array($result,MYSQLI_BOTH);
            $claimno = $claim['claimno'];
            $str = "Check Number:$checkno is already applied for $claimno.Are you sure to contniue ???";
                        echo $str;
        }
                else
                {
                        echo "success";
                }   
?>

You could write your function to return "a promise to return the boolean status value": 您可以编写函数以返回“承诺返回布尔状态值”:

function getMeStatus(checkID){
    var def = $.Deferred();    // Create a deferred object
    $.ajax({                   // Make the ajax call
        type: "POST",
        url: "checkval.php",
        data: {check: CheckID}
    }).done(function (data) {
       if (data !== "success"); {
          var val = confirm(data);
          if (val === false) {
             // Status was false?
             def.resolve(false);
          }
          else {
             // Status was true?
             def.resolve(true);
          }
       }
       else {
          // Status was true?
          def.resolve(true);
       }
    }
    // return a promise to supply the boolean status value later
    return def.promise();
 });

and use it like: 并像这样使用它:

CheckID = document.getElementById('checkID_' + idCounter).value;
getMeStatus(checkID).done(function(status){
    alert(status);  // Use the value here
});

Note: 注意:

  • (Before everyone jumps on this,) this is not an anti-pattern if the return value has to be changed (在每个人都跳上之前,)如果必须更改返回值,则这不是反模式
  • This can be shortened a little, but the required logic from the question is not clear :) 可以将其缩短一点,但是问题中所需的逻辑尚不清楚:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM