收集页面上用户数据的最佳方法是在保留页面其余部分的同时将其显示在同一页面上


Best way to collect user data on a page, display it on the same page while preserving rest of the page

我目前正在构建一个web应用程序,其中有一个部分,用户看到一个图表(已从图表数据库拉),并提示回答几个问题。一旦用户提交了他/她的输入,我想显示他/她的答案加上建议的答案在同一页面上。

我已经建立了大部分的功能,但我不确定什么是最好的方法来显示用户的答案和建议的答案在同一页上使用。现在(如下所示),我正在使用一个大型if语句来检查表单是否已提交,如果已提交,则显示最后一个用户提交的答案和建议的答案。如果表单尚未提交,则显示一个图表。

问题是,一旦提交了表单(语句的else部分),用户仍然应该看到图表。我想我可以通过在$_SESSION中存储id并在else语句中再次拉它来重新显示图表,但这似乎很笨拙。有人能推荐最好的方法来做到这一点(我开放使用jquery或ajax,虽然我必须让自己加快速度)?

if(!isset($_POST['submit']))
    {
        // some code to determine which chart to pull
        if(mysql_num_rows($query))
        {
            while($result = mysql_fetch_array($query))
            {
                $dir = "images/"; // set directory variable
                // open the directory
                if($opendir = opendir($dir))
                {
                    // display the chart to the user
                } 
            }
            // update db table to note that user has seen this problem
        ?>
        <!-- ask for insights -->
        <form action="charts.php" method="POST">
            <input type="text" name="insight[]" placeholder="insight goes here!" />
            <input type="text" name="insight[]" placeholder="more goes here!" />
            <input type="text" name="insight[]" placeholder="don't give up yet" />
            <input type="submit" name="submit" value="Submit data" />
        </form>
        <?php   
        }
        else // throw an error if all questions have been done!
            {
                bringBacktogo($id, $p_type, $count);
            }
    }  
    else
    {
            // some code to show suggested chart insights
            // some code to pull user answers from form above and show them
    } 

试试这个jquery,并完成targetPage.php

中的所有内容
$("#targetDiv").load("targetPage.php",$("#form").serializeArray());