|
Posted by Jerim79 on 04/04/07 19:32
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.com> wrote:
> I need to create a form that takes a number that the user enters, and
> duplicates a question the number of times the user entered. For
> instance, if the customer enters 5 on the first page, when they press
> next the form generates "How old are you?" 5 times on the page. The
> customer will answer all 5 questions then press next. Finally, all the
> local variables get dynamically created and written to a database.
>
> I have already taken care of dynamically creating the question five
> times. Using a simple WHILE clause, this generates the correct number
> of questions. What I am having an issue with at this point, is how to
> dynamically create the local variables.For instance, for the question
> "How old are you?" I would need a unique variable for each instance;
> using something like $Age(n). So that for five times, it would
> automatically created variables $Age1 through $Age5. I have carried
> over the number to the submit page by creating the variable
$Number
> and passing it along as hidden button on the form. $Age'$Number'
> seemed to work for creating the variable, but $_post["Age'$Number'"]
> doesn't work for referencing the global variable. I would need some
> way of looping through and dynamically creating the variables.
>
> I am willing to rethink my approach to the whole problem, so the door
> is wide open to suggestions.
I have been playing around with the various suggestions here. I
haven't gotten anything to work yet. I have read the PHP manual pages.
Maybe I need to give a little bit better detail about what I am trying
to do. The first page of my form is a simple screen that just asks for
a number. Such as this:
INDEX.HTML
<html>
<body>
<form action="survey.php" method="POST">
How many people are in your family? <input type="text"
name="Number" value="" />
<input type="Submit" name="Submit" value="Submit" />
</form>
</body>
</html>
After clicking on Submit, a second screen pops up. This is the PHP
script, with a while loop that displays the same question for the
amount of times the user entered on the last page:
SURVEY.PHP
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Number=$_POST['Number'];
$Number2=$Number;
?>
<form action="submit.php" method="POST">
while ($Number!=0){
echo "How old is person number $Number? " ?
>
<input type="text" name="Age[]"value="" />
<?php
echo Age[$Number];
$Number--;
}
?>
<input type="hidden" name="Number" value="<? echo
$Number; ?>">
<input type="Submit" name="Submit" value="Submit">
</form> <?php
}
else{
echo "You don't have permission to access this page directly.";
}
Okay, so now we have asked the user for the ages of all of the family
members the user told us they had. Now we must write this to the
database, or at least get it over to the form to write it to the
database. So:
SUBMIT.PHP
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Number=$_POST['Number'];
$Number2=$_POST['Number'];
$Age[]=$_POST['Age'];
while ($Number>-1){
$query="INSERT INTO table()
VALUES('$Age[$Number]')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
$Number--;
}
mysql_close($connection);
}
else{
echo "You don't have permission to access this page directly.";
}
Forgetting any other issues, such as why I break out of PHP to do HTML
stuff, the fact that when I read the numbers into the table they will
be in reverse order or that this may not reflect the way you would do
it, what else do I need to do to make this code work correctly? I
understand some may have an issue with why I use three forms, when I
could use just one. That issue aside for the moment, I am trying to
learn the easiest way to do this. It is broken out so I can better
understand the process. Once it is working correctly and I understand
how it works, then I will be happy to consolidate as much as I can. My
main problem right now is between the SURVEY.PHP script and the
SUBMIT.PHP script. I can't seem to get any of the Ages[] over to the
SUBMIT.PHP. I have even tried echoing the values manually such as echo
$Age[0], with no luck.
I haven't tried the second way that was posted here, mostly because I
am know what to put in the SUBMIT.PHP form as far as <input /> fields.
One other way I have tried is that in the SURVEY.PHP form I used
<input type="text" name="Age[<? echo $Number; ?>]" ?>" value"" />
inside of the while loop. I could probably also do name="<? echo
$Age[$Number]; ?>" and let PHP handle the array. Don't even know if
that will work.
[Back to original message]
|