表单在PHP中提交时消失
我的代码中有以下内容,我无法完成代码的后期工作。为了更准确地回答我的问题,在我单击表单在PHP中提交时消失,php,Php,我的代码中有以下内容,我无法完成代码的后期工作。为了更准确地回答我的问题,在我单击OneClick之前,这是我想要的方式。完成后,我会得到另一个表单(form2),其中动态填充了一个TwoClicksubmit按钮。问题就在这里。当我点击按钮twock时,整个“form2”消失了。有人能告诉我哪里出了问题吗 谢谢 <?php session_start(); ?> <DOCTYPE html> <html> <head><title&
OneClick
之前,这是我想要的方式。完成后,我会得到另一个表单(form2),其中动态填充了一个TwoClick
submit按钮。问题就在这里。当我点击按钮twock
时,整个“form2”消失了。有人能告诉我哪里出了问题吗
谢谢
<?php session_start(); ?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
?>
</div>
</body>
</html>
在输出任何输出(在脚本中)之前,必须调用会话启动()
$\u POST['sendone']
未设置,因此不会被回显。要解决此问题,您可以将第一个条件更改为:if(isset($_POST['sendone'])| | isset($_POST['sendtower']))
尝试放置
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
最后。我的意思是,在上一次
}
之后,尝试这个示例并阅读代码注释。虽然我真的不清楚你要做什么,所以我只是把代码移植到我认为你要做的事情上,尽管有更好的方法处理表单。此外,您还应该考虑表单值键,txt1和txt123没有帮助,并且不要解释您希望从表单中获取的变量类型。也许这很有趣
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
$form = null;
//Handle form 1
if(isset($_POST['sendone'])){
//reset session vars if new request
unset($_SESSION['txt1']);
unset($_SESSION['txt123']);
//validate txt1
if(!empty($_POST['txt1'])){
//set into session
$_SESSION['txt1'] = $_POST['txt1'];
//or you could put the value in a <input type="hidden" name="txt1" value="'.htmlspecialchars($_POST['txt1']).'"/>
$form = "
<form name='form2' method='post' action=''>
<table border='1'>
<tr>
<td>form 2 is here!<br></td>
</tr>
<tr>
<td><input type='text' name='txt123' id='txt123'></td>
</tr>
<tr>
<td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td>
</tr>
</table>
</form>";
} else {
$error['txt1'] = 'txt1 is empty!';
}
}
//do second form
if(isset($_POST['sendtwo'])){
//validate
if(!empty($_POST['txt123'])){
//set session
$_SESSION['txt123'] = $_POST['txt123'];
}else{
$error['txt123'] = 'txt2 is empty!';
}
}
//check then do something with both form values
if(empty($error) && isset($_SESSION['txt1']) && isset($_SESSION['txt123'])){
$form = "Both txt1=".htmlspecialchars($_SESSION['txt1'])." and txt123=".htmlspecialchars($_SESSION['txt123'])." was set into session";
}
}
?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="">
<input type='text' name='txt1' id='txt1'>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?php echo isset($error['txt1'])?$error['txt1']:null;?>
</form>
</div>
<div id="two">
<?php echo isset($form)?$form:null;?>
</div>
</body>
</html>
试试这个
<?php
session_start();
function putForm2(){
$myForm = "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
return $myForm;
}
?><!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo putForm2();
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{
echo putForm2();
echo "Text box is empty..."; return;
}
}
?>
</div>
</body>
</html>
为什么要关闭
这里
您应该放置会话\u start()代码>在任何output@YogeshSuthar谢谢Yogesh,我刚刚编辑了它…打字错误@l̕aͩEͩEͩEͩEͩCͭEͩEͩEͩEͩEͯEͩEͩEͯEͯEͩEͩEͩEͩEͩEͩE?这能解决我的问题吗?感谢您在页面顶部启动会话,从而避免会话错误。。它不是强制性的写在顶部的所有php。。。你也可以在任何地方写作。。因为在很多情况下,我们需要在中间编写php,甚至在页面的末尾,我按照上面的Ziarno编辑代码。但问题是,当验证文本显示为“文本框为空…”时,表单被隐藏。如何在屏幕上保留消息和表单2的元素?设置$\u SESSION['txt1']=“txt1为空!”
如果您总是希望form2在提交Form1后出现,而不是回显它,或者只需删除此条件if(isset($\u SESSION['txt1'))
删除返回语句tooThis works Hezbullah Shah,但问题是当消息显示时,txt123会消失。提交sendtwo时,是否有保留消息和txt123的提示?谢谢。如果您在第二个表单中没有输入任何内容,您将得到“文本框为空…”您的代码逻辑是这样的。您已经给出了条件if($\u POST['txt123']='')
@hezbullahsah为什么还要重复2if(isset($\u POST['sendtower'))
?我永远也不会知道。那是打字错误。不用了,谢谢你花了这么多时间。但我只需要在sreen上显示“文本框为空…”消息时,form2元素(在本例中为“form2在此”)文本、文本框“text123”和“sendtwo”提交按钮才可见。在此代码中,当“文本框为空…”显示form2元素时,会发生什么情况?用户必须点击浏览器上的“后退”按钮才能返回。请告诉我这是否解决了您的问题。这可能是OP需要的,但是您需要将
放在最上面。否则,它将返回一条错误消息,我刚刚对它进行了测试。另外,别忘了用感叹号将
更改为
,它丢失了。@Hezbullah Shah宾果!这就是我想要的。谢谢你,伙计。谢谢@Fred的评论。