如何使用php保留两个按钮提交的数据?

如何使用php保留两个按钮提交的数据?,php,Php,如何在使用php的两个按钮提交的表格单元格中保留数据? 以下是输出: 这是我想要的结果(注意:下面的图像只是使用图像编辑器编辑的): 这是我使用的代码: <html> <head> <title>Sample </title> </head> <body> <table border = "1" align = "center" cellpadding = "10" > <form method =

如何在使用php的两个按钮提交的表格单元格中保留数据? 以下是输出:

这是我想要的结果(注意:下面的图像只是使用图像编辑器编辑的):

这是我使用的代码:

<html>
<head>
<title>Sample </title>
</head>
<body>

<table border = "1" align = "center" cellpadding = "10" >
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">

<tr>
<td>Output1 : 
  <?php 
     if (isset($_POST['run1'])) {
     echo "<b>" .$_POST['input1']. "</b>"; 
     }  
  ?> 
 /td>

<td>Output2 : 
<?php 
   if (isset($_POST['run2'])) {
     echo "<b>" .$_POST['input2']. "</b>"; 
   }
?>
</td>
</tr>

<tr>
<td><input type = "text" name = "input1" value = "Type here..."></td>
<td><input type = "text" name = "input2" value = "Type here..."></td>
</tr>

<tr>
<td align = "center"><input type = "submit" name = "run1" value = "Button 1"></td>
<td align = "center"><input type = "submit" name = "run2" value = "Button 2"> </td>
</tr>

</form>
</table>
</body>
</html>

样品

您只需检查其他密钥名称:

 <?php 
     if(!empty($_POST['input1'])) {
        echo "<b>" .$_POST['input1']. "</b>"; 
     }  
  ?> 

编辑:因为您确实想要持久化数据,所以需要一个会话:

<?php
# Start session (don't hide warnings)
session_start();
# Check if content being submitted
if(!empty($_POST['input1']))
    $_SESSION['input1'] = $_POST['input1'];
if(!empty($_POST['input2']))
    $_SESSION['input2'] = $_POST['input2'];
?>
<html>
<head>
<title>Sample </title>
</head>
<body>
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
    <table border = "1" align = "center" cellpadding = "10" >
        <tr>
            <td>Output1 : 
                <?php 
                if(!empty($_SESSION['input1']))
                    echo "<b>" .htmlspecialchars($_SESSION['input1']). "</b>";
                ?> 
            </td>
            <td>Output2 : 
                <?php 
                if(!empty($_SESSION['input2']))
                    echo "<b>" .htmlspecialchars($_SESSION['input2']). "</b>";
                ?> 
            </td>
        </tr>
        <tr>
            <td>
                <input type = "text" name = "input1" value = "Type here...">
            </td>
            <td>
                <input type = "text" name = "input2" value = "Type here...">
            </td>
        </tr>
        <tr>
            <td align = "center"><input type = "submit" name = "run1" value = "Button 1">
            </td>
            <td align = "center">
                <input type = "submit" name = "run2" value = "Button 2">
            </td>
        </tr>
    </table>
</form>
</body>
</html>
试试这个!:)


样品
你的解决方案

<?php 
    session_start();
    if(!isset($_SESSION['inputs'])){ //check weather $_SESSION['inputs'] exist or not. define if not exist like below.
        $_SESSION['inputs'] = array('','');
    }
    if (isset($_POST['run1'])) { //on click first button assign first input's value to $_SESSION['input'][0];
        $_SESSION["inputs"][0] = $_POST['input1'];
    }
    if (isset($_POST['run2'])) {//on click second button assign first input's value to $_SESSION['input'][1];
        $_SESSION["inputs"][1] = $_POST['input2'];
    }
?>
<html>
    <head>
        <title>Sample </title>
    </head>
    <body>
        <form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
            <table border = "1" align = "center" cellpadding = "10" >
                <tr>
                    <td>Output1 : <b><?php echo $_SESSION['inputs'][0]; ?></b></td>
                    <td>Output2 : <b><?php echo $_SESSION['inputs'][1]; ?></b></td>
                </tr>
                <tr>
                    <td><input type = "text" name = "input1" value="<?php echo $_SESSION['inputs'][0]; ?>" placeholder = "Type here..."></td>
                    <td><input type = "text" name = "input2" value="<?php echo $_SESSION['inputs'][1]; ?>" placeholder = "Type here..."></td>
                </tr>
                <tr>
                    <td align = "center"><input type = "submit" name = "run1" value = "Button 1"></td>
                    <td align = "center"><input type = "submit" name = "run2" value = "Button 2"> </td>
                </tr>
            </table>
        </form>
    </body>
</html>

样品
输出2:

