用户填写表单的文本文件,然后单击“提交”。然后将显示已输入的数据。用户单击“返回”返回到表单...
任务:
如何在数据无法保存或遇到错误时保留用户之前输入的文本字段中的值,以便用户不必再次填写所有文本字段?
感谢。
我有这段代码来保留这些值,但我不知道它是否正确或者放在哪里。
<form action="SSPage.php" method="post">
<input type="hidden" value="<?=$_POST['fname'];?>" />
<input type="hidden" value="<?=$_POST['lname'];?>" />
<input type="hidden" value="<?=$_POST['mname'];?>" />
<input type="submit" value="Back" />
这是我的表单代码:
<body>
<form name="form1" method="post" action="">
<table border="0" align="center" class="addPrincipalTable">
<tr>
<td width = "200" > <label>First Name </label>
<label for="textfield"></label></td>
<td width = "180" align="right"><input type="text" name="fname" id="textfield" ></td>
</tr>
<br />
<tr>
<td width = "200" > <label>Middle Name </label>
<label for="textfield"></label></td>
<td width = "180" align="right"><input type="text" name="mname" id="textfield" ></td>
</tr>
<br />
<tr>
<td width = "200" > <label>Last Name </label>
<label for="textfield"></label></td>
<td width = "180" align="right"><input type="text" name="lname" id="textfield" ></td>
</tr>
<br />
<?php
require_once ('../include/config.php');
require_once ('../include/opendb.php');
echo "<tr>";
echo "<td width = 200>"."Group Name"."</td>";
$result = mysql_query("SELECT group_name FROM ss_group") or die(mysql_error());
$dropdown = "<select name=\"group\">\n";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['group_name']}'>{$row['group_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo "<td align='right'>".$dropdown."</td>";
echo "</tr>";
echo "</table>";
?>
<table align="center" class="addPrincipalTable" border="0">
<tr class="options_button" align="center">
<div class = "cancel">
<td align="center" width="384"> <input type="submit" name="insert" value="Insert" width="126" height="18" />
<input type="submit" name="clear" value="Clear" onlick="document.form1.clear.value = '';"/>
<a href = "../SS/SSPage.php"><input type="submit" name="cancel" value="Cancel" /></a> </td>
</tr>
</div>
</table>
</form>
</body>
</html>
<form action="SSPage.php" method="post">
<input type="hidden" value="<?=$_POST['fname'];?>" />
<input type="hidden" value="<?=$_POST['lname'];?>" />
<input type="hidden" value="<?=$_POST['mname'];?>" />
<input type="submit" value="Back" />
<?php
require_once ('../include/config.php');
require_once ('../include/opendb.php');
if (isset($_POST['insert']))
{
$first = $_POST['fname'];
$middle = $_POST['mname'];
$last = $_POST['lname'];
$group= $_POST['group'];
$result = mysql_query("SELECT * from staffing_specialist WHERE first_name LIKE '%$first%' AND middle_name LIKE '%$middle%'
AND last_name LIKE '%$last%'");
$row = mysql_fetch_array($result);
$numOfEntries = mysql_num_rows( $result);
if($numOfEntries >= 1){
header ("location: include/insert_errorDuplicates.php");
exit();
}
if(($first == NULL) || ($middle == NULL) || ($last==NULL)){
header ("location:../include/insert_errorSS.php");
exit();
}
$myquery = mysql_query("SELECT SS_group_ID FROM ss_group WHERE group_name LIKE '%$group%'");//get the group ID through the item from combo box
$myresult = mysql_fetch_array($myquery);
$SS_group_ID = $myresult['SS_group_ID'];//the principal Id
$query = "INSERT INTO staffing_specialist(SS_group_ID,first_name,middle_name,last_name)
VALUES('$SS_group_ID','$first','$middle','$last')";
$result = mysql_query($query);
if($result){
header ("location:../include/insert_successSS.php");
}
else
header ("location../include/insert_errorSS.php");
}
else if (isset($_POST['clear'])){
}
?>
答案 0 :(得分:0)
如果输入的值属性不为空,则将其设置为$_POST['...']
值。
答案 1 :(得分:0)
<input type="text" name="fname" id="textfield"
value="<?= array_key_exists('fname', $_REQUEST) ? $_REQUEST['fname'] : '' ?>">
答案 2 :(得分:0)
我更喜欢使用jQuery / JavaScript进行表单提交,这样您就不需要离开页面了。
jQuery中的代码
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){
$('form#submit').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});