Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单击“提交”时以表单形式保存数据_Php_Form Submit - Fatal编程技术网

Php 单击“提交”时以表单形式保存数据

Php 单击“提交”时以表单形式保存数据,php,form-submit,Php,Form Submit,编辑:道歉-所有代码现在粘贴在下面 对于第一次提出的新手问题表示歉意-我一直在这里和谷歌上寻找答案-我感觉这是一些简单的编码,我搞砸了。 我创建了一个表单,在验证时,检查同一页面上的所有数据,原始数据保留在原始表单中,以便您可以根据需要进行更改。 我在表格中有一个部分,但这不起作用-你可以问多个问题,每个问题最多可以有4个答案 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht

编辑:道歉-所有代码现在粘贴在下面

对于第一次提出的新手问题表示歉意-我一直在这里和谷歌上寻找答案-我感觉这是一些简单的编码,我搞砸了。 我创建了一个表单,在验证时,检查同一页面上的所有数据,原始数据保留在原始表单中,以便您可以根据需要进行更改。 我在表格中有一个部分,但这不起作用-你可以问多个问题,每个问题最多可以有4个答案

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>Test page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="stylenewtest.css" type="text/css" />
<script language="javascript" src="tableadd.js"></script>
</head>
<body>
<?php 
$string = isset($_POST['quiz_format']) ? $_POST['quiz_format'] : false;
$quiz_format = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $string);
$stringquiz_100 = isset($_POST['quiz_100']) ? $_POST['quiz_100'] : false;
$quiz_100 = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_100);
$stringquiz_fback = isset($_POST['quiz_fback']) ? $_POST['quiz_fback'] : false;
$quiz_fback = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_fback);
$stringquiz_title = isset($_POST['quiz_title']) ? $_POST['quiz_title'] : false;
$quiz_title = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_title);
$stringquiz_instruct = isset($_POST['quiz_instruct']) ? $_POST['quiz_instruct'] : false;
$quiz_instruct = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\?,\"\'\._@\\/ !&()=\+;:\t\r\n-]", "", $stringquiz_instruct);
$stringquiz_time = isset($_POST['quiz_time']) ? $_POST['quiz_time'] : false;
$quiz_time = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_time);
?>
<div id="content">
<p><span class="error">* required field.</span></p>
<form method="post" action=""> 
<div class="datagrid">
<table>
<tr><th colspan="2">Please choose the format of the quiz<span class="error">*</span></th></tr>
<tr><td>Hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Hangman") echo "checked";?> value="Hangman"></td></tr>
<tr><td>Gap fill</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Gap fill") echo "checked";?> value="Gap fill"></td></tr>
<tr><td>Multiple choice</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Multiple Choice") echo "checked";?> value="Multiple choice"></td></tr>
<tr><td>Simple Q & A</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Simple Q & A") echo "checked";?> value="Simple Q & A"></td></tr>
<tr><td>Word formation hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Word formation hangman") echo "checked";?> value="Word formation hangman"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Continue repeating the exercise until 100% achieved?<span class="error">*</span></th></tr>
<tr><td>Yes</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="Yes") echo "checked";?> value="Yes"></td></tr>
<tr><td>No</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="No") echo "checked";?> value="No"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Show the answer as correct/incorrect after every question or just at the end?<span class="error">*</span></th></tr>
<tr><td>After every question</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="After every question") echo "checked";?> value="After every question"></td></tr>
<tr><td>Just at the end</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="Just at the end") echo "checked";?> value="Just at the end"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Please enter in the title of the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz title:</td><td><input type="text" name="quiz_title" value="<?php echo $_POST[quiz_title];?>"></td></tr>
</table>
<br />
<table>
<tr><th colspan="2">Please enter in the instructions for the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz instructions:</td><td><textarea name="quiz_instruct" rows="10" cols="40"><?php echo $_POST[quiz_instruct];?></textarea></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<script language="javascript">
window.onload = moreFields;
window.onload = init;
</script>
<table>
<tr><th colspan="5">Please enter in the quiz's questions and answers:<span class="error">*</span></th></tr>
</table>
<div id="readroot" style="display: none">
<input type="button" value="Remove a question field"
    onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
