0

我的代码中有以下内容,我无法开始代码工作的后半部分。为了更准确地回答我的问题,在我单击之前,这将按照我想要的方式工作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> 
4

4 回答 4

0

试试这个例子并阅读代码注释。虽然我真的不清楚您尝试做什么,所以我只是将代码移植到我认为您尝试做的事情,但有更好的方法来处理处理表单。另外,您应该真正考虑一下您的表单值键,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>
于 2013-06-15T05:33:30.977 回答
0
  1. Session_start()必须在输出任何输出之前调用(在脚本的开头)。

  2. 由于您的逻辑,Sendtwo 表单消失了 - 当您提交 Sendtwo 时,$_POST['sendone']未设置,因此不会被回显。要解决这个问题,您可以例如将第一个条件更改为:

if (isset($_POST['sendone']) || isset($_POST['sendtwo']))

于 2013-06-15T05:08:11.513 回答
0

试着把

if(isset($_POST['sendtwo']))
         if(isset($_POST['sendtwo'])) 
         {
             if($_POST['txt123'] == '')
                      {echo "Text box is empty..."; return;}
         }

最后。我的意思是在最后一次之后}

于 2013-06-15T05:11:25.283 回答
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>
于 2013-06-15T06:05:15.723 回答