Page 1 of 1

Dynamic javascript with php and mysql

Posted: Fri Feb 14, 2003 9:29 am
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

Posted: Fri Feb 14, 2003 3:12 pm
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>";
}
?>

Posted: Fri Feb 14, 2003 4:22 pm
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

Posted: Mon Feb 17, 2003 2:24 am
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

Posted: Mon Feb 17, 2003 10:00 am
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