开发者

PHP quiz send data to next page

ok, i'm trying to do a quiz...all good by now. but when i'm trying to send the collected data(radio buttons values) through pages i can't get the logic flow. I have the main idea but i can;t put it into practice.

  1. i want to collect all radio values
  2. create an array containing this values
  3. serialize the array
  4. put the serialized array into a hidden input

the problem is that i want to send data on the same page via $_SERVER['PHP_SELF'] and i don;t know when in time to do those things.(cause on "first" page of the quiz i have nothing to receive, then on the "next" page i receive the S_POST['radio_names'] and just after the second page i can get that hidden input). i hope i made myself understood (it's hard even for me to understand 开发者_如何学Cwhat my question is :D )


You could try to use the $_SESSION object instead... For each page of your quiz, store up the results in the $_SESSION array. On the summary page, use this to show your results.

To accomplish this, on the beginning of each page, you could put something like:

<?
    session_start();
    foreach ($_POST as $name => $resp) {
      $_SESSION['responses'][name] = $resp;
    }
?>

Then, on the last page, you can loop through all results:

<?
    session_start();
    foreach ($_SESSION['responses'] as $name => $resp) {
      // validate response ($resp) for input ($name)
    }
?>


Name your form fields like this:

<input type="radio" name="quiz[page1][question1]" value="something"/>
...
<input type="hidden" name="quizdata" value="<?PHP serialize($quizdata); ?>"/>

Then when you process:

<?PHP
//if hidden field was passed, grab it.
if (! empty($_POST['quizdata'])){
    $quizdata = unserialize($_POST['quizdata']);
}

// if $quizdata isn't an array, initialize it.
if (! is_array($quizdata)){
    $quizdata = array();
}

// if there's new question data in post, merge it into quizdata
if (! empty($_POST)){
    $quizdata = array_merge($quizdata,$_POST['quiz']);
}

//then output your html fields (as seen above)


As another approach, you could add a field to each "page" and track where you are. Then, in the handler at the top of the page, you would know what input is valid:

<?
  if (isset($_POST['page'])) {
    $last_page = $_POST['page'];
    $current_page = $last_page + 1;
    process_page_data($last_page);
  } else {
    $current_page = 1;
  }
?>

... later on the page ...

<? display_page_data($current_page); ?>
<input type="hidden" name="page" value="<?= $current_page ?>" />

In this example, process_page_data($page) would handle reading all the input data necessary for the given page number and display_page_data($page) would show the user the valid questions for the given page number.

You could expand this further and create classes to represent pages, but this might give you an idea of where to start. Using this approach allows you to keep all the data handling in the same PHP script, and makes the data available to other functions in the same script.


You want to use a flow such as

if (isset $_POST){
    //do the data processing and such 
    }
else {
/show entry form
}

That's the most straight forward way I know of to stay on the same page and accept for data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