我尝试将我的表单链接到php文件,但是出现错误并且未提交数据。我不明白如何解决它。帮我解决这个问题
表格代码:
<form target="_blank" action="details.php" method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstName" maxlength="25"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastName" maxlength="25"></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="Gender" value="male">Male
<input type="radio" name="Gender" value="female"> Female
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="Email" maxlength="35"></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phoneNumber" maxlength="20"></td>
</tr>
<tr>
<td>Ask question and provide background information</td>
<td><textarea name="information" rows="5" cols="20">put your question and background information here</textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitForm" value="submit"><input type="submit" name="submitForm" value="reset"></td>
</tr>
</table>
</form>
php文件代码:
<?php if($_POST['submitForm'] == 'reset' ){
$_POST['firstName'] = "";
$_POST['lastName'] = "";
$_POST['Email'] = "";
$_POST['phoneNumber'] = "";
$_POST['Gender'] = "Male";
}
?>
其余代码与表单相同,但不是文本框和广播,而是PHP编码
答案 0 :(得分:2)
将value="submit"
更改为value="reset"
<input type="submit" name="submitForm" value="submit">
如果下面的代码不在details.php
<?php
if($_POST['submitForm'] == 'reset' ){
$_POST['firstName'] = "";
$_POST['lastName'] = "";
$_POST['Email'] = "";
$_POST['phoneNumber'] = "";
$_POST['Gender'] = "Male";
}
?>
然后还考虑从target="_blank"
代码
form
修改强>
根据您更新的问题,您在HTML中有两个提交按钮,将一个提交更改为type="reset"
,并将isset
功能添加到if
条件
if(isset($_POST['submitForm']) && $_POST['submitForm'] == 'reset' )
<tr>
<td colspan="2" align="center"><input type="submit" name="submitForm"
value="submit"><input type="submit" name="submitForm" value="reset">
</td>
</tr>
删除这一个<input type="submit" name="submitForm" value="submit">
,它将起作用
查看实时Demo
答案 1 :(得分:0)
如果您尝试重置表单,可以使用按钮类型重置而不是提交。
<button type="reset" value="Reset">Reset</button>
答案 2 :(得分:0)
请从表单标记中删除 target =“_ blank”。
如果您想将表单数据发布到其他页面,请设置表单操作标记
<form action="page url" action="details.php" method="post">
<input type="submit" name="submitForm" value="submit">
<?php if($_POST['submitForm'] == 'submit' ){
$_POST['firstName'] = "";
$_POST['lastName'] = "";
$_POST['Email'] = "";
$_POST['phoneNumber'] = "";
$_POST['Gender'] = "Male";
}
?>
答案 3 :(得分:-1)
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstName" maxlength="25"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastName" maxlength="25"></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="Gender" value="male">Male
<input type="radio" name="Gender" value="female"> Female
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="Email" maxlength="35"></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phoneNumber" maxlength="20"></td>
</tr>
<tr>
<td>Ask question and provide background information</td>
<td><textarea name="information" rows="5" cols="20">put your question and background information here</textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitForm" value="submit"><input type="submit" name="submitForm" value="reset"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submitForm']))
{
$_POST['firstName'] = $fname;
$_POST['lastName'] = $lname;
$_POST['Email'] = $email;
$_POST['phoneNumber'] = $phonenumber;
$_POST['Gender'] = $gender;
// your query goes here......
}
?>