可能使用$_SESSION保存数据,即使页面重新加载。将其保存到任何持久性存储:SESSION、cookie、database,因此forthI已经尝试了$_SESSION,但仍然得到相同的结果:(感谢您的回复,但我仍然遇到同样的问题,当我在第一个输入框中键入“Hello 1”,然后单击按钮1时,它会出现在第一列(输出1)但当我在第二个输入框中键入“Hello 2”并点击按钮2时,第二列(输出2)上有一个输出但是第一列中的输出1消失了?谢谢,感谢您的帮助。您是在尝试同时加载它们,还是让用户执行一个、提交、另一个,然后再次提交并同时显示?如果是这样,您必须使用会话来持久化数据。是的,这就是我尝试执行的操作:P 1。在第一个文本框中输入,然后点击按钮n 1,出现在顶部表格单元格(第1列)2。再次在第二个文本框中输入,然后点击按钮2,然后再次出现在顶部表格单元格(第2列)谢谢!那么是的,如果你想在不同的时间单击提交按钮,那么你需要保存到会话,但是你需要在html输出到页面之前完成。顺便问一下,有没有办法清除我输入框中记录的数据?我的意思是,当我重新加载页面时,所有数据都会被清除?这是图片:再次感谢高级:)非常感谢:)感谢您提供另一个解决方案:)
<?php
# Start session (don't hide warnings)
session_start();
# Check if content being submitted
if(!empty($_POST['input1']))
    $_SESSION['input1'] = $_POST['input1'];
if(!empty($_POST['input2']))
    $_SESSION['input2'] = $_POST['input2'];
?>
<html>
<head>
<title>Sample </title>
</head>
<body>
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
    <table border = "1" align = "center" cellpadding = "10" >
        <tr>
            <td>Output1 : 
                <?php 
                if(!empty($_SESSION['input1']))
                    echo "<b>" .htmlspecialchars($_SESSION['input1']). "</b>";
                ?> 
            </td>
            <td>Output2 : 
                <?php 
                if(!empty($_SESSION['input2']))
                    echo "<b>" .htmlspecialchars($_SESSION['input2']). "</b>";
                ?> 
            </td>
        </tr>
        <tr>
            <td>
                <input type = "text" name = "input1" value = "Type here...">
            </td>
            <td>
                <input type = "text" name = "input2" value = "Type here...">
            </td>
        </tr>
        <tr>
            <td align = "center"><input type = "submit" name = "run1" value = "Button 1">
            </td>
            <td align = "center">
                <input type = "submit" name = "run2" value = "Button 2">
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<html>
<head>
<title>Sample </title>
</head>
<body>

<table border = "1" align = "center" cellpadding = "10" >
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">

<tr>
<td>Output1 : 
  <?php 

  @session_start();


     if (!empty($_POST['input1'])) {

         $_SESSION["input1"] = $_POST['input1'];
         }

     echo "<b>" .$_SESSION["input1"]. "</b>"; 

  ?> 
 </td>

<td>Output2 : 
<?php 
   if (!empty($_POST['input2'])) {
       $_SESSION["input2"] = $_POST['input2'];
       }
     echo "<b>" .$_SESSION["input2"]. "</b>"; 

?>
</td>
</tr>

<tr>
<td><input type = "text" name = "input1" placeholder = "Type here..."></td>
<td><input type = "text" name = "input2" placeholder = "Type here..."></td>
</tr>

<tr>
<td align = "center"><input type = "submit" name = "run1" value = "Button 1"></td>
<td align = "center"><input type = "submit" name = "run2" value = "Button 2"> </td>
</tr>

</form>
</table>
</body>
</html>
<?php 
    session_start();
    if(!isset($_SESSION['inputs'])){ //check weather $_SESSION['inputs'] exist or not. define if not exist like below.
        $_SESSION['inputs'] = array('','');
    }
    if (isset($_POST['run1'])) { //on click first button assign first input's value to $_SESSION['input'][0];
        $_SESSION["inputs"][0] = $_POST['input1'];
    }
    if (isset($_POST['run2'])) {//on click second button assign first input's value to $_SESSION['input'][1];
        $_SESSION["inputs"][1] = $_POST['input2'];
    }
?>
<html>
    <head>
        <title>Sample </title>
    </head>
    <body>
        <form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
            <table border = "1" align = "center" cellpadding = "10" >
                <tr>
                    <td>Output1 : <b><?php echo $_SESSION['inputs'][0]; ?></b></td>
                    <td>Output2 : <b><?php echo $_SESSION['inputs'][1]; ?></b></td>
                </tr>
                <tr>
                    <td><input type = "text" name = "input1" value="<?php echo $_SESSION['inputs'][0]; ?>" placeholder = "Type here..."></td>
                    <td><input type = "text" name = "input2" value="<?php echo $_SESSION['inputs'][1]; ?>" placeholder = "Type here..."></td>
                </tr>
                <tr>
                    <td align = "center"><input type = "submit" name = "run1" value = "Button 1"></td>
                    <td align = "center"><input type = "submit" name = "run2" value = "Button 2"> </td>
                </tr>
            </table>
        </form>
    </body>
</html>