如何在提交时自动刷新会话数组项列表?

时间:2013-07-03 08:55:16

标签: php arrays submit refresh

我正在学校比较网站。为此,我需要一个处理该功能的插件。我将会话数据保存为学校ID,以便在选择学校后可以在比较表中传递。

我遇到问题的任务:

  1. “添加按钮” - 将会/学校ID添加到会话数组 - $ _SESSION ['schools']
  2. 顶部的仪表板 - echo $ _SESSION ['schools']值(仅供用户体验列出目前列表中的学校)
  3. 按下“添加按钮”时,自动更新仪表板列表。最好不要整页。
  4. 到目前为止我的尝试:

    首先,我评论了PHP表单操作:

        <?php   session_start();    
        $schools = array('post_id');
    
        //If form not submitted, display form. 
        if (!isset($_POST['submit_school'])){
    
        //If form submitted, process input.
        } else {
            //Retrieve established school array.
            $schools=($_POST['school']);
            //Convert user input string into an array.
            $added=explode(',',$_POST['added']);
    
            //Add to the established array.
            array_splice($schools, count($schools), 0, $added);
            //This could also be written $schools=array_merge($schools, $added);
    
        }
    
        $_SESSION['schools'] = $schools;
    ?>
    

    接下来是表单本身:

        <form method="post" action="http://henrijeret.ee/7788/temp_add_button.php" id="add_school">
        <input type="hidden" name="added" value="Value" size="80" />
        <?php
            //Send current school array as hidden form data.
            foreach ($schools as $s){
                echo "<input type=\"hidden\" name=\"school[]\" value=\"$s\" />\n";
            }
        ?>
        <input type="submit" name="submit_school" value="Lisa võrdlusesse" />
    </form>
    

    对于我使用的仪表板:

        <?php
    
    
        foreach($_SESSION['schools'] as $key => $value){
            // and print out the values
            echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
        }
    ?>
    

    这只是一个原型,让我的头脑包裹在我面前的任务......

    问题 有些事情感觉不对..:P

    当我提交表单时,则不会进行第一次更改。当我第二次按下它时,它会更新列表,而不是最后一个字符串。刷新然后整页,然后弹出最后一页

    我非常赞赏这个长话题的建议.Maby我不知道在哪里看,但我有点陷入寻找解决方案..

    链接到我正在运行的代码http://henrijeret.ee/7788/

1 个答案:

答案 0 :(得分:0)

您是在第一次运行时提交表单..如果您将检查您的网址更改以及下次运行..自从您获得此

  //If form not submitted, display form. 
    if (!isset($_POST['submit_school'])){

    //If form submitted, process input.
    } else {
        //Retrieve established school array.
        $schools=($_POST['school']);
        //Convert user input string into an array.
        $added=explode(',',$_POST['added']);

        //Add to the established array.
        array_splice($schools, count($schools), 0, $added);
        //This could also be written $schools=array_merge($schools, $added);

    }

它将转到else语句,因为已经设置了POST。

试试这个:

 //If form not submitted, display form. 
    if (!isset($_POST['submit_school'])){

    //If form submitted, process input.
    } else {
        //Retrieve established school array.
        $schools=($_POST['school']);
        //Convert user input string into an array.
        $added=explode(',',$_POST['added']);

        //Add to the established array.
        array_splice($schools, count($schools), 0, $added);
        //This could also be written $schools=array_merge($schools, $added);

       $_SESSION['schools'] = $schools;

    }