AJAX调用显示来自html中php脚本的响应

时间:2022-11-07 01:25:23

I have a html file where users can input a value. I wrote a script in PHP that checks if this value is present in the databse. If it's present it returns

我有一个html文件,用户可以在其中输入值。我在PHP中编写了一个脚本,用于检查数据库中是否存在此值。如果它存在则返回

{"active":true}

Now my goals is that when the user inputs their value and submit they will be redirected to a certain page if this active is true. If it's false they should see an error message.

现在我的目标是,当用户输入他们的值并提交时,如果此活动为真,他们将被重定向到某个页面。如果它是假的,他们应该看到一条错误消息。

So here's what I've tried with my AJAX call:

所以这就是我尝试过的AJAX调用:

$("document").ready(function(){
 $(".checkform").submit(function(e){
    e.preventDefault();
    $.ajax({
      type: "GET",
      dataType: "json",
      url: "api/check.php", 
      data: data,
      success: function(data) {
        if(data.active=="true"){
            alert("success");
            location.href="where_you_want";
         }else{
             alert("failure");
         }
      }
    });
    return false;
  });
});

Here is my HTML:

这是我的HTML:

<form action="api/check.php" id="requestacallform" method="GET" name="requestacallform" class="formcheck">
                <div class="form-group">
                  <div class="input-group">                               
                    <input id="#" type="text" class="form-control" placeholder="Jouw subdomein" name="name"/>
                  </div>
                </div>
                <input type="submit" value="Aanmelden" class="btn btn-blue" />
</form>

For some reason I get an error:

出于某种原因,我收到一个错误:

Uncaught ReferenceError: data is not defined

未捕获的ReferenceError:未定义数据

I am new to AJAX and I am not sure if what I am trying is correct. Any help would be greatly appreciated!

我是AJAX的新手,我不确定我所尝试的是否正确。任何帮助将不胜感激!

Thanks in advance.

提前致谢。

2 个解决方案

#1


0  

Can you try:

你能试一下吗:

  $(".aanmeldenmodal").submit(function(e){
      e.preventDefault();

#2


0  

I am updating my answer in whole

我正在全面更新我的答案

<html>
<body>
    <form action="api/check.php" id="requestacallform" method="GET" name="requestacallform" class="formcheck">
        <div class="form-group">
            <div class="input-group">                               
                <input id="txt1" type="text" class="form-control" placeholder="Jouw subdomein" name="name"/>
            </div>
        </div>
        <input type="submit" value="Aanmelden" class="btn btn-blue checkform" />
    </form> 
</body>
</html>

jQuery part is like

jQuery部分就像

  $("document").ready(function () {

    $("body").on("click", ".checkform", function (e) {
        e.preventDefault();
        var request = $("#txt1").value;
        $.ajax({
        type: 'GET',
        url: 'ajax.php',
        data: {request: 'request'},
        dataType: 'json',
        success: function (data) {
           if(data.active==true){
               alert("success");
           }else{
                alert("failure");
           }

        }
    });

    });
});

ajax.php should be like this

ajax.php应该是这样的

if(isset($_GET['request'])){
  //check for the text
   echo json_encode($arr);     
}

#1


0  

Can you try:

你能试一下吗:

  $(".aanmeldenmodal").submit(function(e){
      e.preventDefault();

#2


0  

I am updating my answer in whole

我正在全面更新我的答案

<html>
<body>
    <form action="api/check.php" id="requestacallform" method="GET" name="requestacallform" class="formcheck">
        <div class="form-group">
            <div class="input-group">                               
                <input id="txt1" type="text" class="form-control" placeholder="Jouw subdomein" name="name"/>
            </div>
        </div>
        <input type="submit" value="Aanmelden" class="btn btn-blue checkform" />
    </form> 
</body>
</html>

jQuery part is like

jQuery部分就像

  $("document").ready(function () {

    $("body").on("click", ".checkform", function (e) {
        e.preventDefault();
        var request = $("#txt1").value;
        $.ajax({
        type: 'GET',
        url: 'ajax.php',
        data: {request: 'request'},
        dataType: 'json',
        success: function (data) {
           if(data.active==true){
               alert("success");
           }else{
                alert("failure");
           }

        }
    });

    });
});

ajax.php should be like this

ajax.php应该是这样的

if(isset($_GET['request'])){
  //check for the text
   echo json_encode($arr);     
}