Page task_form.html
<html> <head> <meta charset='utf-8'> <title>Интерфейс формы</title> </head> <body> <form name="formfortask" action="ask_names.php" method="post"> <h3>Создать элемент "строка ввода текста":</h3> <input type="checkbox" name="types[]" value="string"><br> <h3>Количество элементов:</h3> <input type="text" name="numbers[string]" size="3"><br><br> <h3>Создать элемент "текстовая область":</h3> <input type="checkbox" name="types[]" value="text"><br> <h3>Количество элементов:</h3> <input type="text" name="numbers[text]" size="3"><br><br> <input type="submit" value="Создать"> </form> </body> </html>
Page ask_names.php. Requesting form element names:
<?php echo "<meta charset='utf-8'>"; // Этот скрипт запрашивает названия элементов формы, которая здесь - task_form.html $file = "task.php"; // файл, который будет обрабатывать сгенерированную этим скриптом форму function Ask_names() { global $file; if (isset($_POST["types"])) { $st = '<form action="'.$file.'" method="post">'; foreach ($_POST["types"] as $k => $type) { $num = $_POST["numbers"][$type]; for ($i=1; $i<=$num; $i++) { $st .= "Введите имя $i-го элемента типа $type: "; $st .= "<input type='text' name=names[$type][]><br>"; } $st.= "<input type='hidden' name='types[]' value='$type'>"; $st.= "<input type='hidden' name='numbers[]' value='$num'><br>"; } $st .= "<input type='submit' name='send' value='Send'></form>"; return $st; } else { echo "Select type"; } } echo Ask_names(); ?>
Page task.php. Creating form elements:
<?php echo "<meta charset='utf-8'>"; $show_file = "task_show1.php"; function Create_element($type, $name) { $str = ""; switch ($type) { case "string": $str .= "$name: <input type='text' name='string[]'><br>"; break; case "text": $str .= "$name: <textarea name='text[]'></textarea><br>"; break; } return $str; } function Create_form() { global $show_file; $str = '<form action="'.$show_file.'" method="post">'; foreach ($_POST["types"] as $k => $type) { $num = $_POST["numbers"][$k]; for ($i=1; $i<=$num; $i++) { $arr = $_POST["names"][$type][$i-1]; $str .= Create_element($type, $arr); } } $str .= "<input type='submit' value='Send'></form>"; echo $str; } $crt = "Create_form"; $crt(); echo "<hr>"; ?>
Page task_show1.php. Processing the form that is generated here - task.php:
<?php echo "<meta charset='utf-8'>"; function Create_output() { if (isset($_POST["types"])) { $st = $_POST["types"]; foreach ($st as $k => $type) { $num = $_POST["numbers"][$k]; for ($i=1; $i<=$num; $i++) { $arr = $_POST["names"][$type][$i-1]; $arr .= "<br>"; if ($type == "string") { $num1 = $_POST["numbers"][string]; for ($ii=1; $ii<=$num1; $ii++) { $str = $_POST["string"][$ii-1]; } } if ($type == "text") { $num1 = $_POST["numbers"][text]; for ($ii=1; $ii<=$num1; $ii++) { $str = $_POST["text"][$ii-1]; } } } echo "$arr<br>"; echo "$str"; } } } $cro = "Create_output"; $cro(); ?>
The problem is that the form is not processed, because the Error Warning: Invalid argument supplied for foreach()
appears Warning: Invalid argument supplied for foreach()
in the 4th row of the page task_show1.php. Even with the help of isset
, processing does not work, for some reason the array becomes empty. What could be the problem? How to fix it?