PDA

View Full Version : AJAX: UserName is Available



leonidassavvides
07-27-2009, 10:23 PM
In AJAX in a Registration Form : to appear "UserName is Available" I must query the database upon after what event (PHP), user stop enters chars in Input Field ? If yes, what event is it this ? This AJAX & PHP code (both) is visible (source) from browser ?

thetestingsite
07-28-2009, 12:20 AM
Take a look at this page:
http://www.shawngo.com/gafyd/index.html

You could modify the PHP script to use a Database instead of a CSV file very easily, but if you need help with that, let us know.

Hope this helps.

leonidassavvides
07-28-2009, 04:47 AM
You mean needed change with DATABASE QUERY only the check.php ?

The jquery.js is it needed any change at all ?
I must attach it to form webpage ?
form.name="register" ,
field.name="email"

leonidassavvides
07-28-2009, 04:50 AM
have to delete anything unneeded from jquery.js ?

leonidassavvides
07-28-2009, 09:37 AM
query error appear , is it ajax code without problems ?

error
"
Unable to execute query. Please try again later. error3
"
at line 14 (attached check.php code)

see at

http://www.poliscarhire.com/customeradmin/register-acBETA-ajax.php

email(username) field to be checked ... thks again!

leonidassavvides
07-28-2009, 09:49 AM
email addresses are appropriate for username form input field for this to work?

thetestingsite
07-28-2009, 02:38 PM
In the php code, you have to define the variable $CustomersTable either as a variable or just insert it into the code:



$CustomersTable = 'Customers';
$queryajax = "SELECT * FROM $CustomersTable";

$queryajax = "SELECT * FROM `Customers`";

Hope this helps.

The rest of the files do not need to be changed. Just leave them as is. (it appears as though the ajax portion is working on the form field, it is just returning an php error.

leonidassavvides
07-29-2009, 01:09 PM
please note

$CustomersTable = 'Customers';

is included in dbinfo.php file I include ...

I tried below but after REMOVING die statement ...now ONLY
Username Available
shows whatever I insert...I use dmwr cs4 ...

see attachments

leonidassavvides
07-29-2009, 01:13 PM
Must be something about below $f declarations->
How I declare a var only in a function valid ?

$f = "n"; // outside function

if (strtolower($username) == strtolower($row['email'])) { // in function
$f = "y";
break; // break works here

seems $f=is always value of 'n'

well ?

thetestingsite
07-29-2009, 03:31 PM
See if this works for you.



<?php
error_reporting(6147); //change 6147 to 0 after testing. never use @ to suppress errors.

function check_username($username) {

include("../dbinfo.php");

$linkidajax = mysql_connect($hostname,$username,$password) or die( "Unable to connect to Database Server. Please try again later.");

mysql_select_db($database,$linkidajax) or die( "Unable to select database. Please try again later,"); // @

$queryajax = "SELECT email,status FROM $CustomersTable WHERE status='active'";

//$CustomersTable=customers2; in `customers2`

$resultajax = mysql_query($queryajax,$linkidajax) or die('Unable to execute query. Please try again later. error3');

// I removed and worked or die("Unable to execute query. Please try again later. error3");
$count = @mysql_num_rows($resultajax);
// echo $count;$f = "n";

for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_assoc($resultajax);

if (strtolower($username) == strtolower($row['email'])) {

// $email == $row[1] $row['email'] && $row['status'] == "active"
$f = "y";
break;
}

}

if ($f == "y") {
return '<span style="color:#f00">Username Unavailable</span>';
}
else {
return '<span style="color:#0c0">Username Available</span>';
}

mysql_free_result($resultajax);
mysql_close($linkidajax);
}

echo check_username($_POST['username']); // call the check_username function and echo the results.$file,$file_in,
?>


Hope this helps.

leonidassavvides
07-29-2009, 06:07 PM
I try everything but below is always FALSE whatever...


if (strtolower($row['email']) == strtolower($username)) { echo 'true'; } else { echo 'false'; };


now I did some debugging
$rowajax = @mysql_fetch_assoc($resultajax); // in for loop
$myarray[$j]=$rowajax['email']; // in for loop

In other code-->
if ($f == "y") {
return '<span style="color:#f00">Username Unavailable</span>';
} else {
echo $myarray[1];
echo strtolower($username);
return '<span style="color:#0c0">Username Available</span>';
}


but ONLY prints echo strtolower($username); [B]and NOT the array ?

leonidassavvides
07-29-2009, 06:15 PM
Also I checked

"a080725@cytanet.com.cy" == strtolower($username)

inside loop with field="a080725@cytanet.com.cy"

but also didn't work

hence the problem with one of statements below :

$f = "y";
break;

break statement alternatively exist ? what try ?

leonidassavvides
07-29-2009, 09:19 PM
further tests I make show:problem in scope of var[for loop since of the two assignments neither pass: $f="y";]...how do it not only in for loop valid ?
since below the $f="y"; does NOT pass by anyway but rather pass initial $f="n"; for any value in text field ?



for ($j=0;$j<$count;$j++) {
$rowajax = @mysql_fetch_assoc($resultajax);
//$myarray[$j]=$rowajax['email'];

if (strtolower($rowajax['email']) == strtolower($username)) { // $email == $row[1] "a080725@cytanet.com.cy" $row['email'] strtolower($rowajax['email']) && $row['status'] == "active"
$f = "y";
break;
}
$f="y";
}

leonidassavvides
07-31-2009, 10:11 PM
database table field must be not 'email' but 'username' ?
how I check a query, if it produces data in a php/ajax script like check.php ?

leonidassavvides
08-02-2009, 08:55 AM
problem Solved At Last, Thks for any help...

the point was to insert the:

include("../x.php");
$linkidajax = mysql_connect($hostname,$username,$password) or die( "Unable to connect to Database Server. Please try again later.");
mysql_select_db($database,$linkidajax) or die( "Unable to select database. Please try again later,");

inside the function...