PHP 提交表单没有 return 任何值
PHP isset for a submit form didn't return any value
通过遵循互联网上的一些示例,我设法制作了一个表单来插入用户。
我将 isset 用于提交功能。当表单完全填写并提交时,结果将回显以显示状态消息。不幸的是,表单根本不起作用,当点击提交按钮时,表单被重置。
由于我使用存储过程,ReturnStatus
和ReturnMessage
都会根据表单中的输入值自动调用。
这是代码。
<?php
include "config.php";
if(isset($_POST['sub']))
{
ini_set('error_reporting', E_ALL);
error_reporting(-1);
$RegType="D";
$UserId=$_POST["UserId"];
$UserPwd=$_POST["UserPwd"];
$UserNm=$_POST["UserNm"];
$stmt=odbc_exec($conn,"CALL UserInsert ('".$RegType."','UserId','UserPwd','UserNm')");
if (!$stmt) {
"Error : " . odbc_errormsg();
}
if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
if($ReturnStatus==1) {
echo $ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
}
?>
<table>
<form class="form" method="post">
<tr>
<td class = "nama">Nama Pengguna<span class="required"> * </span></td>
<td><input type="text" name="UserNm" value=""></td>
</tr>
<tr>
<td class = "userid">Id Pengguna<span class="required"> * </span></td>
<td><input type="text" name="UserId" value=""></td>
</tr>
<tr>
<td class = "password">Kata Laluan<span class="required"> * </span></td>
<td><input type="password" name="UserPwd" value=""></td>
</tr>
<tr><td>
<input type="submit" value="Save">
</td></tr>
</table>
我确定我错过了什么。请指导我。谢谢。
已更新
修复了以下内容:
- Fix the name of the button
- PHP error reporting
- Isset
请更改
<input type="submit" value="Simpan"> // you are not added name attribute here
到
<input type="submit" value="Simpan" name="submit">
检查以下行:
<input type="submit" value="Simpan">
并且您正在尝试通过以下方式访问它:
if(!isset($_POST['submit']))
这里你忘记在输入中添加 name="submit"
所以让它像:
<input type="submit" value="Simpan" name="submit">
说明 $_POST['index']
这里index
是tag的名字
如果您在 input
中添加 name
属性,请键入 submit
那么我希望您的代码能够正常工作。
所以在 html 编辑:
<input type="submit" value="Simpan">
作为
<input type="submit" name='submit' value="Simpan">
我修复了一些错误并且它工作正常
<table>
<form class="form" method="post">
<tr><td class = "nama">Name : <span class="required"> * </span></td>
<td><input type="text" name="UserNm" value=""></td></tr>
<tr><td class = "userid">Id : <span class="required"> * </span></td>
<td><input type="text" name="UserID" value=""></td></tr>
<tr><td>
<input type="submit" value="Simpan" name="submit">
</td></tr>
</form>
</table>
<?php
if(isset($_POST['submit'])){
$RegType="D";
$UserId=$_POST["UserID"];
$UserNm=$_POST["UserNm"];
echo $UserId;
echo $UserNm;
$stmt=odbc_exec($conn,"CALL UserInsert ('".$RegType."','UserId','UserNm')");
if (!$stmt) {
"Error : " . odbc_errormsg();
}
if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
if($ReturnStatus==1) {
echo $ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
}
?>
在您的提交按钮上使用 name="submit"
那么post车就可以进出
几个重要建议:
首先在你的代码中启用PHP error reporting
,这将帮助你找到真正的错误:
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// Report all PHP errors
error_reporting(-1);
第二个,不知道你为什么在isset()
检查里面用<form></form>
,这样会return 页面刷新什么都没有。
第三,很重要,大家在回答中提到,你在按钮输入中缺少name属性。
<input type="submit" value="Simpan" name="submit">
Fourth,假设,如果你修复了这个错误,你将在 $ReturnStatus
的失败案例中得到未定义的变量。您必须需要在顶级声明中定义为 0,因为如果 odbc_fetch_row($stmt)
失败,这将 return Undefined Variable Warnings
通过遵循互联网上的一些示例,我设法制作了一个表单来插入用户。
我将 isset 用于提交功能。当表单完全填写并提交时,结果将回显以显示状态消息。不幸的是,表单根本不起作用,当点击提交按钮时,表单被重置。
由于我使用存储过程,ReturnStatus
和ReturnMessage
都会根据表单中的输入值自动调用。
这是代码。
<?php
include "config.php";
if(isset($_POST['sub']))
{
ini_set('error_reporting', E_ALL);
error_reporting(-1);
$RegType="D";
$UserId=$_POST["UserId"];
$UserPwd=$_POST["UserPwd"];
$UserNm=$_POST["UserNm"];
$stmt=odbc_exec($conn,"CALL UserInsert ('".$RegType."','UserId','UserPwd','UserNm')");
if (!$stmt) {
"Error : " . odbc_errormsg();
}
if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
if($ReturnStatus==1) {
echo $ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
}
?>
<table>
<form class="form" method="post">
<tr>
<td class = "nama">Nama Pengguna<span class="required"> * </span></td>
<td><input type="text" name="UserNm" value=""></td>
</tr>
<tr>
<td class = "userid">Id Pengguna<span class="required"> * </span></td>
<td><input type="text" name="UserId" value=""></td>
</tr>
<tr>
<td class = "password">Kata Laluan<span class="required"> * </span></td>
<td><input type="password" name="UserPwd" value=""></td>
</tr>
<tr><td>
<input type="submit" value="Save">
</td></tr>
</table>
我确定我错过了什么。请指导我。谢谢。
已更新
修复了以下内容:
- Fix the name of the button
- PHP error reporting
- Isset
请更改
<input type="submit" value="Simpan"> // you are not added name attribute here
到
<input type="submit" value="Simpan" name="submit">
检查以下行:
<input type="submit" value="Simpan">
并且您正在尝试通过以下方式访问它:
if(!isset($_POST['submit']))
这里你忘记在输入中添加 name="submit"
所以让它像:
<input type="submit" value="Simpan" name="submit">
说明 $_POST['index']
这里index
是tag的名字
如果您在 input
中添加 name
属性,请键入 submit
那么我希望您的代码能够正常工作。
所以在 html 编辑:
<input type="submit" value="Simpan">
作为
<input type="submit" name='submit' value="Simpan">
我修复了一些错误并且它工作正常
<table>
<form class="form" method="post">
<tr><td class = "nama">Name : <span class="required"> * </span></td>
<td><input type="text" name="UserNm" value=""></td></tr>
<tr><td class = "userid">Id : <span class="required"> * </span></td>
<td><input type="text" name="UserID" value=""></td></tr>
<tr><td>
<input type="submit" value="Simpan" name="submit">
</td></tr>
</form>
</table>
<?php
if(isset($_POST['submit'])){
$RegType="D";
$UserId=$_POST["UserID"];
$UserNm=$_POST["UserNm"];
echo $UserId;
echo $UserNm;
$stmt=odbc_exec($conn,"CALL UserInsert ('".$RegType."','UserId','UserNm')");
if (!$stmt) {
"Error : " . odbc_errormsg();
}
if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
if($ReturnStatus==1) {
echo $ReturnMessage=odbc_result($stmt,'ReturnMessage');
}
}
?>
在您的提交按钮上使用 name="submit"
那么post车就可以进出
几个重要建议:
首先在你的代码中启用PHP error reporting
,这将帮助你找到真正的错误:
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// Report all PHP errors
error_reporting(-1);
第二个,不知道你为什么在isset()
检查里面用<form></form>
,这样会return 页面刷新什么都没有。
第三,很重要,大家在回答中提到,你在按钮输入中缺少name属性。
<input type="submit" value="Simpan" name="submit">
Fourth,假设,如果你修复了这个错误,你将在 $ReturnStatus
的失败案例中得到未定义的变量。您必须需要在顶级声明中定义为 0,因为如果 odbc_fetch_row($stmt)
失败,这将 return Undefined Variable Warnings