By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Dynamic Form

P: n/a
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.

Apr 4 '07 #1
Share this Question
Share on Google+
26 Replies


P: n/a
Jerim79 wrote:
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.
Firstly, I'd get rid of the single quotes at they come out as
Age'1', but you probably want Age1

For an unknown number of ages you could do:

$ages=array();
foreach( $POST as $key=>$value ) {
if (substr($key,0,3)=="Age") {
$ages[$key]=$_POST[$key];
}
}

If there were 5 ages you would end up with

$ages["Age1"] = <age_entered1>;
$ages["Age2"] = <age_entered2>;
$ages["Age3"] = <age_entered3>;
$ages["Age4"] = <age_entered4>;
$ages["Age5"] = <age_entered4>;

If you don't want an array, you can do:

foreach( $POST as $key=>$value ) {
if (substr($key,0,3)=="Age") {
${$key}=$_POST[$key];
}
}

The ${$key} would make your variables $Age1, $Age2 etc.
Hope that helps, untested, let me know.
Apr 4 '07 #2

P: n/a
On Apr 4, 7:47 am, Tyno Gendo <you@localhostwrote:
Jerim79 wrote:
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.

Firstly, I'd get rid of the single quotes at they come out as
Age'1', but you probably want Age1

For an unknown number of ages you could do:

$ages=array();
foreach( $POST as $key=>$value ) {
if (substr($key,0,3)=="Age") {
$ages[$key]=$_POST[$key];
}
}

If there were 5 ages you would end up with

$ages["Age1"] = <age_entered1>;
$ages["Age2"] = <age_entered2>;
$ages["Age3"] = <age_entered3>;
$ages["Age4"] = <age_entered4>;
$ages["Age5"] = <age_entered4>;

If you don't want an array, you can do:

foreach( $POST as $key=>$value ) {
if (substr($key,0,3)=="Age") {
${$key}=$_POST[$key];
}
}

The ${$key} would make your variables $Age1, $Age2 etc.

Hope that helps, untested, let me know.
Thank you so much. It is nice to finally see someone answer my
question right off the bat. Usually when I post these types of
question, I get about 5 "Why are you doing it that way?" type posts,
along with 3 completely off topic responses and at least one "Let me
see your UML." If I am lucky, after about 15 posts, I might get one
half answer such as "Why don't you use an array?" Honestly, thank you
for answering the question. I wish there was a point system so I could
give you some points. I will try either of the suggestions and let you
know.

Apr 4 '07 #3

P: n/a
On Apr 4, 5:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

What I do is make a form with a large number of blanks (order form,
children stats), and then in validation determine which ones are
filled in.

Now if you want to edit it, you bring the data back in a session array
(to determine change) and an array for the POST form. When validation
is good you compare the POSTed data to the session data and do what
changes are necessary.

This process bypasses the (enter number here) thing as well as the
confusion in trying to add more child elements at a later time.

Apr 4 '07 #4

P: n/a
Jerim79 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.
A much easier way would be to just use an array:

<input name="age[]" ...>

Then your answers will be in the array $_POST['age'] or $_GET['age'],
depending on how you submit the form.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 4 '07 #5

P: n/a
Jerry Stuckle wrote:
A much easier way would be to just use an array:

<input name="age[]" ...>

Then your answers will be in the array $_POST['age'] or $_GET['age'],
depending on how you submit the form.
That's a very good point.. I've used this once before also, top tip.
Apr 4 '07 #6

P: n/a
Tyno Gendo wrote:
Jerry Stuckle wrote:
>A much easier way would be to just use an array:

<input name="age[]" ...>

Then your answers will be in the array $_POST['age'] or $_GET['age'],
depending on how you submit the form.

