2011-04-21 51 views
0

我目前正在建设一个web应用程序,有一个部分,在那里用户看到的图表(已经从拉的休息一个图表数据库),并提示回答几个问题。一旦用户提交了他/她的输入,我想在同一页面上显示他/她的答案和建议的答案。的最佳方式,在同一页上显示,同时保留页面

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

问题是,一旦表单被提交(语句的其他部分),用户仍应该看到图表。我想我可以重新显示图表,方法是将ID存储在$ _SESSION中,然后再次将其拖入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 
    } 
+1

移动你的表单处理程序到不同的页面,并使用AJAX?这可能听起来过于简单,但你的问题有点宽泛。 – HurnsMobile 2011-04-21 14:13:51

回答

0

试试这个jQuery的,做所有的东西在targetPage.php

$("#targetDiv").load("targetPage.php",$("#form").serializeArray()); 
+0

谢谢Sourav。我没有用想象中的那么实现,但AJAX和jQuery是解决一个很好的结合。 – 2011-04-22 18:27:29

相关问题