W
W
webseodesigner2019-07-25 21:19:33
JavaScript
webseodesigner, 2019-07-25 21:19:33

AJAX how to get response without html page?

All the best.
I'm trying to make a form adding a product without reloading.
In all examples, this is based on three files:
1 - html form code
2 - javascript file
3 - php handler.
I have an html form and PHP handler is one file, the post request is sent to the address of the current page.
Attention to the question:
How to catch only the answer of the PHP script from the answer?
Now in the response comes the form html and the script response.
It is not possible to split into two files (php script form html).

$( document ).ready(function() {
document.getElementById('razmestit').addEventListener('submit', function(evt){
  var http = new XMLHttpRequest(), f = this;
  evt.preventDefault();
  http.open("POST", location.href, true);
  http.onreadystatechange = function() {
    if (http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
          if (http.responseText.indexOf(f.name.value) !== 0) {
    $('input').val(``);
    $('textarea').val(``);
        }
    }
  }
  http.onerror = function() {
    alert('Что-то не так!');
  }
  http.send(new FormData(f));
}, false);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
webseodesigner, 2019-07-25
@webseodesigner

I asked myself, I will answer.
In the file with the html form, first the php handler with exit!

if (isset($_POST['name'])) {
  echo 'Ваше объявление добавлено и будет опубликовано после проверки.';
  exit();
}
html.......

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question