开发者

I want to make a availability button on my registration page

Many websites have an "ch开发者_开发知识库eck availability" button. And I want to have this to. But this seems to be only available when using Ajax and Jquery. Is there any way for me to do this using only PHP and Javascript. Because i'm a starting programmer, which does not have the skills to work with Ajax or Jquery.

What I want, I have a username field. I typ in a username, and I want to check if the name is available. If the person clicks on the check availability button, I want a pop-up that says "This username is available" or "This username has already been taken".

If any knows how to do this in just PHP and Javscript, I would be very obliged to know.


Using ajax (and jquery) is easier than it seems. on your client-side you have a request like this:

$.ajax({
    url: 'usernameChecker.php',
    dataType: 'GET',
    data: 'username=' + $("#yourUserNameFieldID").val(),
    success: function(result)
    {
        alert(result);
    }
});

Of course you need to include jquery to implement this. jQuery makes it easy to make ajax-calls.

On your serverside you can use something like this:

<?php
if(isset($_GET["username"]))
{
    // check username
    if(username_is_free) 
    // of course this needs to be a bit different, but you'll get the idea
    {
       echo "Username is free!";
    }
    else echo "Username is taken!";
}
?>


$(document).ready(function(){
    // available-button is the ID of the check availability button
    //checkAvailability.php is the file which gets the username param and checks if its available
    // user is the id of the input text field where the user enter the username
    // available-message is the id of a div which displays the availability message. Use this instead of alert
    $('#available-button').click(function() {
        $.ajax({
            type : 'POST',
            url : 'checkAvailability.php',
            data: {
                username : $('#user').val()
            },
            success : function(data){
                $('#available-message').text(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Some Error occured. Try again")
            }
        });
        return false;
    });
});


Use jQuery ajax, that is the best & easy way to do it.

Using Ajax send a request to a php page which will take username as parameter (get or post method, you can specify in ajax call) and then on server side search for uniqueness of username in database & return appropriate response.


Is there any way for me to do this using only PHP and Javascript. Because i'm a starting programmer, which does not have the skills to work with Ajax or Jquery.

I think you'll find making an AJAX request in jQuery a lot more simple than writing one in pure javascript.

As jQuery is an extension on Javascript, you can use minimal jQuery to make the request to the server, and then deal with the response using pure javascript.

Look into using jQuery AJAX:

http://api.jquery.com/jQuery.ajax/

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

Here you would change the success part to something like:

success: function(data){
        if (data == 1){
            $(".message").html("Available");
        }
        else {
            $(".message").html("Already Taken");
        }
      }


I would recommend using jQuery for this task.

jQuery (or another Javascript library) will make the task simple to complete compared trying to do it without using one of them.

jQuery docs are good and for there are plenty of online tutorials matching what you want to do.

You will benefit from putting in the time to learn about the jQuery library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