2

我是 php 新手,我正在尝试通过会话传递提交按钮的值。

到目前为止,我的代码如下所示:

main_page.php

     session_start();
     echo '<form method="post" action="details_page.php">';
 echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
 echo '</form>';
     $value_to_pass = $_POST[submit];
 echo $value_to_pass;
     $_SESSION['value'] = $value_to_pass;

}

details_page.php

session_start();
    echo $value_to_pass = $_SESSION['value'];
    echo $value_to_pass;

我需要它在 details_page.php 中打印 $value_to_pass

4

2 回答 2

0

首先你改变你下面的代码

 echo '<form method="post" action="details_page.php">';
 echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
 echo '</form>';

与以下

 echo '<form method="post" action="details_page.php">';
 echo '<input type="hidden" name="ip_address" id="ip_address" value="'.$ip_address.'">';
 echo '<li><a href="ifconfig.php> <input type="submit" name=submit /></a></li>';
 echo '</form>';

这实际上是最佳实践。我也跟着这个。所以尽量避免在“提交”按钮中传递值。将其传递给“隐藏”字段。

然后得到如下值: -

 $value_to_pass = $_POST["ip_address"];

我想它会帮助你。谢谢。

于 2013-08-09T11:20:55.970 回答
0

这非常令人困惑

 session_start();
 echo '<form method="post" action="details_page.php">';
 echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
 echo '</form>';
 $value_to_pass = $_POST[submit];
 echo $value_to_pass;
 $_SESSION['value'] = $value_to_pass;

考虑将其更改为此,以便您的 POST 代码仅在实际提交表单时执行。如果没有此检查,您的 SESSION 将在未提交表单时被分配一个空白值,这可能会给您带来奇怪的结果。

 session_start();
 echo '<form method="post" action="details_page.php">';
 echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
 echo '</form>';

 // Note also you need single quotes in the $_POST array around 'submit'
 if(isset($_POST['submit']))
 {
   $value_to_pass = $_POST[submit];
   echo $value_to_pass;
   $_SESSION['value'] = $value_to_pass;
 }

并将 details_page.php 更改为

session_start();
// Do not echo this line, as you are echoing the assignment operation.
$value_to_pass = $_SESSION['value'];
var_dump($value_to_pass);
于 2013-08-09T11:17:14.223 回答