i want to check if the cell number a user is trying to enter already exists. But the code below always calls this "Cell Number in use",
what im i doing wrong? Still starting with php.
<?php
mysql_connect("localhost", "root", "password", "users");
mysql_select_db("users");
$cell = (isset($_POST["cell"])?
$_POST["cell"] : null);
$query=mysql_query("SELECT * from users where cell='$cell' ");
$find=mysql_num_rows($query);
echo $find;
?>
<script>
$(document).ready(function(){
$("#Cell").blur(function(){
$("#Status_Cell").show();
$("#Status_Cell").html("checking...");
var cell = $("#Cell").val();
$.ajax({
type:"post",
url:"formpost",
data:"cell="+cell,
success:function(data){
if(data==0){
$("#Status_Cell").html("Cell Number available");
}
else{
$("#Status_Cell").html("Cell Number in use");
}
}
});
});
});
</script>
I see your post url is: url:"formpost"
if js code and the php in the same file, the return data of ajax will be whole html and javascript instead of just "echo $find;"; of course your data will never be 0;
First thing to do is to see what is the value of data
returned. That will give you some idea where the problem is. Also, it is better to use count()
than to get all rows just so you can do mysql_num_rows()
.
SELECT COUNT(*) from users where cell='$cell'
will return the number of rows with cell that equals $cell
. Interpret that result and then echo what you need. Also check if query was successful (handle errors). If the $_POST['cell']
is not set there is no reason to query the database for cell number that is null
, just return desired value immediately. You also got comments that you are vulnerable to sql injections so you should consider fixing that too.
This should work:
<?php
if(isset($_POST['cell'])) {
mysql_connect("localhost", "root", "password", "users");
mysql_select_db("users");
$cell = (isset($_POST["cell"])?
$_POST["cell"] : null);
$query=mysql_query("SELECT * from users where cell='$cell' LIMIT 1");
$find=mysql_num_rows($query);
die($find);
}
?>
<script>
$(document).ready(function(){
$("#Cell").blur(function(){
$("#Status_Cell").show();
$("#Status_Cell").html("checking...");
var cell = $("#Cell").val();
$.ajax({
type:"post",
url:"formpost",
data:"cell="+cell,
success:function(data){
if(data==0){
$("#Status_Cell").html("Cell Number available");
}
else{
$("#Status_Cell").html("Cell Number in use");
}
}
});
});
});
</script>
So if you send "data" to the script it'll look for results, output the count, and exit.
If not, it'll display the rest of the page.
Also note how I put a LIMIT 1 there, just a little performance optimization, since you only want to know if the data is there or not.
Edit: This is a better approach in terms of performance and security:
<?php
if(isset($_POST['cell'])) {
mysql_connect("localhost", "root", "password", "users");
mysql_select_db("users");
$cell = $_POST["cell"];
$query = mysql_query("SELECT COUNT(*) AS number from users where cell='".mysql_real_escape_string($cell)."' LIMIT 1");
$find = mysql_result($query, 0);
die($find);
}
?>
if you are receiving the result from the same page in your ajax call its as if you have browsed to the page so the returned content will include everything on the page including your javascript. Try either using a different page as the ajax target or doing somthing like this:
<?php
if(isset($_POST["cell"]))
{
mysql_connect("localhost", "root", "password", "users");
mysql_select_db("users");
$cell = $_POST["cell"];
$query=mysql_query("SELECT * from users where cell='$cell' ");
$find=mysql_num_rows($query);
echo $find;
}
else
{
?>
<script>
$(document).ready(function(){
$("#Cell").blur(function(){
$("#Status_Cell").show();
$("#Status_Cell").html("checking...");
var cell = $("#Cell").val();
$.ajax({
type:"post",
url:"formpost",
data:"cell="+cell,
success:function(data){
if(data==0){
$("#Status_Cell").html("Cell Number available");
}
else{
$("#Status_Cell").html("Cell Number in use");
}
}
});
});
});
</script>
<?php } ?>
Which should mean that the javascript is only written out if you dont have a post value.