473,715 Members | 6,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Form

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'$Numbe r'"]
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
26 2807
Jerim79 wrote:
and passing it along as hidden button on the form. $Age'$Number'
seemed to work for creating the variable, but $_post["Age'$Numbe r'"]
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
On Apr 4, 7:47 am, Tyno Gendo <you@localhostw rote:
Jerim79 wrote:
and passing it along as hidden button on the form. $Age'$Number'
seemed to work for creating the variable, but $_post["Age'$Numbe r'"]
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
On Apr 4, 5:27 am, "Jerim79" <m...@hotmail.c omwrote:
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'$Numbe r'"]
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
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'$Numbe r'"]
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*******@attgl obal.net
=============== ===
Apr 4 '07 #5
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
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;$co unter<count($ag e);$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
On Apr 4, 8:51 am, l...@portcommod ore.com wrote:
On Apr 4, 5:27 am, "Jerim79" <m...@hotmail.c omwrote:
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'$Numbe r'"]
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
On Apr 4, 10:06 am, Tyno Gendo <you@localhostw rote:
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;$co unter<count($ag e);$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
On Apr 4, 7:27 am, "Jerim79" <m...@hotmail.c omwrote:
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'$Numbe r'"]
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=$Numbe r;

?>
<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($qu ery) or die('Query failed: ' .
mysql_error());
$Number--;

}
mysql_close($co nnection);
}

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1976
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of fields. I'd like to create a page in which all of this is stored as you move along as hidden variables, until the end, when the user submits. I can't figure out one thing: I have dynamic form elements (dropdowns), that I'd like to use instead of...
1
17667
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
0
3514
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list boxes do not necessarily need to have a selection made to be used in the dynamic query. In essence the form can have selections made in all or none of its list boxes to form the dynamic query I am looking to get some feedback in reference to...
3
2940
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process. My Current Form (6 tabs): - Owner, Property, Title, Docs, Queries, & Reports - User is able to see (while navigating through the tabs) above in the form : Owner Name, Address, Parcel, and SSN
1
6320
by: Will | last post by:
Hi all. I'm learning VB.Net and am developing a WinForms app. I'm trying to make an app that I will use to scan in one or more than on image. I want to use a tabbed interface to hold each image. Here's the code I'm using for testing purposes. I've got the code in the form's load event, but I think I'd have the same problems no matter where the code existed. Right now, the form has an empty tab control, everthing else is dynamic. <code>
6
2921
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library that sets the properties send/passed from the main windows form. I'm having problems with the class library, the button control collection and my referencing it ie this.Control.Add(aControl);. Any and all help is appreciated. Thanks in advance.
3
1808
by: Tyler Carver | last post by:
I am trying to use some dynamic controls that are built and then added to tables. The problem that I am having is the timing of when I can populate the controls and have the state remain after a postback. The main question would be this: Why does this work for maintaining state after a postback for dynamic controls: myText = new Label(); myText.ID = "myText";
7
1888
by: Abraham Luna | last post by:
how do i stop the dynamic validators from breaking explorer if i use a dynamic validator and move to a different control it breaks explorer and i can type in the page when i'm not supposed to. thank you.
8
2015
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code for the button. (This is done by the form itself, not by using VS.Net) (button and source code should be a record in database, these information should be retrieve from database when the form shows up) What I want is:
2
2939
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic sub-report will capture what grades the student has achieved in a list of different subjects and the reason I need it to be dynamic is that students take different subjects. Basically I've been trying to doctor the KB article on dynamic
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.