Jump to content

[SOLVED] PHP & AJAX & Mysql help :(


jamesxg1

Recommended Posts

Hiya,

 

i have a user availability script and i need it to check my database but for some unknown reason all it is posting back is "username available" even when they aren't,

 

 

user_availability.php

<?php

include 'includes/Functions.php';
include 'includes/Connection.php';

$username = Clean($_GET['user_name']);

$check = ceil(mysql_num_rows(mysql_query("SELECT `username` FROM `members` WHERE username = '$username'")));

if($check > 0) {

echo "no";

} else {

echo "yes";

}

$check_sql = "SELECT `username` FROM `members`" or die (mysql_error());
$check_query = mysql_query($check_sql);

while($usernames = mysql_fetch_array($check_query)) {

@$usernamecheck = $usernames['username'];

}

if ($username == "$usernamecheck") {

echo "no";

} else {

echo "yes";

}


?>

 

 

register.html

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>
Registration
</title>



<script src="includes/JS/jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">

$(document).ready(function()
{
$("#username").blur(function()
{
	//remove all the class add the messagebox classes and start fading
	$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
	//check the username exists or not from ajax
	$.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
        {
	  if(data=='no') //if username not avaiable
	  {
	  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
		{ 
		  //add message and change the class of the box and start fading
		  $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
		});		
          }
	  else
	  {
	  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
		{ 
		  //add message and change the class of the box and start fading
		  $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);	
		});
	  }

        });

});
});
</script>
<style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
.top {
margin-bottom: 15px;
}
.messagebox{
position:absolute;
width:100px;
margin-left:30px;
border:1px solid #c93;
background:#ffc;
padding:3px;
}
.messageboxok{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #349534;
background:#C9FFCA;
padding:3px;
font-weight:bold;
color:#008000;

}
.messageboxerror{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #CC0000;
background:#F7CBCA;
padding:3px;
font-weight:bold;
color:#CC0000;
}

</style>






<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- CSS -->
<link rel="stylesheet" href="includes/CSS/structure.css" type="text/css" />
<link rel="stylesheet" href="includes/CSS/form.css" type="text/css" />

<!-- JavaScript -->
<script type="text/javascript" src="includes/JS/wufoo.js"></script>
</head>

<body id="public">

<img id="top" src="includes/CSS/images/top.png" alt="" />
<div id="container">

<h1 id="logo"></h1>

<form id="form69" name="form69" class="wufoo topLabel" autocomplete="off"
enctype="multipart/form-data" method="post" action="RegisterProcess.php">

<div class="info">
<h2>Registration</h2>
<div></div>
</div>

<ul>


<li id="foli0" 		class="    ">
<label class="desc" id="title0" for="Field0">
	Name
		</label>
<span>
	<input id="Field0" 			name="Field0" 			type="text" 			class="field text" 			value="" 			size="8" 			tabindex="1" 			/>
	<label for="Field0">First</label>
</span>
<span>
	<input id="Field1" 			name="Field1" 			type="text" 			class="field text" 			value="" 			size="14" 			tabindex="2" 			/>
	<label for="Field1">Last</label>
</span>
</li>


<li id="foli2" 		class="complex     ">
<label class="desc" id="title2" for="Field2">
	Address
		</label>
<div>
	<span class="full">
	<input id="Field2" 			name="Field2" 			type="text" 			class="field text addr" 			value="" 			tabindex="3" 			/>
	<label for="Field2">Street Address</label>
	</span>
	<span class="full">
	<input id="Field3" 			name="Field3" 			type="text" 			class="field text addr" 			value="" 			tabindex="4" 			/>
	<label for="Field3">Address Line 2</label>
	</span>
	<span class="left">
	<input id="Field4" 			name="Field4" 			type="text" 			class="field text addr" 			value="" 			tabindex="5" 			/>
	<label for="Field4">City</label>
	</span>
	<span class="right">
	<input id="Field5" 			name="Field5" 			type="text" 			class="field text addr" 			value="" 			tabindex="6" 			/>
	<label for="Field5">State / Province / Region / County</label>
	</span>
	<span class="left">
	<input id="Field6" 			name="Field6" 			type="text" 			class="field text addr" 			value="" 			maxlength="15" 			tabindex="7" 			/>
	<label for="Field6">Postal / Zip Code</label>
	</span>
	<span class="right">
	<select id="Field7" 			name="Field7" 			class="field select addr" 			tabindex="8" 			>
		<option value="" selected="selected"></option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Antigua and Barbuda">Antigua and Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaijan">Azerbaijan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
