Есть задача Подготовьте форму, в которую бы вводился заголовок страницы,цвета фона,цвет шрифта и текст страницы. Создайте HTML-страницу с помощью этой формы. (Вопрос как можно сделать по другому или оптимизировать этот код) Вот исполнение : PHP: <?php if (empty($title)){ ?> HTML: <form> <h1><center>Ведите данные:</center></h1> <center><table border =1 > <tr> <th>Заголовок страницы:</th> <th> <input type = "text" name = "title"> </th> <tr> <th>цвет фона </th> <th> <input type ="text" name = "bordercolor"> </th> <tr> <th>цвет текста </th> <th> <input type ="text" name = "fontcolor"> </th> </table> <table> <tr> <th> <textarea name = "tekst" rows = 10 cols = 40> </textarea><br> <input type = "submit" value = "Ввести все данные"> </th> </center> </table> </form> PHP: <?php } else { print <<<HERE <html> <head> <title>$title</title> </head> <body bgcolor="$bordercolor"> </center> <font color="$fontcolor"> <h1><center>$tekst</h1></center> </font> </center> </body> </html> HERE; }// вывод страницы ?> Код:
проще некуда. можно только усложнить. брать значения переменных из $_GET, резать, тримить, проверять на правильность...
для начала это register_globals зло. GET тут тоже не к месту и атрибуты принято писать в строчку PHP: <?php if(isset($_POST['done'])){ print " <html> <head> <title>{$_POST['title']}</title> </head> <body bgcolor=\"{$_POST['bordercolor']}\"> </center> <font color=\"{$_POST['fontcolor']}\"> <h1><center>{$_POST['tekst']}</h1></center> </font> </center> </body> </html>"; die(); } ?> <form method='POST'> <h1><center>Ведите данные:</center></h1> <center><table border =1 > <tr> <th>Заголовок страницы:</th> <th> <input type = "text" name = "title"> </th> <tr> <th>цвет фона </th> <th> <input type ="text" name = "bordercolor"> </th> <tr> <th>цвет текста </th> <th> <input type ="text" name = "fontcolor"> </th> </table> <table> <tr> <th> <textarea name = "tekst" rows = 10 cols = 40> </textarea><br> <input type = "submit" name='done' value = "Ввести все данные"> </th> </center> </table> </form>
Дополню от себя чуток PHP: <?php if(isset($_POST['done'])){ //Обезопасимся, не случай, если кто-то решит вписать целую страницу в нашу страницу =) // Отрежем сначала лишние символы, после - уберем пробелы. if (isset($_POST['title'])) { $_POST['title'] = trim(substr($_POST['title'], 0, 255)); // обрезаем название страницы до 255 символов. - все равно такое длинное никто не увидит. } if (isset($_POST['bordercolor'])) { $_POST['bordercolor'] = trim(substr($_POST['bordercolor'], 0, 32)); // Хватит и 16, но пусть будет 32 } if (isset($_POST['fontcolor'])) { $_POST['fontcolor'] = trim(substr($_POST['fontcolor'], 0, 32)); // то же самое. } if (isset($_POST['text'])) { $_POST['text'] = trim(substr($_POST['text'], 0, 2048)); // Хватит и пары килобайт. } print " <html> <head> <title>{$_POST['title']}</title> </head> <body bgcolor=\"{$_POST['bordercolor']}\"> </center> <font color=\"{$_POST['fontcolor']}\"> <center>{$_POST['text']}</h1></center> </font> </center> </body> </html>"; die(); } ?> <form method='POST'> <h1><center>Ведите данные:</center></h1> <center><table border =1 > <tr> <th>Заголовок страницы:</th> <th> <input type = "text" name = "title"> </th> <tr> <th>цвет фона </th> <th> <input type ="text" name = "bordercolor"> </th> <tr> <th>цвет текста </th> <th> <input type ="text" name = "fontcolor"> </th> </table> <table> <tr> <th> <textarea name = "text" rows = 10 cols = 40> </textarea><br> <input type = "submit" name='done' value = "Ввести все данные"> </th> </center> </table> </form>
igordata зло советуешь PHP: <?php if(isset($_POST['done'])){ foreach(array("title"=>255,"bordercolor"=>7,"fontcolor"=>7,"tekst"=>2028) as $key=>$val){ if(!empty($_POST[$key])){ if(strlen($_POST[$key])>$val){ print "Ошибка, максимальная длина поля '{$key}' = '{$val}' символов"; exit; } } }