0

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

我遇到问题的任务:

  1. “添加按钮” - 将帖子/学校 ID 添加到会话数组 - $_SESSION['schools']
  2. 顶部的仪表板 - 回显 $_SESSION['schools'] 值(仅用于用户体验,列出当前在列表中的学校)
  3. 当按下“添加按钮”时,会自动更新仪表板列表。最好不要整页。

到目前为止我的尝试:

首先,我评论了 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

当我提交表单时,没有进行第一次更改。当我第二次按下它时,它将更新列表而忽略最后一个字符串。刷新然后整个页面,然后弹出最后一个

我非常喜欢关于这个长主题的建议。也许我不知道在哪里看,但我有点坚持寻找解决方案。

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

4

1 回答 1

0

您在第一次运行时提交表单.. 如果您检查它,您的 URL 更改并在下一次运行.. 因为你得到了这个

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

    }
于 2013-07-03T09:09:26.167 回答