Page 1 of 1

Button to change variable string value

Posted: Wed Aug 02, 2006 11:21 am
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

Posted: Wed Aug 02, 2006 11:26 am
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.

Posted: Wed Aug 02, 2006 12:06 pm
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';
}

?>

Thanks

Posted: Wed Aug 02, 2006 4:48 pm
by andycole
superb, much appreciated guys you've really helped me.

Andy