AJAX & Prototype Page 4 - Completing a User-Defined CSS Website with PHP |
Now that we have prepared all the validation statements, if the input passes the validation, it will output the requested CSS/Style revision. If not, it will end the script execution. The PHP script will be: if (($backgroundcolorcount=7) && ($fontcolorcount=7) && ($firstbackgroundcolor='#') && ($firstfontcolor='#') && ctype_alnum($alnumbackgroundcolor) && ctype_alnum($alnumfontcolor)) { //valid inputs, process data echo $first; echo '<style type="text/css">'. "n"; echo 'body{ color:'.$fontcolor.';background:'.$backgroundcolor.';}'. "n"; echo '</style>'. "n"; } else { //Invalid inputs, end script die(); } Note that the alphanumeric validation above is carried out by the ctype_alnum function. Combining all scripts, the completed script for css.php will be: <?php if ((!(empty($_POST["backgroundcolor"]))) && (!(empty($_POST["fontcolor"])))) { //POST field not empty, process data $backgroundcolor = trim($_POST["backgroundcolor"]); $fontcolor=trim($_POST["fontcolor"]); //start of validation //count characters $backgroundcolorcount= strlen($backgroundcolor); $fontcolorcount= strlen($fontcolor); //grab the first character of the input $str1 = $backgroundcolor; $str2 = $fontcolor; $firstbackgroundcolor = $str1[0]; $firstfontcolor = $str2[0]; //remove # for alphanumeric validation $alnumbackgroundcolor = substr($backgroundcolor, -6); $alnumfontcolor = substr($fontcolor, -6); //validation condition if (($backgroundcolorcount=7) && ($fontcolorcount=7) && ($firstbackgroundcolor='#') && ($firstfontcolor='#') && ctype_alnum($alnumbackgroundcolor) && ctype_alnum($alnumfontcolor)) { //valid inputs, process data echo $first; echo '<style type="text/css">'. "n"; echo 'body{ color:'.$fontcolor.';background:'.$backgroundcolor.';}'. "n"; echo '</style>'. "n"; } else { //Invalid inputs, end script die(); } } else { //POST field empty, end script die(); } ?> Other application notes You can download and see the entire application. Note that this application only extends to customization of vital styling elements (font color and the background color). However, by updating and improving the script you can extend its capabilities not only to customization of font and background colors, but other CSS elements as well, such as font size, font type, etc.
blog comments powered by Disqus |
|
|
|
|
|
|
|