12 replies [Last post]
clam
clam's picture
Offline
Regular
Last seen: 15 years 14 weeks ago
Joined: 2006-10-17
Posts: 11
Points: 0

I know there are ways posted out there on how to do this, but I wanted to try my own way. It's a fairly simple idea:

a link "change style" calls a javascript function, which submits a hidden form that posts back to the current page. the php script checks if the value $_POST[mystyle] is not null, and if it is not, assigns it to a session variable which is then used to determine the stylesheet to load.

I know the session variable to determine the style sheet works fine, and I know the javascript function is being called fine. the problem seems to be, the $_POST[mystyle] value is always null.

I realise this forum is more focused purely on CSS and this is more a php/html problem, but I figured it might interest some people, and there are plenty of people capable enough on here to spot my error, so please help! =)

<?php if ($_POST[mystyle] != NULL) $_SESSION['mystylesheet'] = $_POST[mystyle]; ?>

/>

Switch to no style sheet

Tags:
Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 31 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Fair enough asking but

Fair enough asking but couldn't you have chosen an appropriate forum section?

Moving to 'Off Topic'.

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 31 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

At a quick glance are you

At a quick glance are you sure that $_POST['mystyle'] is actually empty? have you tried testing it? printing out the variable. You don't appear to have started a session and as you're calling you post variable directly into a session variable I would have thought it would return as empty.

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 31 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

When testing your scripts do

When testing your scripts do you run error reporting?
error_reporting(E_ALL)

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 31 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Scrub the session_start()

Scrub the session_start() although you will need it if you want to access the session on subsequent pages.

It looks as though your javascript function is at fault as a form input submit works ok.

<?php
//session_start();
//error_reporting(E_ALL);

if ($_POST[mystyle] != NULL) $_SESSION['mystylesheet'] = $_POST[mystyle];

?>

/>

<?php echo "$_SESSION[mystylesheet]"; ?>

" method ="post" >

Switch to no style sheet

This though kicks the javascript into life although I'm not sure why the javascript is being used.

function stylechange()
{
alert("function called ok!");
document.stylechangeform.submit();
}

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 9 years 47 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Clam, consider using a

Clam, consider using a cookie rather than a hidden form and session variables. Javascript can set the cookie value, it'll be passed back to the page with every page request for that particular domain. That enables your php to set the appropriate style sheet for new page requests and your javascript to do it live.

clam
clam's picture
Offline
Regular
Last seen: 15 years 14 weeks ago
Joined: 2006-10-17
Posts: 11
Points: 0

thanks for all the replies,

thanks for all the replies, everyone. As soon as I get home I will try some of these ideas out and see what i come up with.

That was however, just a snippet of my code, I am starting the session earlier.
Come to think of it though, using javascript to create the cookies would probably make more sense than using php. I only recently started learning PHP and I think I got overly excited about sessions =)

also, sorry about posting in the wrong forum, I wasn't really sure. I am still learning CSS but hope soon to be an active member of these forums and provide some help for others.

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 31 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Don't worry about posting to

Don't worry about posting to the wrong area.

regards sessions I think many get carried away with them and style switching , I know I spent ages formulating little switchers based around session cookies in the belief that with cookies disabled one would still be able to carry the information via PHP's trans sid function but it's not seemingly that simple, I've got simple session working and with cookies disabled the SID is appended to the url but there are problems such as the fact that if using a separate script to get and set and then return via header location you find that the session is not carried over and you have to manually append the SID which seems to cause other problems and when your original page parses a session_start can ovewrite the set one, all sorts of little problems arise as well as the fact that of course we are taught that passing sessions around via urls is naughty as the great god google doesn't like it, I just found that it was a pipe dream trying to use sessions in this way and have just settled on plain vanilla cookies for my final magnus opus styleswitcher.

Have to admit though I'm happier not using javascript, but that may well be naive of me, but I recently got a wee bit fed up with a well known javascript switcher.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

clam
clam's picture
Offline
Regular
Last seen: 15 years 14 weeks ago
Joined: 2006-10-17
Posts: 11
Points: 0

thanks again for the

thanks again for the replies. I decided to persist instead of switching to purely javascript cookies, because I have some ideas about where I want to go with this script. I fixed it though...

the problem?

changestyle.submit(); //obviously not the same as:
document.changestyle.submit();

I obviously don't know too much about Javascript and the DOM, etc...

Well, like you mentioned, Hugo, since it probably won't work with session rewriting, I am just using cookies anyway, so it doesnt help if someone has their cookies disabled, but it was a learning experience.

It is also a lot less code than some of the javascript ones I have seen out there, and I like knowing I built it, and that the work mainly occurs at the server side.

clam
clam's picture
Offline
Regular
Last seen: 15 years 14 weeks ago
Joined: 2006-10-17
Posts: 11
Points: 0

Well it works, so mystery

Well it works, so mystery solved. However, if anyone feels like poking holes in it's design, please be my guest.

<?php // Default sheet if (!isset($_SESSION['mystylesheet'])) $_SESSION['mystylesheet']= "styles/stylin.css";

// Check for change
if ($_POST[mystyle] != NULL) $_SESSION['mystylesheet']= $_POST[mystyle];
?>

Style Switcher
/>




Style Switcher



Current Style: <?php echo "$_SESSION[mystylesheet]"; ?>



  • Style One

  • Style Two

chiwank
Offline
newbie
indonesia
Last seen: 15 years 42 weeks ago
indonesia
Joined: 2006-10-27
Posts: 1
Points: 0

edit my templates friendster

hi, my name is chiwank
would u help my how to change my templates friendster with using HTML code..?

thanks b4
chiwank

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 31 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Why have you posted in some

Why have you posted in some one else's thread?

Please post your question in the appropriate forum section.

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

thepineapplehead
thepineapplehead's picture
Offline
Moderator
Last seen: 16 weeks 2 hours ago
Timezone: GMT+1
Joined: 2004-06-30
Posts: 9683
Points: 819

Oh my dear sweet Lord . . .

Oh my dear sweet Lord . . .

Verschwindende wrote:
  • CSS doesn't make pies