Ajax response display response from php script in html

I have an html file in which users can enter a value. I wrote a script in PHP that checks if this value is present in the database. If it is present, it returns

{"active": true}

Now my goals are that when the user enters their value and sends them, they will be redirected to a specific page if it is active. If it is wrong, they should see an error message.

So here is what I tried with my AJAX call:

$("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:

<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 not defined

I'm new to AJAX and I'm not sure if I'm trying correctly. Any help would be greatly appreciated!

Thanks in advance.

+4
2

<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

  $("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

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

:

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

Source: https://habr.com/ru/post/1652017/


All Articles