NYCPHP Meetup

NYPHP.org

[nycphp-talk] A question about run php variable in javascript

Bruce Martin bmartin at mac.com
Thu May 1 14:40:40 EDT 2008


I ran into this issue too, what I ended up doing was using the  
XMLHttpRequest in JavaScript to call my PHP script, which after  
getting the data from the database, it caches the data in an array.  
so on subsequent call, used in a mouseOver(), it grabs the info from  
the array.

So First my JavaScript runs on mouseOver.
It checks the array, if defined then it uses that value, if not then  
I hit the php code.
After that it sets the value of the array to the data I just got and  
then it's local until they user goes to another page anyway.

Bruce Martin
The Martin Solution
bruce at martinsolution.com



On May 1, 2008, at 11:59 AM, chad qian wrote:

> I need to run php to get street from mysql database.
> Then test the street variable in javascript
> if street is emty,javascript will pop up alert window
>
> Here are my thoughts:
>
> <?php
> //connection to server,database
>
> $q = "select street from apt where username = '$user'";
> $result = mysql_query($q) or die(mysql_error());
> while($row = mysql_fetch_array($result)){
>   $street=$row['street'];
> }
> ?>
> <script LANGUAGE="JavaScript">
> st=<?=$street?>;
> if(st)
>  alert("miss street");
> </script>
>
> Can I run st=<?=street?> in javascript?I'm not sure here.
>
> Thanks!
>
> chad
>
> In a rush? Get real-time answers with Windows Live Messenger.
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20080501/ce965867/attachment.html>


More information about the talk mailing list