<option value="Belarus">Belarus</option>
<option value="Belgium">Belgium</option>
<option value="Belize">Belize</option>
<option value="Benin">Benin</option>
<option value="Bermuda">Bermuda</option>
<option value="Bhutan">Bhutan</option>
<option value="Bolivia">Bolivia</option>
<option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
<option value="Botswana">Botswana</option>
<option value="Brazil">Brazil</option>
<option value="Brunei">Brunei</option>
<option value="Bulgaria">Bulgaria</option>
<option value="Burkina Faso">Burkina Faso</option>
<option value="Burundi">Burundi</option>
<option value="Cambodia">Cambodia</option>
<option value="Cameroon">Cameroon</option>
<option value="Canada">Canada</option>
<option value="Cape Verde">Cape Verde</option>
<option value="Central African Republic">Central African Republic</option>
<option value="Chad">Chad</option>
<option value="Chile">Chile</option>
<option value="China">China</option>
<option value="Colombia">Colombia</option>
<option value="Comoros">Comoros</option>
<option value="Congo">Congo</option>
<option value="Costa Rica">Costa Rica</option>
<option value="Côte d'Ivoire">Côte d'Ivoire</option>
<option value="Croatia">Croatia</option>
<option value="Cuba">Cuba</option>
<option value="Cyprus">Cyprus</option>
<option value="Czech Republic">Czech Republic</option>
<option value="Denmark">Denmark</option>
<option value="Djibouti">Djibouti</option>
<option value="Dominica">Dominica</option>
<option value="Dominican Republic">Dominican Republic</option>
<option value="East Timor">East Timor</option>
<option value="Ecuador">Ecuador</option>
<option value="Egypt">Egypt</option>
<option value="El Salvador">El Salvador</option>
<option value="Equatorial Guinea">Equatorial Guinea</option>
<option value="Eritrea">Eritrea</option>
<option value="Estonia">Estonia</option>
<option value="Ethiopia">Ethiopia</option>
<option value="Fiji">Fiji</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="Gabon">Gabon</option>
<option value="Gambia">Gambia</option>
<option value="Georgia">Georgia</option>
<option value="Germany">Germany</option>
<option value="Ghana">Ghana</option>
<option value="Greece">Greece</option>
<option value="Grenada">Grenada</option>
<option value="Guatemala">Guatemala</option>
<option value="Guinea">Guinea</option>
<option value="Guinea-Bissau">Guinea-Bissau</option>
<option value="Guyana">Guyana</option>
<option value="Haiti">Haiti</option>
<option value="Honduras">Honduras</option>
<option value="Hong Kong">Hong Kong</option>
<option value="Hungary">Hungary</option>
<option value="Iceland">Iceland</option>
<option value="India">India</option>
<option value="Indonesia">Indonesia</option>
<option value="Iran">Iran</option>
<option value="Iraq">Iraq</option>
<option value="Ireland">Ireland</option>
<option value="Israel">Israel</option>
<option value="Italy">Italy</option>
<option value="Jamaica">Jamaica</option>
<option value="Japan">Japan</option>
<option value="Jordan">Jordan</option>
<option value="Kazakhstan">Kazakhstan</option>
<option value="Kenya">Kenya</option>
<option value="Kiribati">Kiribati</option>
<option value="North Korea">North Korea</option>
<option value="South Korea">South Korea</option>
<option value="Kuwait">Kuwait</option>
<option value="Kyrgyzstan">Kyrgyzstan</option>
<option value="Laos">Laos</option>
<option value="Latvia">Latvia</option>
<option value="Lebanon">Lebanon</option>
<option value="Lesotho">Lesotho</option>
<option value="Liberia">Liberia</option>
<option value="Libya">Libya</option>
<option value="Liechtenstein">Liechtenstein</option>
<option value="Lithuania">Lithuania</option>
<option value="Luxembourg">Luxembourg</option>
<option value="Macedonia">Macedonia</option>
<option value="Madagascar">Madagascar</option>
<option value="Malawi">Malawi</option>
<option value="Malaysia">Malaysia</option>
<option value="Maldives">Maldives</option>
<option value="Mali">Mali</option>
<option value="Malta">Malta</option>
<option value="Marshall Islands">Marshall Islands</option>
<option value="Mauritania">Mauritania</option>
<option value="Mauritius">Mauritius</option>
<option value="Mexico">Mexico</option>
<option value="Micronesia">Micronesia</option>
<option value="Moldova">Moldova</option>
<option value="Monaco">Monaco</option>
<option value="Mongolia">Mongolia</option>
<option value="Montenegro">Montenegro</option>
<option value="Morocco">Morocco</option>
<option value="Mozambique">Mozambique</option>
<option value="Myanmar">Myanmar</option>
<option value="Namibia">Namibia</option>
<option value="Nauru">Nauru</option>
<option value="Nepal">Nepal</option>
<option value="Netherlands">Netherlands</option>
<option value="New Zealand">New Zealand</option>
<option value="Nicaragua">Nicaragua</option>
<option value="Niger">Niger</option>
<option value="Nigeria">Nigeria</option>
<option value="Norway">Norway</option>
<option value="Oman">Oman</option>
<option value="Pakistan">Pakistan</option>
<option value="Palau">Palau</option>
<option value="Palestine">Palestine</option>
<option value="Panama">Panama</option>
<option value="Papua New Guinea">Papua New Guinea</option>
<option value="Paraguay">Paraguay</option>
<option value="Peru">Peru</option>
<option value="Philippines">Philippines</option>
<option value="Poland">Poland</option>
<option value="Portugal">Portugal</option>
<option value="Puerto Rico">Puerto Rico</option>
<option value="Qatar">Qatar</option>
<option value="Romania">Romania</option>
<option value="Russia">Russia</option>
<option value="Rwanda">Rwanda</option>
<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option>
<option value="Saint Lucia">Saint Lucia</option>
<option value="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option>
<option value="Samoa">Samoa</option>
<option value="San Marino">San Marino</option>
<option value="Sao Tome and Principe">Sao Tome and Principe</option>
<option value="Saudi Arabia">Saudi Arabia</option>
<option value="Senegal">Senegal</option>
<option value="Serbia and Montenegro">Serbia and Montenegro</option>
<option value="Seychelles">Seychelles</option>
<option value="Sierra Leone">Sierra Leone</option>
<option value="Singapore">Singapore</option>
<option value="Slovakia">Slovakia</option>
<option value="Slovenia">Slovenia</option>
<option value="Solomon Islands">Solomon Islands</option>
<option value="Somalia">Somalia</option>
<option value="South Africa">South Africa</option>
<option value="Spain">Spain</option>
<option value="Sri Lanka">Sri Lanka</option>
<option value="Sudan">Sudan</option>
<option value="Suriname">Suriname</option>
<option value="Swaziland">Swaziland</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
<option value="Syria">Syria</option>
<option value="Taiwan">Taiwan</option>
<option value="Tajikistan">Tajikistan</option>
<option value="Tanzania">Tanzania</option>
<option value="Thailand">Thailand</option>
<option value="Togo">Togo</option>
<option value="Tonga">Tonga</option>
<option value="Trinidad and Tobago">Trinidad and Tobago</option>
<option value="Tunisia">Tunisia</option>
<option value="Turkey">Turkey</option>
<option value="Turkmenistan">Turkmenistan</option>
<option value="Tuvalu">Tuvalu</option>
<option value="Uganda">Uganda</option>
<option value="Ukraine">Ukraine</option>
<option value="United Arab Emirates">United Arab Emirates</option>
<option value="United Kingdom">United Kingdom</option>
<option value="United States">United States</option>
<option value="Uruguay">Uruguay</option>
<option value="Uzbekistan">Uzbekistan</option>
<option value="Vanuatu">Vanuatu</option>
<option value="Vatican City">Vatican City</option>
<option value="Venezuela">Venezuela</option>
<option value="Vietnam">Vietnam</option>
<option value="Yemen">Yemen</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option>
	</select>
	<label for="Field7">Country</label>
	</span>
