What I am trying to do is create a CMS using PHP and AJAX. I'm trying to create a form that'll change css for the whole website.
Is it possible to store a php variable or will I need to use a database to achieve this?
PHP/CSS code
$bgcolour = $_POST['bgColour'];
?>
body {
background-color:<?php echo $bgcolour; ?>;
}
AJAX CODE
$(document).ready(function(){
$('#css').on('submit',function(e){
$.ajax({
url:'../css/style.php',
data:$(css).serialize(),
type:'POST',
success:function(data){
$('<style type="text/css"></style>')
html(css)
. appendTo("head");
console.log(data);
if(data != "Error") {
$("#data").html(data).show().fadeOut(9000);
}
else {
$("#data").html(data).show().fadeOut(9000);
}
},
error:function(data){
$("#error").show().fadeOut(5000);
}
});
e.preventDefault();
document.getElementById("css").reset();
$("#main").load(location.href + " #main");
});
});
HTML Code
<h2>Change Website Style</h2>
<form name="css" id="css" action="">
Background Colour: <input type="text" name="bgColour" value="" />
<input type="submit" name="css" /></form>
It depends really, I agree with @DanielM - you will need to store this information in a database if you want these settings to carry throughout the users' session.
Another option is to use the session super global $_SESSION
to store that information. However, once the user leaves your site - the site will revert back to the "default" background color.
If you are going to use some sort of authentication; meaning the user logs in, and then sets the background color. Database is the way to go. That way when the page loads, you can write a php script to get/set the color they entered.
If you just want an anonymous user to enter a color and don't care that it goes away when they leave, you might get away using $_SESSION
.
Hope this helps...