<table>
<tr>
    <td>Question:</td>
    <td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
    <td>Answer 1:</td>
    <td>Answer 2:</td>
    <td>Answer 3:</td>
    <td>Answer 4:</td>
</tr>
<tr>
    <td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
    <td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
    <td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
    <td><input type="text" name="a4[]" value="<?php echo $_POST['a4'][$i] ?>"></td>
</tr>
</table>
</div>

<span id="writeroot"></span><input type="button" id="moreFields" value="Click here to add further question fields" /><br />
</div>
<br />
<div class="datagrid">
<table>
<tr><td><input type="submit" name="Submit1" value="Submit"/></td></tr>
</table> 
</div>
</form>

<?php
if ( isset( $_POST['Submit1'] ) ) { 
echo "<form method=post action=insert.php>";
echo "<h1>Please confirm the quiz details are correct</h1>";
echo "<div class=datagrid>";
echo "<table>";
if(empty($string)){
echo("<tr><td>Quiz format:</td><td><h3>Please go back and choose a quiz format<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz format:</td><td>" . $quiz_format . "</td></tr>");
echo("<input type=hidden name=quiz_format value='" . $quiz_format . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td>" . $quiz_100 . "</td></tr>");
echo("<input type=hidden name=quiz_100 value='" . $quiz_100 . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td>" . $quiz_fback . "</td></tr>");
echo("<input type=hidden name=quiz_fback value='" . $quiz_fback . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_title)){
echo("<tr><td>Quiz title:</td><td><h3>Please go back and enter in a title for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz title:</td><td>" . $quiz_title . "</td></tr>");
echo("<input type=hidden name=quiz_title value='" . $quiz_title . "' />");
}
if(empty($stringquiz_instruct)){
echo("<tr><td>Quiz instructions:</td><td><h3>Please go back and enter in instructions for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz instructions:</td><td>" . $quiz_instruct . "</td></tr>");
echo("<input type=hidden name=quiz_instruct value='" . $quiz_instruct . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_time)){
echo("<tr><td>Time limit:</td><td>not specified</td></tr>");
}
else
{
echo("<tr><td>Time limit:</td><td>" . $quiz_time . "</td></tr>");
echo("<input type=hidden name=quiz_time value='" . $quiz_time . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td colspan=5>The questions and answers to your quiz:</td></tr>";
$aq = $_POST['q'];
$aa1 = $_POST['a1'];
$aa2 = $_POST['a2'];
$aa3 = $_POST['a3'];
$aa4 = $_POST['a4'];
$N = count($aq);
for($i=1; $i < $N; $i++){
if($aq[$i]==""){
echo("<tr><td>Question:</td><td><h3>Please go back and enter a question</h3></td></tr>");
$showbutton=1;
}
elseif($aa1[$i]==""){
echo("<tr><td>Answer:</td><td><h3>Please go back and enter at least one answer</h3></td></tr>");
$showbutton=1;
}
else{
 echo ("<table><tr><td>Question:</td><td colspan=3>" . $aq[$i]. "<input type=hidden name=q[] value='" . $aq[$i] . "'></td></tr>
<tr><td>Answer 1:</td><td>Answer 2:</td><td>Answer 3:</td><td>Answer 4:</td></tr>
<tr><td>" .$aa1[$i] . " <input type=hidden name=a1[] value='" .$aa1[$i] . "'></td>
<td>" .$aa2[$i] . " <input type=hidden name=a2[] value='" .$aa2[$i] . "'></td>
<td>" .$aa3[$i] . " <input type=hidden name=a3[] value='" .$aa3[$i] . "'></td>
<td>" .$aa4[$i] . " <input type=hidden name=a4[] value='" .$aa4[$i] . "'></td> </tr></table>");
  }
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td><input type=button value='<< Go Back' onclick='goBack()' /></td></tr>";
if ($showbutton =="1"){
}
else{
echo "<tr><td><input type=submit value=Submit></td></tr>";
echo "</table>";
echo "</form>";
}
}
?>
</div>
</body>
</html>
但每次我提交时,问题和答案都会进行,但表单字段会丢失所有数据。如何将数据保存在字段中?
多谢提前

您只需将输入值设置为$\u POST['old\u value\u name]或$\u GET['old\u value\u name],具体取决于您使用的表单方法。因此,假设您正在从您的问题中查找密钥$i,那么您的新代码将如下所示:

<table>
    <tr>
        <td>Question:</td>
        <td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
    </tr>
    <tr>
        <td>Answer 1:</td>
        <td>Answer 2:</td>
        <td>Answer 3:</td>
        <td>Answer 4:</td>
    </tr>
    <tr>
        <td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
        <td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
        <td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
        <td><input type="text" name="a4[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
    </tr>
</table>

据我所知,您的问题是希望在将值发送到服务器后将其保存在表单中。我已经创建了一个表单,当您进行验证时,您可以检查同一页面上的所有数据,原始数据保留在原始表单中,以便您可以根据需要进行更改

没关系。我记得几个月前我也想要一样的。这就是我为什么要帮忙的原因

只有一个问题是,按下提交按钮后,表单被发送到服务器,页面被重新加载。 作为回报,您将从webserver获得一个新页面,您在表单字段操作中指出了该页面

我会这样做:

    Target form: <br/>
    <form id="target" action="index.php" method='post'>
        <input  name="first_field" type="text" value="<?php  if ( isset($_POST['first_field']) ) 
            echo  $_POST['first_field'];     ?>"  /> <br/>
        <input name="second_field" type="text" value="<?php if (  isset($_POST['second_field'])) 
       echo $_POST['second_field'];?>" /> <br/>            

         <p><b>What kind of girls do you like?:</b><Br>
            <input type="radio" name="browser" value="brunette" <?php if( $_POST['browser']==="brunette" ) echo "checked";  ?>  >  brunette<Br>
            <input type="radio" name="browser" value="blonde" <?php if( $_POST['browser']==="blonde" ) echo "checked";  ?>  > blonde<Br>
            <input type="radio" name="browser" value="red" <?php if( $_POST['browser']==="red" ) echo "checked";  ?> > red<Br>
         </p>


        <input type="submit" />
    </form>

我们鼓励您提出如何更改/改进的想法。

a1、a2等如何获得价值?向我们展示代码。我对你所说的有问题:你可以问多个问题,每个问题最多可以有4个答案。。我有可以发布的工作代码,但关于多重问题的那部分是我困惑的地方。您只有1个问题和多个答案作为代码发布。我需要知道多个问题是如何起作用的$aa1=$_POST['a1']$aa2=$_POST['a2']$aa3=$_POST['a3']$aa4=$_POST['a4']$N=计数$aq;对于$i=1$i<$N$i++{echo Question:.$aq[$i]。回答1:回答2:回答3:回答4:.$aa1[$i]。;}}}对代码表示歉意-希望粘贴后面的内容。多个问题涉及控制表-window.onload=moreFields的js脚本;window.onload=init;onclick=this.parentNode.parentNode.removeChildthis.parentNode;好吧,我不知道JS牵涉其中,老实说,JS不是我的强项。您需要将所有代码放在您的问题中,而不是放在注释中,以便其他人能够访问您的问题,知道他们必须准确地处理什么。还要添加javascript标记@user3162674这个答案没有任何意义。名称为a1[],它将在$\u POST中创建一个a1数组。因此,如果您需要参考您自己做的**$\u POST['a1'][key]**大家好,谢谢您的帮助-尽管数据仍然没有保存在字段中。我想这可能是我没有提供足够信息的错。此表使用js脚本运行-不确定这是否与此有关。如果使用类似于:Search:function submitform{document.myform.submit;}的javascript方法,则使用$\u GET似乎可以很好地工作。window.onload=moreFields;window.onload=init;只是试了一下,恐怕没有乐趣