That's a very good point.. I've used this once before also, top tip.
I was just checking because I thought there was a reason I avoided the
age[] type method once, like perhaps it only sent form values that had
been filled in and were not blank, but this is not the case and it does
send all values, so as I went to the extent of testing it, here's the
code in case anyone needs it ;-)

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$age = $_POST["age"];
if ( is_array($age) ) {
echo "There are " . count($age) . " elements in this
array.";
for ($counter=0;$counter<count($age);$counter++) {
echo "<p>Age $counter is $age[$counter]</p>";
}
} else {
echo "age is not an Array";
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
Age1<input type="text" name="age[]" value="10" /><br />
Age2<input type="text" name="age[]" value="20" /><br />
Age3<input type="text" name="age[]" value="30" /><br />
Age4<input type="text" name="age[]" value="40" /><br />
Age5<input type="text" name="age[]" value="50" /><br />
<input type="submit" />
</form>

Apr 4 '07 #7

P: n/a
On Apr 4, 8:51 am, l...@portcommodore.com wrote:
On Apr 4, 5:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

What I do is make a form with a large number of blanks (order form,
children stats), and then in validation determine which ones are
filled in.

Now if you want to edit it, you bring the data back in a session array
(to determine change) and an array for the POST form. When validation
is good you compare the POSTed data to the session data and do what
changes are necessary.

This process bypasses the (enter number here) thing as well as the
confusion in trying to add more child elements at a later time.
That is a very good idea as well. I am just thinking from a user
standpoint. Some users may want only 1 or 2. Some may want 10 or more.
I am just concerned about a user getting confused by having this long
form to fill out. I hate to say it, but users get confused very
easily. As long as I can, I would like to stick with the dynamic
system. That way, if a user only wants 3, then they won't be confused
by 7 extra "mini-forms." What I am attempting to do, is ask 6
questions for each number they type in. So if they type in 5 for the
number, it will ask them 6*5 questions which is 30.

For example, let's say my first page asks "How many concerts have you
been to in the last year?" A user types in 5, and then presses
Continue. The next screen that comes up, asks the user the same 6
questions for each concert they say they have attended, all on one
form. I would hate for the customer to only need to fill out one set
of questions, but get confused by all the rest of the sets on their
and eventually get give up.

Apr 4 '07 #8

P: n/a
On Apr 4, 10:06 am, Tyno Gendo <you@localhostwrote:
Tyno Gendo wrote:
Jerry Stuckle wrote:
A much easier way would be to just use an array:
<input name="age[]" ...>
Then your answers will be in the array $_POST['age'] or $_GET['age'],
depending on how you submit the form.
That's a very good point.. I've used this once before also, top tip.

I was just checking because I thought there was a reason I avoided the
age[] type method once, like perhaps it only sent form values that had
been filled in and were not blank, but this is not the case and it does
send all values, so as I went to the extent of testing it, here's the
code in case anyone needs it ;-)

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$age = $_POST["age"];
if ( is_array($age) ) {
echo "There are " . count($age) . " elements in this
array.";
for ($counter=0;$counter<count($age);$counter++) {
echo "<p>Age $counter is $age[$counter]</p>";
}
} else {
echo "age is not an Array";
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
Age1<input type="text" name="age[]" value="10" /><br />
Age2<input type="text" name="age[]" value="20" /><br />
Age3<input type="text" name="age[]" value="30" /><br />
Age4<input type="text" name="age[]" value="40" /><br />
Age5<input type="text" name="age[]" value="50" /><br />
<input type="submit" />
</form>
You have all given me some very wonderful and useful ideas. Thank you
all very much. (I just noticed the "Rate this post" ability.)

Apr 4 '07 #9

P: n/a
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

Apr 4 '07 #10

P: n/a
Tyno Gendo wrote:
Tyno Gendo wrote:
>Jerry Stuckle wrote:
>>A much easier way would be to just use an array:

<input name="age[]" ...>

Then your answers will be in the array $_POST['age'] or $_GET['age'],
depending on how you submit the form.

That's a very good point.. I've used this once before also, top tip.

I was just checking because I thought there was a reason I avoided the
age[] type method once, like perhaps it only sent form values that had
been filled in and were not blank, but this is not the case and it does
send all values, so as I went to the extent of testing it, here's the
code in case anyone needs it ;-)
<snip>

