•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,626 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 198 | Replies: 7
![]() |
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 535
Reputation:
Rep Power: 3
Solved Threads: 54
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation:
Rep Power: 2
Solved Threads: 50
You can use a php loop to fill a javascript array, something like this:
Then you that js array that you can compare to your original js array.
php Syntax (Toggle Plain Text)
<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.
Last edited by R0bb0b : Jul 9th, 2008 at 10:04 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
•
•
Join Date: Jul 2008
Location: Hyderabad,India.
Posts: 522
Reputation:
Rep Power: 2
Solved Threads: 53
Yes,we can write our script code in head tags...
And database connection is like this:
We can write database connection in our php page at top of the page by using <? ?> tags...
And database connection is like this:
php Syntax (Toggle Plain Text)
<? 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...
Last edited by Shanti Chepuru : Jul 10th, 2008 at 12:46 am.
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation:
Rep Power: 2
Solved Threads: 50
•
•
•
•
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 Syntax (Toggle Plain Text)
<?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>
Last edited by R0bb0b : Jul 10th, 2008 at 1:26 am.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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['userID'] . "\", ";
}
$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['userID'] . "\", "; } $return .= '); '; return $return; } ?>
What is that I am missing?
Thanks,
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['userID'] . "\", ";
}
$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['userID'] . "\", "; } $return .= '); '; return $return; } ?>
What is that I am missing?
Thanks,
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- how to access accessing PHP variable in JavaScript (PHP)
- Help -regarding Loading Labels In Html From Xml Using Javascript Or Php (JavaScript / DHTML / AJAX)
- php/mySQL full time position (Web Development Job Offers)
- PHP Form mailing troubles (PHP)
- converting from javascript to HTML? (Java)
- embedding java script in php (PHP)
Other Threads in the PHP Forum
- Previous Thread: PHP and TPL file, variable doesn't work
- Next Thread: Looking for specialized ecommerce software


Linear Mode