User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2008
Posts: 4
Reputation: wdev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wdev wdev is offline Offline
Newbie Poster

javascript and php

  #1  
Jul 9th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 535
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 54
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: javascript and php

  #2  
Jul 9th, 2008
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.
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: javascript and php

  #3  
Jul 9th, 2008
You can use a php loop to fill a javascript array, something like this:
  1. <script language="javascript" type="text/javascript">
  2. var jsarray = Array(<?
  3. $jsarray = "";
  4. while ($row = mysql_fetch_assoc($result))
  5. {
  6. $jsarray .= "\"" . $row['id'] . "\", ";
  7. }
  8. $jsarray = substr($jsarray, 0, -2); //eliminate the last ", "
  9. echo $jsarray;
  10. ?>);
  11. </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
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: wdev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wdev wdev is offline Offline
Newbie Poster

Re: javascript and php

  #4  
Jul 10th, 2008
thanks a lot!
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: wdev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wdev wdev is offline Offline
Newbie Poster

Re: javascript and php

  #5  
Jul 10th, 2008
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,
Reply With Quote  
Join Date: Jul 2008
Location: Hyderabad,India.
Posts: 522
Reputation: Shanti Chepuru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 53
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Posting Pro

Re: javascript and php

  #6  
Jul 10th, 2008
Yes,we can write our script code in head tags...
And database connection is like this:
  1. <?
  2. global $link;
  3. $link = mysql_connect('localhost', 'root', '1234') ; // Connection
  4. mysql_select_db("mydb") or die(mysql_error()); // Selection of database
  5.  
  6. //for closing
  7. <?
  8. if(isset($link))
  9. {
  10. mysql_close($link);
  11. }
  12. ?>
  13.  
  14. ?>

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.
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: javascript and php

  #7  
Jul 10th, 2008
Originally Posted by wdev View Post
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:
  1. <?php
  2. include("inc/functions.inc");
  3. $db = dbconn();
  4.  
  5. function getJSArray()
  6. {
  7. global $db;
  8. $return = "";
  9. $query = "select texttagpk from texttag";
  10. $result = mysql_query($query, $db);
  11. $return .= '<script language="javascript" type="text/javascript">
  12. var jsarray = new Array(';
  13. while ($row = mysql_fetch_assoc($result))
  14. {
  15. $return .= "\"" . $row['texttagpk'] . "\", ";
  16. }
  17. $return = substr($return, 0, -2); //eliminate the last ", "
  18. $return .= '); </script>';
  19. return $return;
  20. }
  21. ?>
  22. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  23. <html xmlns="http://www.w3.org/1999/xhtml">
  24. <head>
  25. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  26. <title>Untitled Document</title>
  27. <?= getJSArray(); ?>
  28. </head>
  29.  
  30. <body>
  31. </body>
  32. </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
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: wdev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wdev wdev is offline Offline
Newbie Poster

Re: javascript and php

  #8  
Jul 10th, 2008
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,
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC