skatermike21988 Posted October 16, 2006 Share Posted October 16, 2006 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 changedand 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 More sharing options...
skatermike21988 Posted October 16, 2006 Author Share Posted October 16, 2006 any ideas, i have been googleing on it for hour and hours now, it is driving me insane!! Link to comment https://forums.phpfreaks.com/topic/24063-php-scripting-with-some-javascript/#findComment-109330 Share on other sites More sharing options...
pndof12006 Posted October 16, 2006 Share Posted October 16, 2006 i did this once. what i did was set cookies and used a lil PHP example:[code]<? setcookie(color,$colorcode,+3600)<body bgcolor="<? echo #$_COOKIE[$colorcode];>">?>[/code] Link to comment https://forums.phpfreaks.com/topic/24063-php-scripting-with-some-javascript/#findComment-109333 Share on other sites More sharing options...
akitchin Posted October 16, 2006 Share Posted October 16, 2006 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 https://forums.phpfreaks.com/topic/24063-php-scripting-with-some-javascript/#findComment-109342 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.