M
M
Matvey Mamonov2015-01-13 17:50:09
PHP
Matvey Mamonov, 2015-01-13 17:50:09

How to send a query to the database without refreshing the page?

Hello, today I decided to move to a new level for myself and improve the quality of my site.

I became very interested in how things like checking the availability of a nickname, logging into an account without reloading the page, and similar things are done.

Having rummaged through the ezernet, I found out that the so-called AJAX technology is connected with this. I would like to know how it works, what it is, what it is, how it connects, what it is eaten with and what you can read on this issue.

Now closer to the point. I think that it is not necessary to explain in detail what exactly I want, everything is clear here. You need to authorize the user without reloading the page. I have the necessary php code, but, of course, with this approach, I will have to reload the page.

Who can advise on this? It is desirable, of course, to give an exhaustive example of how this can be done, with code, of course. I would be very grateful.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Prilepa, 2015-01-13
@eucalipt

Suppose there is a form on the site that works without ajax:
In order for it to work through ajax, you need to hang a submit event handler on js, and interrupt the event in it (using the return false; method), instead of sending a request using js.
For simplicity and cross-browser compatibility, you can include jquery.
Then the following code will appear:

$(function(){ // DOM ready
  $('#test_form').submit(function(){
    $.post(
      $(this).attr('action'), // url
      $(this).serializeArray(), // data
      function(answ) //callback
      {
        //...
      },
      'html' // answer type (html, text, json, ...)
    );
    return false;
  });
});

Next is a matter of fantasy. I often make a full-fledged noajax version, then on the backend I add if (!$_REQUEST['ajax']) { ... } wrappers around blocks that do not need to be returned with ajax (to save traffic).
To prevent the user from spamming requests to the server, you can set a request indicator, etc.

A
Alexander Taratin, 2015-01-13
@Taraflex

https://code-live.ru/post/php-ajax-registration/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question