简体   繁体   中英

How to validate the html forms dynamically using php?

I want to check the availability of a username (entered in an HTML input field) across my server database using php. I know we cannot do this unless we submit data to the server. One possible solution is to relaunch the page again but i don't want to do this. Is there any possiblity to do it alternatively? How gmail does this? when we lose focus from the username field on signup form of gmail it automatically checks the availability. Any ideas?

Usually this is done using so-called "AJAX" technique. The idea is simple: you use javascript to send some data to your service, get some data back and update the page accordingly.

Modern Javascript libraries have helper methods for this.

You should have a script on the server that accepts username, checks its availability and emits some JSON (or javascript, or whatever you are able to consume).

You can also bind such check to onblur event. jQuery has helper for this as well.

So, in pseudocode it looks something like this

<script type='text/javascript'>
  jQuery(function() {
    $('#username_field').blur(function() {
      $.get('/check_availability.php', {username : this.val()}, function(data) {
        // update web page according to received data
      });
    });
  });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM