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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.