Click to See Complete Forum and Search --> : Using server-side database with Javascript script


brianilland
08-12-2004, 04:49 AM
Hi all,

Finally got a bit of javascript working, which works out a quick and dirty client side online quote. Now, the whole javascript works based on the value of a dropdown within Option tags. The way Ive done it I've hardcoded it below :

value="Product/Size/Price"
eg. value="My Product/3.14/35.60"

Now, ideally what I would like to do is to remove all the hardcoded references to product/size/price and add load them from a mySQL database on the server using PHP. My site is a mambo/php one, so I can create a database ok with the details using phpmyAdmin.

Does anyone have any examples of code where this is done - i.e - where PHP reads in values from a database to populate as variables within a javascript.

OK, maybe that's more of a php question - more on the topic of javascript, can you tell me how I can at least populate the 'value=' with a variable, so I could define

dropdown1value = "My Product/3.14/35.60"

in the header section, and then

value=dropdownvalue1;

in the form option tags.......I tried using document.write but I think it doesn't like the '/'....?

Thanks in advance....

Daniel T
08-12-2004, 04:57 AM
Originally posted by brianilland
I tried using document.write but I think it doesn't like the '/'....?
You must excape slashes: replace / with \/

As for using PHP to define JS variables, you could do something along the lines of this:<script type="text/javascript">
avariable = <?php $myrow = mysql_fetch_row(mysql_query("SELECT * FROM $table WHERE `ID` = $id")); echo $myrow[0]; ?>;
</script>
Of course, this is a very vague example, and you'd have to change the PHP/MySQL part to work with your situation.

brianilland
08-12-2004, 05:48 AM
Nice one. That'll give me somewhere to start. Thanks!