astonecipher1 Posted December 5, 2012 Share Posted December 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271621-run-time-variables/ Share on other sites More sharing options...
kicken Posted December 5, 2012 Share Posted December 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271621-run-time-variables/#findComment-1397642 Share on other sites More sharing options...
gizmola Posted December 5, 2012 Share Posted December 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271621-run-time-variables/#findComment-1397643 Share on other sites More sharing options...
astonecipher1 Posted December 5, 2012 Author Share Posted December 5, 2012 Thank you so much! I have been working on a solution for two days and after posting in another forum I was just told the way I was doing it was wrong without an actual way to implement. Quote Link to comment https://forums.phpfreaks.com/topic/271621-run-time-variables/#findComment-1397652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.