我的代码中有以下内容,我无法完成代码工作的后半部分。为了更准确地说明我的问题,我按照我想要的方式工作,直到我点击OneClick
。完成后,我得到另一个表单(form2),其中TwoClick
提交按钮动态填充。这个问题来了。当我点击按钮TwoClick
时,整个'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>
答案 0 :(得分:0)
Session_start()
(在脚本的开头)。
由于您的逻辑,Sendtwo表单消失 - 当您提交Sendtwo时,$_POST['sendone']
未设置,因此不会被回显。要解决这个问题,您可以将第一个条件更改为:
if (isset($_POST['sendone']) || isset($_POST['sendtwo']))
答案 1 :(得分:0)
尝试放
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
在最后。我的意思是在最后}
答案 2 :(得分:0)
尝试此示例并阅读代码注释。虽然我真的不清楚你的尝试是什么,所以我只是把代码移植到我认为你尝试的todo,有更好的方法来处理表格。另外你应该考虑你的表单值键,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>
答案 3 :(得分:0)
试试这个
<?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>