</div>
</li>


<li id="foli8" 		class="phone leftHalf    ">
<label class="desc" id="title8" for="Field8">
	Phone
		</label>
<span>
	<input id="Field8" 			name="Field8" 			type="text" 			class="field text" 			value="" 	 			maxlength="12" 			tabindex="9" 			/>
	<label for="Field8"></label>
</span>

</li>


<li id="foli9" 		class="rightHalf    ">
<label class="desc" id="title9" for="Field9">
	Email
		</label>
<div>
	<input id="Field9" 			name="Field9" 			type="text" 			class="field text medium" 			value="" 			maxlength="255" 			tabindex="12" 			/> 
</div>
</li>

<li id="foli9" 		class="rightHalf    ">
<label class="desc" id="title9" for="Field9">
	Password
		</label>
<div>
	<input id="Field10" 			name="Field10" 			type="text" 			class="field text medium" 			value="" 			maxlength="255" 			tabindex="12" 			/> 
</div>
</li>

<li id="foli9" 		class="leftHalf    ">
<label class="desc" id="title9" for="Field9">
	Username
		</label>
<div>
	<input id="username" 			name="username" 			type="text" 			class="field text medium" 			value="" 			maxlength="255" 			tabindex="12" 			/> 
   <span id="msgbox" style="display:none"></span>
