tags:

views:

194

answers:

4

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 what my question is :D )

+3  A: 

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)
    }
?>
jheddings
i don' want to use sessions (security and host provider reasons)
kmunky
i know how to use sessions...but i don't want to
kmunky
What security concerns do you have? And in what way does your provider disallow to use sessions?
Tomas Markauskas
i don't know why...but this is what i know...they have turned off register_globals and i'm not allowed to modify them with .htaccess
kmunky
I don't think you understand what it means to have register_globals turned off. You can still use $_SESSION, $_POST, $_GET, $_REQUEST, $_SERVER, etc.
Matt Huggins
You don't need register_globals or .htaccess to use sessions.
Tomas Markauskas
+1  A: 

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.

Alex Mcp
but i can't rely on that 'cause isset($_POST) will return true from the first page...because this is how i get the selected quiz(via post method i send a quiz id to the first page...to know what quiz to load)
kmunky
then say isset($_POST['quizentryitem']) or some other unique member of the post array
Alex Mcp
+1  A: 

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.

jheddings
+1  A: 

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)
timdev
i think this is what misses (that is_array thing)...hmm let me try again with that
kmunky
but if an array is serialized is_array still returns true?
kmunky