Jump to content

Php scripting with some javascript.


skatermike21988

Recommended Posts

Ok, i have a simple background color changer, well what i want to do is have it set the color they select as a session variable so when they go to another page the color hasn't changed back to normal.

here is my simple javascript code:
[code]
function bgcolor(color) {
document.bgColor=color;
}
[/code]

and here is how my links are:
[code]
<a href='#' onclick="bgcolor('blue');" id='color' value='blue'>Blue</a>
<a href='#' onclick="bgcolor('black');" id='color' value='blue'>Black</a>
//etc. etc.
[/code]

so i need to get the color set into a session variable that will update when the color is changed

and then i can just have like so
[code]
<?
$color="$_SESSION[color]";
?>
<body onload="javascript:bgcolor("<? echo $color; ?>");">
[/code]

All help apreciated.
Link to comment
https://forums.phpfreaks.com/topic/24063-php-scripting-with-some-javascript/
Share on other sites

his syntax is ridiculously wrong, do not use his code.

it's hard to pass vars from javascript to PHP, but quite a bit easier to go the other way around.  if you can submit a form or send them to a link where the 'color' variable is defined, you can use:

[code]$_SESSION['bg_color'] = $_POST['color'] (or $_GET['color'] if you sent them to a link);[/code]

and in the body tag, simply echo this value into your javascript function:

[code]<body onload="javascript:bgcolor('<?php echo $_SESSION['bg_color']; ?>');">[/code]

keep in mind you need to have session_start() on all the pages you want to carry this color over onto.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.