That's true for checkboxes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 4 '07 #11

P: n/a
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
>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.
foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 4 '07 #12

P: n/a
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:

<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
>
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I looked at the PHP website and it didn't mention this as a
possibility.

Apr 4 '07 #13

P: n/a
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:

<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
>
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I looked at the PHP website and it didn't mention this as a
possibility.

Apr 4 '07 #14

P: n/a
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:

<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
>
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I looked at the PHP website and it didn't mention this as a
possibility.

Apr 4 '07 #15

P: n/a
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:

<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
>
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I looked at the PHP website and it didn't mention this as a
possibility.

Apr 4 '07 #16

P: n/a
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:

<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
>
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I looked at the PHP website and it didn't mention this as a
possibility.

Apr 4 '07 #17

P: n/a
Another thought, could I create a third array from the fist two arrays
and then read that 2 dimensional array into the table? Or should I
read the array into specific columns on the table? Such as $Age goes
into table.age and $Height goes into table.height.
Apr 4 '07 #18

P: n/a
Another thought, could I create a third array from the fist two arrays
and then read that 2 dimensional array into the table? Or should I
read the array into specific columns on the table? Such as $Age goes
into table.age and $Height goes into table.height.
Apr 4 '07 #19

P: n/a
Jerim79 wrote:
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Jerim79 wrote:
>>On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.
foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:

<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /

I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:

foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I looked at the PHP website and it didn't mention this as a
possibility.
Ah, I missed that. Well, then, do:

for ($i=0; i < $Number; $i++) {
$query="INSERT INTO table() VALUES('$age[$i]','$Height[$i]')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

And BTW - you do know items in a SQL table are unordered. Just because
you insert them in a specific order does not mean they will come out in
the same order.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 4 '07 #20

P: n/a
On Apr 4, 6:22 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
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.
foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:
<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:
foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}
I looked at the PHP website and it didn't mention this as a
possibility.

Ah, I missed that. Well, then, do:

