G
G
GrandinaWi2021-04-30 16:12:19
AJAX
GrandinaWi, 2021-04-30 16:12:19

How to make an output condition on AJAX success?

Hello. There is a PHP code in which if the data in the form is empty, then it sets the error variable to true. It is necessary to make a condition in AJAX upon success, if error = true , then execute a certain section of code. But I have an error somewhere in the AJAX code apparently.

$id=$_POST['id_header'];
if($id=="")
{
  $error=true;

}

function headDelete(result,ajax_form,url){

  $.ajax({
    url: url,
    type: "POST",
    dataType: "html",
    data: $("#header_form").serialize(),
     success: function(response){
      
      if (response==true) {
            $('#result').addClass('alert alert-danger');
        $('#result').html("Поле не заполнено");
      }
    else
    {
         $('#result').addClass('alert alert-success');
        $('#result').html("Данные удалены успешно");
     } 
     }

  });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AUser0, 2021-04-30
@GrandinaWi

It is necessary if(response.error="true"),
and accordingly in PHP output

echo(json_encode(array('error' => "true")));exit();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question