dongshi7433 2014-04-24 20:08
浏览 22

php表单和会话分配在同一个文件中

I am trying to use post in a form to save form data from a dropdown as a session in the same file as the form

<?php
session_start();
if(isset($_SESSION['post-data']['surnameid']))
  unset($_SESSION['post-data']['surnameid']);
?>     

Then in the body of html

<form action="" method="post">
<?php
include 'connect.inc';

$sql = "SELECT surnameid FROM customer";
$result = mysql_query($sql);

echo "<select name='surnameid'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['surnameid'] . "'>" . $row['surnameid'] .    </option>";
}
include 'close.inc';
?>

<br/> 
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php    
$_SESSION['post-data'] = $_POST;

echo $_SESSION['post-data']['surnameid'];
?>

The assiginment to $_SESSION does not work

  • 写回答

1条回答 默认 最新

  • dtcu5885 2014-04-24 20:15
    关注

    Try this : You forgot a double quote when you are putting echoing

    make sure that you have the rows in the database ( we can't test that ) but i tested with predefined data and this version works

    form.php

        <?php
    session_start();
    
    ?>
    <form action="" method="post">
    <?php
    
    echo "<select name='surnameid'>";
        echo "<option value='1'>test</option>";
        echo "<option value='2'>test2</option>";
    
    ?>
    
    <br/> 
    <input type="submit" name="Submit" value="Submit!" />
    </form>
    <?php    
    
    if( !empty($_POST)){
        $_SESSION['post-data'] = $_POST;
    }
    
    
    if(isset($_SESSION['post-data']['surnameid']))
    echo $_SESSION['post-data']['surnameid'];
    ?>
    

    test_session.php

    <?php
    session_start();
    print_r($_SESSION['post-data']['surnameid']);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数