houstoncity wrote:
Could someone help me with the following problem?
I am running the pages on a php server. On page 1, I ask users to
answer one radio-button question which has options (a), (b), (c), and
(d). Based on their answer, I need to load different page 2
accordingly. At the same time, I need to save the answer for the one
question on page 1 to the database. I know how to save the answer to
the database. But, I don't know how to write the code for
differentiating which page to load and where to place the code (on
page 1 or page 2). Could someone help me, please? Thank you very
much.
There are a number a ways to do this. Here are three.
First way, store the URLS of the different "page 2"s in an array
<?
$page2url = array('a' => 'page2a',
'b'=>'another_page2',
'c'=>'yet_another_page2',
'd'=>'page2d')
if (isset($_POST['answer'])){ // $_POST['answer'] has the value of the
radio button
header('location: http://your.web.site/' .
$page2url[$_POST['answer']] . '.php');
exit(); }
?>
Second, name you "page2"s so they have the answer in their filename,
i.e. page2a.php:
<?
if (isset($_POST['answer'])) {
header('location http://your.web.site/page2' . $_POST['answer'] .
'.php');
exit();
?>
Third, keep all of your code in on file, then there is no need to pass
answers anywhere:
<?
if (isset($_POST['answer']))
switch ($_POST['answer']) {
case 'a':
// do work for answer a
break;
case 'b':
// do work for answer b
break;
case 'c':
// do work for answer c
break;
case 'd':
// do work for answer d
break;
}
The above code hasn't been tested, so YMMV.
Ken