hey guys,

This question might be discussed over thousand times but please help me on this one!
I have a customer registration (html) page on which I have included form level validations using java script. On click of the submit of this page control goes to regist.php where I run a query to insert records into mysql database.

I have a userid field on the html page which I want to validate against database of mysql.

How can I get the php array from mysql database of existing userids and compare it using javascript to the one recently entered?

Please explain,

Thanks

ajax, or use php to validate your form.

i am leaving so i wont be able to provide examples right now. hopefully someone else can help, i was just trying to point you in the right direction.

You can use a php loop to fill a javascript array, something like this:

<script language="javascript" type="text/javascript">
var jsarray = Array(<?
$jsarray = "";
while ($row = mysql_fetch_assoc($result))
{
	$jsarray .= "\"" . $row['id'] . "\", ";
}
$jsarray = substr($jsarray, 0, -2); //eliminate the last ", "
echo $jsarray;
?>);
</script>

Then you that js array that you can compare to your original js array.

thanks a lot!

Thanks a lot to both of you.
R0bb0b,
I have couple of questions.

Should we write the above code in a function in the <head> section?
Should we connect to the database in the php code itself?

thanks,

Yes,we can write our script code in head tags...
And database connection is like this:

<?
global $link; 
$link = mysql_connect('localhost', 'root', '1234') ; // Connection
mysql_select_db("mydb") or die(mysql_error()); // Selection of database

//for closing
<?
if(isset($link))
{
mysql_close($link);
}
?>

?>

We can write database connection in our php page at top of the page by using <? ?> tags...

Thanks a lot to both of you.
R0bb0b,
I have couple of questions.

Should we write the above code in a function in the <head> section?
Should we connect to the database in the php code itself?

thanks,

That's probably what I would do is put it in a function and then you can call it anywhere. You don't necessary have to call it in the head but that's where I would do it. Tested this on my database and it works fine:

<?php
include("inc/functions.inc");
$db = dbconn();

function getJSArray()
{
	global $db;
	$return = "";
	$query = "select texttagpk from texttag";
	$result = mysql_query($query, $db);
	$return .= '<script language="javascript" type="text/javascript">
				var jsarray = new Array(';
	while ($row = mysql_fetch_assoc($result))
	{
		$return .= "\"" . $row['texttagpk'] . "\", ";
	}
	$return = substr($return, 0, -2); //eliminate the last ", "
	$return .= '); </script>';
	return $return;
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?= getJSArray(); ?>
</head>

<body>
</body>
</html>

Thanks guys. I appreciate your help.

This is what I have done so far...
a 'dbcon.inc' file

<?php


function getData()
{
//connect to the database
$db = mysql_connect("localhost","root","");
mysql_select_db("xyz", $db);



}
?>

a html file to create array

<?php


include("dbcon.inc");


$db = getData();


function getJSArray()


{


global $db;


$return = "";


$query = "select userID from customer";


$result = mysql_query($query, $db);


$return .= '  <script language="javascript" type="text/javascript">
var jsarray = new Array( ';


while ($row = mysql_fetch_assoc($result))


{


$return .= "\"" . $row . "\", ";


}


$return .= '); </script>';


return $return;


}


?>


<!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>


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>Untitled Document</title>


<?= getJSArray(); ?>


</head>


<body>


</body>
</html>

but when I run this html ...............
I get this printed on the page....

var jsarray = new Array() '; while ($row = mysql_fetch_assoc($result)) { $return .= "\"" . $row . "\", "; } $return .= '); '; return $return; } ?>

What is that I am missing?

Thanks,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.