</div>
</li>



<li class="buttons">
			<input id="saveForm" class="btTxt submit" type="submit" value="Submit" />
		</li>

<li style="display:none">
		</li>
</ul>
</form>

</div><!--container-->
<img id="bottom" src="includes/CSS/images/bottom.png" alt="" />

</body>
</html>

 

can anyone see a problem ?

 

Many thanks,

 

James.

Link to comment
Share on other sites

you cant do this

$check_sql = "SELECT `username` FROM `members`" or die (mysql_error());
$check_query = mysql_query($check_sql);

 

do this

$check_sql = "SELECT `username` FROM `members`";
$check_query = mysql_query($check_sql) or die(mysql_error());

i think :)

Link to comment
Share on other sites

you cant do this

$check_sql = "SELECT `username` FROM `members`" or die (mysql_error());
$check_query = mysql_query($check_sql);

 

do this

$check_sql = "SELECT `username` FROM `members`";
$check_query = mysql_query($check_sql) or die(mysql_error());

i think :)

 

Still dont work =/

 

i may just give up on it =/

Link to comment
Share on other sites

Just do this:

 

$check_sql = "SELECT `username` FROM `members` WHERE username='$username'";
$check_query = mysql_query($check_sql);

echo (mysql_num_rows($check_query)==0) ? 'yes' : 'no';

 

 

Link to comment
Share on other sites

There is still another problem. In the script that checks for availability you are doing two checks. The first checks for a specific user name (makes sense). I'm not even sure what the second check does. Based upon the logic it checks if the username being checked is the LAST username in the results of the last query. I see not value in this check.

 

Anyway, you have two IF/ELSE checks that will both output "no" or "yes". So the result from that script will always have TWO responses that are concatenated together and will be one of the following

- nono

- noyes

- yesno

- yesyes

 

Then in your JavaScript code you only check to see if the result is "no". If so then it states the username is unavailable. Otherwise it states the username is available.

 

1. First fixe the script to ONLY output a "true" if the username is OK to use and "false" otherwise (true/false is a better programming standard than yes/no) - without the duplication.

 

2. Change the JavaScript to look for a positive response to show the username is available, and make the unavailable response for the "else" condition. The way it is currently coded if there is an error in the script it will show the username as available. Or, alternatively, you could have three conditions: 1) true - username is available, 2) false - username is not available, 3) "Else" - any thing not a yes or no will result in an apropriate erro message being displayed.

 

In any case - test your server-side script before trying to implement the AJAX call. If you did I'm sure you would have uncovered several of these errors already.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.