Dynamic javascript with php and mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Dynamic javascript with php and mysql

Post by oldtimer »

Is there a way to make javascript be built on the fly with php and mysql. I have a bid program I made for my store and i had to hard code in the javascript like

Code: Select all

<script language="JavaScript">
a_HDDid&#1111;1] = "15";
a_HDDprice&#1111;1] = "84";
</script>
Now on this one the id[1] would be an array out of all the entries for that one. 15 is the mysql id row and 84 is the price. right now the price is all java but the descriptions and the rest of the information is all through php/mysql. Would be much nicer to update prices via web interface than though updating my java script code every time.

Jerry
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

Just switch between JS & PHP.

Code: Select all

<?php
$qry="SELECT id, price FROM Table";
$result=mysql_query($qry,$conn);
while($row=mysql_fetch_object($result)){
  echo "<script language="javascript">";
  echo "a_HDDid[1]=".$row->id;
  echo "a_HDDprice[1]=".$row->price;
  echo "</script>";
}
?>
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Think I am going to try to echo the id into the hddid[] too instead of the 1 ,2, 3 etc and see if that works. :) one less array to work with.


Thanks for that BTW. I tried it before but did not look like that.

Jerry
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

daven wrote:Just switch between JS & PHP.
Just to be technical - you are not actually switching between PHP and JS, instead you are using PHP to echo out the JS which means you can use PHP to determine what variables are given to the JS.

viewtopic.php?t=1030

Mac
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I looked at what I did before and it was different. This way works great. before I was trying to echo out the variable inside the javascirpt(It dont work ) but echoing everything works.

Although after I got it all transformed this time my prices were not calculating. After printing off 11 sheets of code I found my problem. I had JavScript, deleted the A on accident hehehe.

Anyway all is working great and I built myself a webinterface to handle all the products and even add some. Thanks for the help.

Jerry
Post Reply