Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.