I'm currently building a web-app that has one section where the user sees a chart (which has been pulled from a database of charts) and is prompted to answer a few questions. Once the user has submitted his/her input, I'd like to display his/her answers plus the suggested answers on the same page.
I have built most of the functionality already, but am not sure what is the best method to use for showing both the users answers and the suggested answers on the same page. Right now (shown below), I'm using a big if statement that checks if the form has been submitted and if so, shows the last user submitted answers and the suggested answers. If the form hasn't been submitted, a chart is shown.
The problem is that once the form is submitted (the else part of the statement), the user should still see the chart. I think I could redisplay the chart by storing the id in $_SESSION an开发者_运维百科d pull it again in the else statement but that seems clunky. Can someone recommend what the best way to do this would be (I'm open to using jquery or ajax although I'd have to get myself up to speed on them)?
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
}
try this jquery, and do all the stuff in the targetPage.php
$("#targetDiv").load("targetPage.php",$("#form").serializeArray());
精彩评论