Jump to content

Run Time Variables


astonecipher1

Recommended Posts

Complex question. Can you create and store variables at runtime?

 

Part of my web app gives the admin the ability to create a multiple choice test. One page asks the admin how many questions will be in the quiz. Then, on the next page a loop runs creating X number inputs for questions, answers 1 - 4, and the correct answer.

 

My problem is, I do not know how many questions will be entered. I tried to create the variables dynamically with $question . $i, $i being the question number making it specific to those questions, answers, and the correct answer. But, the variables do not pass because of that or because of how.

 

Is there a way to do this?

Link to comment
https://forums.phpfreaks.com/topic/271621-run-time-variables/
Share on other sites

Use an array to store the question data.  To have your form inputs create an array, name them with [] on the end.  Eg:

<form method="post">
 Question 1: <input type="text" name="question[]">
 Question 2: <input type="text" name="question[]">
 Question 3: <input type="text" name="question[]">
 ...
</form>

 

In your PHP code then, $_POST['question'] will be an array of all the values submitted.  You can use a foreach() loop to iterate over it and do whatever you need to for each question.

 

 

Link to comment
https://forums.phpfreaks.com/topic/271621-run-time-variables/#findComment-1397642
Share on other sites

The way people do this currently is to utlize javascript/dhtml in the client that dynamically alters the form on the clientside. Libraries like jquery have greatly decreased the complexity of doing this type of development, and making it highly likely the resulting solution will work across browsers. Kicken has demonstrated a nice trick that facilitates this greatly on the serverside.

Link to comment
https://forums.phpfreaks.com/topic/271621-run-time-variables/#findComment-1397643
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.