通过session.php传递变量


passing variables through session - php

我是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

这是一个非常令人困惑的

 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);

首先您更改以下代码

 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"];

我想这会对你有所帮助。谢谢