Button to change variable string value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andycole
Forum Newbie
Posts: 3
Joined: Wed Aug 02, 2006 11:12 am

Button to change variable string value

Post by andycole »

Hi all,

I'm currently looking to make a simple php script for my site which changes the value of a variable with the press of a button. The script is aimed at changing the CSS stylesheet of my site with a different one the way I 'd like to do do this is by using an echo of a variable inside the stylesheet path in the head tag.

For example a variable named $style1 with a value of "main.css", and when the button is pressed the value of $style1 changes to "second.css" therefore changing the path to the css file to a different path and reloading the page with that style on.

Any ideas on how I could achieve this? Bare in mind I want to keep the script very simple.

Thanks for any help in advance,

Andy C
Telius
Forum Newbie
Posts: 3
Joined: Wed Aug 02, 2006 9:27 am

Post by Telius »

Your best bet would be to make that a form and submit the required information (say, second.css) through a $_POST variable. When loading the page, you simply set "$style1=$_POST['style']" or whatever you would've named the input field. You should probably still make security checks, like stripping any html tags in case they'd try to put malicious javascript, but meh.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

whitelist the available stylesheets in an array, then a simple in_array() check..

Code: Select all

<?php

$sheets = array('style1', 'style2', 'etc');

if (in_array($_POST['sheet'], $sheets)) {
    $stylesheet = $_POST['sheet'] . 'css';
} else {
    $stylesheet = 'default.css';
}

?>
andycole
Forum Newbie
Posts: 3
Joined: Wed Aug 02, 2006 11:12 am

Thanks

Post by andycole »

superb, much appreciated guys you've really helped me.

Andy
Post Reply