for ($i=0; i < $Number; $i++) {
$query="INSERT INTO table() VALUES('$age[$i]','$Height[$i]')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

And BTW - you do know items in a SQL table are unordered. Just because
you insert them in a specific order does not mean they will come out in
the same order.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thank your for you help so far. I know I keep asking for help, but
honestly I have never done something like this before. I am just
having one issue now. I am correctly passing the $Number value to the
last page. I have used echo commands at the top and bottom of the last
page to verify that it is the number the user typed in on the first
page. However this doesn't write anything to the database:

for ($i=0; i<$Number; $i++) {
$query="INSERT INTO table()
VALUES('$age[$i]','$Height[$i]')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I even tried just outputting the values to the screen with this, but
it doesn't show anything:

for ($n=0; n<$Number; $n++){
echo $Age[$n];
echo $Height[$n];
}

I know that $Number is correct, as I used the echo commands above. My
only other thought is that for some reason, I can't access the array
with $Age[$n]. When I try to print the array location directly to the
screen using $Age[0], $Age[1], etc, it just prints "Array" on the
screen. So the foreach() worked, it just wouldn't print two values on
the same row, but $Age[] doesn't seem to be accessing correctly.
Apr 5 '07 #21

P: n/a
On Apr 5, 8:29 am, "Jerim79" <m...@hotmail.comwrote:
On Apr 4, 6:22 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Jerim79 wrote:
On Apr 4, 4:09 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Jerim79 wrote:
>>On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.comwrote:
>>>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.
> foreach ($Age as $a) {
> $query="INSERT INTO table() VALUES('$a')";
> $result = mysql_query($query) or die('Query failed: ' .
> mysql_error());
> }
>--
>==================
>Remove the "x" from my email address
>Jerry Stuckle
>JDS Computer Training Corp.
>jstuck...@attglobal.net
>==================
Thanks, that worked perfectly. Just one other question. What if I want
two inputs? Such as:
<input type="text" name="Age[] "value="" />
<input type="text" name="Height[]" value="" /
I would need to read both arrays into the same table as they relate to
each other. Such as Age[1]Height[1], then Age[2]Height[2], and so on.
I tried using foreach($Age as $a, $Height as $h) or foreach($Age as $a
AND $Height as $h). Or do I even need to specify the second array in
foreach()? Could I just insert it as such:
foreach ($Age as $a) {
$query="INSERT INTO table() VALUES('$a','$Height')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}
I looked at the PHP website and it didn't mention this as a
possibility.
Ah, I missed that. Well, then, do:
for ($i=0; i < $Number; $i++) {
$query="INSERT INTO table() VALUES('$age[$i]','$Height[$i]')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}
And BTW - you do know items in a SQL table are unordered. Just because
you insert them in a specific order does not mean they will come out in
the same order.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thank your for you help so far. I know I keep asking for help, but
honestly I have never done something like this before. I am just
having one issue now. I am correctly passing the $Number value to the
last page. I have used echo commands at the top and bottom of the last
page to verify that it is the number the user typed in on the first
page. However this doesn't write anything to the database:

for ($i=0; i<$Number; $i++) {
$query="INSERT INTO table()
VALUES('$age[$i]','$Height[$i]')";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

I even tried just outputting the values to the screen with this, but
it doesn't show anything:

for ($n=0; n<$Number; $n++){
echo $Age[$n];
echo $Height[$n];
}

I know that $Number is correct, as I used the echo commands above. My
only other thought is that for some reason, I can't access the array
with $Age[$n]. When I try to print the array location directly to the
screen using $Age[0], $Age[1], etc, it just prints "Array" on the
screen. So the foreach() worked, it just wouldn't print two values on
the same row, but $Age[] doesn't seem to be accessing correctly.
I am wondering if I should declare $Age as an array such as
$Age=array(); before I do $Age[]=$_POST['Age'];. I am just wondering
if PHP is creating the array properly and really identifying it as an
array.

Apr 5 '07 #22

P: n/a
A new situation that has arised is that now the for loop, even though
it doesn't insert anything other than 0 to the database, it seems to
be stuck in an infinite loop. Here is the code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Number=$_POST['Number'];
echo $Number;
$Age[]=$_POST['Age'];
$Height[]=$_POST['Height'];

include ("../functions/db_conn.php");

for ($i=0; i <= $Number; $i++) {
$query="INSERT INTO Age(Age,Height)
VALUES('$Age[$i]','$Height[$i]')";
echo $Age[i], $Height[i];
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

There are two echo $Number commands, one is not pictured here as it is
at the bottom of the page. Both of these confirm that $Number equals
2. I might try a while loop, to just see if that fixes anything. I
don't know why this infinite loop just appeared.

Apr 5 '07 #23

P: n/a
Jerim79 wrote:
A new situation that has arised is that now the for loop, even though
it doesn't insert anything other than 0 to the database, it seems to
be stuck in an infinite loop. Here is the code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Number=$_POST['Number'];
echo $Number;
$Age[]=$_POST['Age'];
$Height[]=$_POST['Height'];

include ("../functions/db_conn.php");

for ($i=0; i <= $Number; $i++) {
$query="INSERT INTO Age(Age,Height)
VALUES('$Age[$i]','$Height[$i]')";
echo $Age[i], $Height[i];
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

There are two echo $Number commands, one is not pictured here as it is
at the bottom of the page. Both of these confirm that $Number equals
2. I might try a while loop, to just see if that fixes anything. I
don't know why this infinite loop just appeared.
Is the code cut and paste? its missing the $ off the i<= $Number
Apr 5 '07 #24

P: n/a
Jerim79 wrote:
A new situation that has arised is that now the for loop, even though
it doesn't insert anything other than 0 to the database, it seems to
be stuck in an infinite loop. Here is the code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Number=$_POST['Number'];
echo $Number;
$Age[]=$_POST['Age'];
$Age = $_POST['Age'];
$Height[]=$_POST['Height'];
$Height = $_POST['Height'];
>
include ("../functions/db_conn.php");

for ($i=0; i <= $Number; $i++) {
for ($i=0; $i <= $Number; $i++) {
$query="INSERT INTO Age(Age,Height)
VALUES('$Age[$i]','$Height[$i]')";
echo $Age[i], $Height[i];
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

There are two echo $Number commands, one is not pictured here as it is
at the bottom of the page. Both of these confirm that $Number equals
2. I might try a while loop, to just see if that fixes anything. I
don't know why this infinite loop just appeared.
I made some corrections above.

$_POST['Age'] is already an array. When you put it into $Age[], you're
putting the entire array into the first element of another array - $Age.
You want to just put it into $Age. The same with $Height.

And the problem with your loop is you were missing a '$' in front of the
second 'i'.

In a development environment you should always have all errors on and
displayed (error_reporting = E_ALL and display_errors = On in your
php.ini file). You would have gotten a notice about 'i'. And if you
hadn't already created $Age and $Height as arrays, you would have gotten
messages there, also.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 5 '07 #25

P: n/a
On Apr 5, 10:36 am, Tyno Gendo <you@localhostwrote:
Jerim79 wrote:
A new situation that has arised is that now the for loop, even though
it doesn't insert anything other than 0 to the database, it seems to
be stuck in an infinite loop. Here is the code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Number=$_POST['Number'];
echo $Number;
$Age[]=$_POST['Age'];
$Height[]=$_POST['Height'];
include ("../functions/db_conn.php");
>
for ($i=0; i <= $Number; $i++) {
$query="INSERT INTO Age(Age,Height)
VALUES('$Age[$i]','$Height[$i]')";
echo $Age[i], $Height[i];
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}
There are two echo $Number commands, one is not pictured here as it is
at the bottom of the page. Both of these confirm that $Number equals
2. I might try a while loop, to just see if that fixes anything. I
don't know why this infinite loop just appeared.

Is the code cut and paste? its missing the $ off the i<= $Number
You guys are completely awesome. That was the problem. I have one
quick parting questions. I should probably just start a new thread,
but since you all seem to know your stuff, I thought I would ask here
first.

I have been playing around with the form, using radio buttons instead
of text boxes. The problem I am having, is that within the while loop
that displays the question for the number of times a user specifies,
all occurrences of that question use the same name for the radio
button. Here is the while loop:

while ($Number!=0){
<input type="radio" name="Age[]" value="20-30">20-30
<input type="radio name="Age[]" value="30-40">30-40
<input type="radio name"Age[]" value="40-50">40-50
$Number--;
}

Let's say someone entered 3 for $Number. When the loop executes it
would produce:

<input type="radio" name="Age[]" value="20-30">20-30
<input type="radio name="Age[]" value="30-40">30-40
<input type="radio name"Age[]" value="40-50">40-50

<input type="radio" name="Age[]" value="20-30">20-30
<input type="radio name="Age[]" value="30-40">30-40
<input type="radio name"Age[]" value="40-50">40-50

<input type="radio" name="Age[]" value="20-30">20-30
<input type="radio name="Age[]" value="30-40">30-40
<input type="radio name"Age[]" value="40-50">40-50

The problem is that with radio buttons, you can only choose one
option. With this code, it only allows the user to choose one option
from among the 9 listed, instead of one from each set. Since it really
needs to be radio buttons, I am trying to find a way to dynamically
name each set. Would something like this work:

<input type="radio name"Age<?php echo $Number ?>[]"
value="40-50">40-50

But that would seem to negate the whole need for arrays, which you all
were so patient in teaching me. This may be more of an HTML question
as how to define sets of radio buttons.
Apr 5 '07 #26

P: n/a
Just some FYI. I redid the form using echo"" commands, instead of
breaking out into HTML. That might make it easier. Maybe I can do
something like:

echo "<input type=\"radio\" name=\"Age$Number[]\" value=
\"40-50\">40-50";

Apr 5 '07 #27

This discussion thread is closed

Replies have been disabled for this discussion.