This topic is locked
[SOLVED]

 SQL & javascript

6/29/2011 9:08:20 AM
PHPRunner General questions
jclabuschagne author

I have a need to access SQL data (through CustomQuery), and use this data in Javascript to manipulate the style of the body(change background image).
More details: When a registered user selects a certain value from a dropdown box, the page background should load a new background image. Then when a guest loads another page, this page must check what the Registered User selected (through sql query) and change the style accordingly.
Is there a way to do this?
There is a "how to access PHP variables" document in the help files, but I gather that this is for already initialized data.

C
cgphp 6/29/2011

Johan MMPONLINE,
using a dropdown, a custom button and a PHP snippet you can:

  • update db in the server section of the button based on the dropdown selection (for registered user)
  • in the PHP snippet get the new value from the db and set a var like explained in the "how to access PHP variables" tutorial (for guest user)
  • in the javascript OnLoad event change the background color of the page using the new js var (for guest user)

jclabuschagne author 7/1/2011



Johan MMPONLINE,
using a dropdown, a custom button and a PHP snippet you can:

  • update db in the server section of the button based on the dropdown selection (for registered user)
  • in the PHP snippet get the new value from the db and set a var like explained in the "how to access PHP variables" tutorial (for guest user)
  • in the javascript OnLoad event change the background color of the page using the new js var (for guest user)


Thanks, you put me on the right track: Here is what I did,
Added a

if ($_REQUEST["Func"])

{

$sql="select type,theme from functions where functionID=".$_REQUEST["Func"];

$rs=CustomQuery($sql);

$data=db_fetch_array($rs);

echo "<script>var type='" . $data["type"] . "';</script>";
}


Then in [color="#FF8C00"]Javascript Onload event:

switch (type)

{

case "Wedding":

document.body.style.backgroundImage="url(images/wedding.jpg)";

break;

case "Corporate":

document.body.style.backgroundImage="url(images/corporate.jpg)";

break;

case "Birthday":

document.body.style.backgroundImage="url(images/birthday.jpg)";

break;
default:

document.body.style.backgroundImage="url(images/empty.jpg)";

}