Checking username on local server using jquery, ajax and php ajax() not connecting -
so have mysql database on local system , connecting using php. know there nothing wrong the server (apache) because have used in calls using php.
the problem think "ajax() request
i have started using ajax. followed tutorial implement way check if username exists on db asynchronously.
all code works until ajax() request.
i don't errors. see on screen result of code:
$("#availability_status").html(' checking availability...'); //add loading image in span id="availability_status"
i have been searching solution day , it's driving me mad. thank in advance!
<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>register</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script type="text/javascript"> $(document).ready(function() { //when dom ready alert("ready!"); $("#username").change(function() { //if therezs change in username textbox var username = $("#username").val();//get value in username textbox if(username.length > 3) { $("#availability_status").html('<img src="loader.gif" align="absmiddle"> checking availability...'); //add loading image in span id="availability_status" $.ajax({ type: "post", url: "http://localhost/ajax_check_username.php", data: {username: username}, success: function(server_response){ if(server_response == '0')//if ajax_check_username.php return value "0" { $("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="green"> available </font> '); //add image span id "#availability_status" } else if(server_response == '1')//if returns "1" { $("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">not available </font>'); } } }); } else { $("#availability_status").html('<font color="#cc0000">username short</font>'); //if in case username less or equal 3 characters } return false; }); }); </script> </head> <body> <div id="content"> <strong>register</strong> <form action="http://localhost/register2a.php" method="get"> <div class="style_form"> <label for="username">username :</label> <input type="text" name="username" id="username" class="form_element"/> <span id="availability_status"></span> </div> <div class="style_form"> <label for="full_name">full name :</label> <input type="text" name="full_name" id="full_name" class="form_element"/> </div> <div class="style_form"> <label for="email">email :</label> <input type="text" name="email" id="email" class="form_element"/> </div> <div class="style_form"> <input name="submit" type="submit" value="submit" id="submit_btn" /> </div> </form> </div> </body> </html>
////here php file
<?php $conn = mysqli_connect('localhost', 'root', 'sherly743759', 'login'); //include database connection file if(isset($_post['username']))//if username has been submitted { //check username not taken $conn = mysqli_connect('localhost', 'root', 'sherly743759', 'login'); $username = mysql_real_escape_string($username); $query = "select username member username = '$username';"; $result = mysql_query($query); if(mysql_num_rows($result) > 0) { echo '1'; //not available } else { echo '0'; // username available } } ?>
i use request on ajax issues, meaning jquery ajax type:"get" , read parameter on php.. can test php page running in browser this
localhost/ajax_check_username.php?username=something
Comments
Post a Comment