473,390 Members | 1,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

submitting form from button or dropdown

I'm trying to setup a dyamic dropdown list that displays a number of
text fields based on the selected number in the dropdown. The problem
I am running into is capturing the data already entered before the list
is repopulated. For example, suppose the user selects 3 in the drop
down list and 3 text fields are shown. If the user populates the 3 text
fields with data and decides to change the drop down to a list of 4 or
5, how do I capture the 3 fields that are entered? It appears that if
I include the "Add" button in the same form as the onchange=submit for
the drop down list the form gets screwed up. Perhaps there is a better
way to do this with php?

Thanks for any help...Here's my test file:

<?php
$showCity = $_REQUEST['showCity'];
if(empty($showCity) || $showCity == "") {
$showCity = 1;
}
$cities = array();
for($i = 0; $i<$showCity;$i++) {
if($_REQUEST['city'.$i] != "") {
$cities[$i] = $_REQUEST['city$i'];
print "city $i = " . $cities[$i] . "<br>";
} else {break;}

}
print "cities.length = ".count($cities) . "<br>";
?>

<html>
<body>
<table border="0">
<form method="post" action="test.php">
<tr>
<td colspan="4">Define up to 50 City/State combinations.
<br/><br/></td>
</tr>
<tr>
<td colspan="4">Insert
<select name="showCity" onchange="submit()" >
<?php
for($i = 0; $i < 50; $i++) {
$selected = "";
if(($i+1) == $showCity) {
$selected = "selected";
}
?>
<option <?php echo($selected)?value="<?php echo($i+1)?>"><?php
echo($i+1)?></option>
<?php
}
?>
</selectCities:<br/>
</td>
</tr>

<?php
for($i = 0; $i < $showCity; $i++) {
?>

<tr>
<td>City:</td>
<td>&nbsp;<input type="text" name="city<?php echo($i)?>" size="20"
value="<?php echo($cities[$i])?>" maxlength="100" /></td>
<td align="right">&nbsp;&nbsp;&nbsp;<b><u>State</u>:</b></td>
<td>
<select name="state<?php echo($i)?>" size="1" >
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option selected value="CA">CA</option>
</select>
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><br/>
<input type="submit" name="submit" value="Add" class="button" />
</td>
</tr>
</form>
</table>
</body>
</html>

Jan 16 '07 #1
4 2595

Greg Scharlemann wrote:
I'm trying to setup a dyamic dropdown list that displays a number of
text fields based on the selected number in the dropdown. The problem
I am running into is capturing the data already entered before the list
is repopulated. For example, suppose the user selects 3 in the drop
down list and 3 text fields are shown. If the user populates the 3 text
fields with data and decides to change the drop down to a list of 4 or
5, how do I capture the 3 fields that are entered? It appears that if
I include the "Add" button in the same form as the onchange=submit for
the drop down list the form gets screwed up. Perhaps there is a better
way to do this with php?

Thanks for any help...Here's my test file:

<?php
$showCity = $_REQUEST['showCity'];
if(empty($showCity) || $showCity == "") {
$showCity = 1;
}
$cities = array();
for($i = 0; $i<$showCity;$i++) {
if($_REQUEST['city'.$i] != "") {
$cities[$i] = $_REQUEST['city$i'];
print "city $i = " . $cities[$i] . "<br>";
} else {break;}

}
print "cities.length = ".count($cities) . "<br>";
?>

<html>
<body>
<table border="0">
<form method="post" action="test.php">
<tr>
<td colspan="4">Define up to 50 City/State combinations.
<br/><br/></td>
</tr>
<tr>
<td colspan="4">Insert
<select name="showCity" onchange="submit()" >
<?php
for($i = 0; $i < 50; $i++) {
$selected = "";
if(($i+1) == $showCity) {
$selected = "selected";
}
?>
<option <?php echo($selected)?value="<?php echo($i+1)?>"><?php
echo($i+1)?></option>
<?php
}
?>
</selectCities:<br/>
</td>
</tr>

<?php
for($i = 0; $i < $showCity; $i++) {
?>

<tr>
<td>City:</td>
<td>&nbsp;<input type="text" name="city<?php echo($i)?>" size="20"
value="<?php echo($cities[$i])?>" maxlength="100" /></td>
<td align="right">&nbsp;&nbsp;&nbsp;<b><u>State</u>:</b></td>
<td>
<select name="state<?php echo($i)?>" size="1" >
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option selected value="CA">CA</option>
</select>
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><br/>
<input type="submit" name="submit" value="Add" class="button" />
</td>
</tr>
</form>
</table>
</body>
</html>
The problem is that when you assign the value of the $_REQUEST variable
to your array, you use single quotes around city$i, so the $i doesn't
get resolved the way you want. If you use double quotes, like:

$cities[$i] = $_REQUEST["city$i"];

You'll get the behavior you expected.

Jan 16 '07 #2
I had a similar problem and through much trial and effort i solved it.
I can send you a watered down copy of the solution if you like. Now
Before i begin there are most probably numerous ways to do this and
with that said i didnt exactly do it as u did. my variable that
determined how many passengers,dogs monkey whatever came from another
page that posted but it can easily be done like this(only 3 options to
be brief)

so here is the dropdown on the page passengers.htm

<form method="POST" action="dynamic.php">
<select name="passengers" >
<option value="0">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" name="send" value="submit">

Then capture the option in the PHP of the next form called dynamic.php

<html>
//usual web page code here..(<headetc)
<?php
if (isset($_POST['send']) ) // from previous page * See NOTE below
{
$pass = $_POST['passengers'];
//here is where we dynamically "grow" the form if the # of passengers
is 0 nothing is displayed. if its 1, one passenger with a firstname and
surname field is displayed etc...
for ($counter = 0; $counter < $pass; $counter ++)
{
$offset = $counter + 1;
?>
<form method="post" name="passform" action="dynamic.php" >
<table >
<tr>
<Th >Passenger <?php echo $offset;?></Th>//$offset Variable is for the
Passenger # 1,2,3 etc
</tr>
<tr>
<td >Surname : </td>
<td ><input name="sname[]" type="text" value="<?php echo
$_POST['sname'][$counter];?>">
//this is to store the variable for later processing &validation
//Note the variable name is 'sname[]' to account for an array or number
of sname fields
//value="<?php echo $_POST['sname'][$counter];?>" keeps track of
particular field and in case //there is an error your user doesnt need
to re-fill the form. it is also a means of you to track the //variable
for later processing
</td>
</tr>
<tr>
<tr>
<td >Surname : </td>
<td ><input name="fname[]" type="text" value="<?php echo
$_POST['fname'][$counter];?>"//this is to store the variable for
later processing &validation
</td>
</tr>
<tr>
<td>
<input name="send" type="submit" value="Send Request">
/*NOTE: This submit button is called 'send' like the previous
page.Thus the selected # of passengers will immediately appear on this
'dynamic.php' form. To do this on one page its very possible just be
carefull about your naming, you'l need to create another form within
the page and submit it somehow. Javascript if onSelect or a submit and
post via PHP(Remember to remove this note its not a proper html comment
but no biggie)*/
</td>
</tr>
<table >
<?php
}
// we opened two braces earlier after we checked if 'send' was set from
previous page to add our
//dynamic content so lets close them
}
?>
//usual code here web page code here..
<html>

// The validation of the form can become very tricky once you have
numerous fields that must be //added, securely validated,formatted and
submitted. So i wont get into that now. But im sure can //figure that
out. As i said i can email you a perfectly functional version with
validation. But this is a //starting point

Later Timani (php_Boi)

BJMurphy wrote:
Greg Scharlemann wrote:
I'm trying to setup a dyamic dropdown list that displays a number of
text fields based on the selected number in the dropdown. The problem
I am running into is capturing the data already entered before the list
is repopulated. For example, suppose the user selects 3 in the drop
down list and 3 text fields are shown. If the user populates the 3 text
fields with data and decides to change the drop down to a list of 4 or
5, how do I capture the 3 fields that are entered? It appears that if
I include the "Add" button in the same form as the onchange=submit for
the drop down list the form gets screwed up. Perhaps there is a better
way to do this with php?

Thanks for any help...Here's my test file:

<?php
$showCity = $_REQUEST['showCity'];
if(empty($showCity) || $showCity == "") {
$showCity = 1;
}
$cities = array();
for($i = 0; $i<$showCity;$i++) {
if($_REQUEST['city'.$i] != "") {
$cities[$i] = $_REQUEST['city$i'];
print "city $i = " . $cities[$i] . "<br>";
} else {break;}

}
print "cities.length = ".count($cities) . "<br>";
?>

<html>
<body>
<table border="0">
<form method="post" action="test.php">
<tr>
<td colspan="4">Define up to 50 City/State combinations.
<br/><br/></td>
</tr>
<tr>
<td colspan="4">Insert
<select name="showCity" onchange="submit()" >
<?php
for($i = 0; $i < 50; $i++) {
$selected = "";
if(($i+1) == $showCity) {
$selected = "selected";
}
?>
<option <?php echo($selected)?value="<?php echo($i+1)?>"><?php
echo($i+1)?></option>
<?php
}
?>
</selectCities:<br/>
</td>
</tr>

<?php
for($i = 0; $i < $showCity; $i++) {
?>

<tr>
<td>City:</td>
<td>&nbsp;<input type="text" name="city<?php echo($i)?>" size="20"
value="<?php echo($cities[$i])?>" maxlength="100" /></td>
<td align="right">&nbsp;&nbsp;&nbsp;<b><u>State</u>:</b></td>
<td>
<select name="state<?php echo($i)?>" size="1" >
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option selected value="CA">CA</option>
</select>
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><br/>
<input type="submit" name="submit" value="Add" class="button" />
</td>
</tr>
</form>
</table>
</body>
</html>

The problem is that when you assign the value of the $_REQUEST variable
to your array, you use single quotes around city$i, so the $i doesn't
get resolved the way you want. If you use double quotes, like:

$cities[$i] = $_REQUEST["city$i"];

You'll get the behavior you expected.
Jan 17 '07 #3
I had a similar problem and through much trial and effort i solved it.
I can send you a watered down copy of the solution if you like. Now
Before i begin there are most probably numerous ways to do this and
with that said i didnt exactly do it as u did. my variable that
determined how many passengers,dogs monkey whatever came from another
page that posted but it can easily be done like this(only 3 options to
be brief)

so here is the dropdown on the page passengers.htm

<form method="POST" action="dynamic.php">
<select name="passengers" >
<option value="0">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" name="send" value="submit">

Then capture the option in the PHP of the next form called dynamic.php

<html>
//usual web page code here..(<headetc)
<?php
if (isset($_POST['send']) ) // from previous page * See NOTE below
{
$pass = $_POST['passengers'];
//here is where we dynamically "grow" the form if the # of passengers
is 0 nothing is displayed. if its 1, one passenger with a firstname and
surname field is displayed etc...
for ($counter = 0; $counter < $pass; $counter ++)
{
$offset = $counter + 1;
?>
<form method="post" name="passform" action="dynamic.php" >
<table >
<tr>
<Th >Passenger <?php echo $offset;?></Th>//$offset Variable is for the
Passenger # 1,2,3 etc
</tr>
<tr>
<td >Surname : </td>
<td ><input name="sname[]" type="text" value="<?php echo
$_POST['sname'][$counter];?>">
//this is to store the variable for later processing &validation
//Note the variable name is 'sname[]' to account for an array or number
of sname fields
//value="<?php echo $_POST['sname'][$counter];?>" keeps track of
particular field and in case //there is an error your user doesnt need
to re-fill the form. it is also a means of you to track the //variable
for later processing
</td>
</tr>
<tr>
<tr>
<td >Surname : </td>
<td ><input name="fname[]" type="text" value="<?php echo
$_POST['fname'][$counter];?>"//this is to store the variable for
later processing &validation
</td>
</tr>
<tr>
<td>
<input name="send" type="submit" value="Send Request">
/*NOTE: This submit button is called 'send' like the previous
page.Thus the selected # of passengers will immediately appear on this
'dynamic.php' form. To do this on one page its very possible just be
carefull about your naming, you'l need to create another form within
the page and submit it somehow. Javascript if onSelect or a submit and
post via PHP(Remember to remove this note its not a proper html comment
but no biggie)*/
</td>
</tr>
<table >
<?php
}
// we opened two braces earlier after we checked if 'send' was set from
previous page to add our
//dynamic content so lets close them
}

?>
//usual code here web page code here..
<html>

The validation of the form can become very tricky once you have
numerous fields that must be added, securely validated,formatted and
submitted. So i wont get into that now. But im sure can figure that
out. As i said i can email you a perfectly functional version with
validation. But this is a starting point

i can also tell you that this

<select name="state<?php echo($i)?>" size="1" >
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option selected value="CA">CA</option>
</select>

will be a problem in case there is need to validate and the page
're-POSTS' to itself coz it wont retain the value.especially if you
have a number of them in an array

<select name="state[]" size="1" >//will be tracked by array hence
"state[]"
<option value="AZ"<?php if (($_POST['Ptitle'][$counter] == '1')) {
echo '
selected="selected"'; } ?>AZ</option>
//etc...
</select>

Hope it helps

Later Timani (php_Boi)

BJMurphy wrote:
Greg Scharlemann wrote:
I'm trying to setup a dyamic dropdown list that displays a number of
text fields based on the selected number in the dropdown. The problem
I am running into is capturing the data already entered before the list
is repopulated. For example, suppose the user selects 3 in the drop
down list and 3 text fields are shown. If the user populates the 3 text
fields with data and decides to change the drop down to a list of 4 or
5, how do I capture the 3 fields that are entered? It appears that if
I include the "Add" button in the same form as the onchange=submit for
the drop down list the form gets screwed up. Perhaps there is a better
way to do this with php?

Thanks for any help...Here's my test file:

<?php
$showCity = $_REQUEST['showCity'];
if(empty($showCity) || $showCity == "") {
$showCity = 1;
}
$cities = array();
for($i = 0; $i<$showCity;$i++) {
if($_REQUEST['city'.$i] != "") {
$cities[$i] = $_REQUEST['city$i'];
print "city $i = " . $cities[$i] . "<br>";
} else {break;}

}
print "cities.length = ".count($cities) . "<br>";
?>

<html>
<body>
<table border="0">
<form method="post" action="test.php">
<tr>
<td colspan="4">Define up to 50 City/State combinations.
<br/><br/></td>
</tr>
<tr>
<td colspan="4">Insert
<select name="showCity" onchange="submit()" >
<?php
for($i = 0; $i < 50; $i++) {
$selected = "";
if(($i+1) == $showCity) {
$selected = "selected";
}
?>
<option <?php echo($selected)?value="<?php echo($i+1)?>"><?php
echo($i+1)?></option>
<?php
}
?>
</selectCities:<br/>
</td>
</tr>

<?php
for($i = 0; $i < $showCity; $i++) {
?>

<tr>
<td>City:</td>
<td>&nbsp;<input type="text" name="city<?php echo($i)?>" size="20"
value="<?php echo($cities[$i])?>" maxlength="100" /></td>
<td align="right">&nbsp;&nbsp;&nbsp;<b><u>State</u>:</b></td>
<td>
<select name="state<?php echo($i)?>" size="1" >
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option selected value="CA">CA</option>
</select>
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><br/>
<input type="submit" name="submit" value="Add" class="button" />
</td>
</tr>
</form>
</table>
</body>
</html>

The problem is that when you assign the value of the $_REQUEST variable
to your array, you use single quotes around city$i, so the $i doesn't
get resolved the way you want. If you use double quotes, like:

$cities[$i] = $_REQUEST["city$i"];

You'll get the behavior you expected.
Jan 17 '07 #4
Thanks for your help...unfortunately, obtaining the number of
city/state combinations from the previous page will not work. Perhaps
there's a way to "grow" the form via javascript that might work
better... I'll let you know if I find anything of value.

Jan 17 '07 #5

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

Similar topics

1
by: Ralph Freshour | last post by:
I have two dropdown list: frm_cbo_top frm_cbo_bottom I have an OnChange="submit_form();" in each of these dropdown lists - the javascript does a form.submit() call
6
by: James Baker | last post by:
I have a form that contains another form. For some reason the "nested" form won't submit via document.FormName.submit(); it says "object is null or not a value" or something like that. When I...
1
by: mavrick101 | last post by:
I have a form that I want to submit to a different page and in a new window. I have done this using an HTML button and putting a target="_blank" attribute in the FORM tag. This does give me the...
2
by: Greg T | last post by:
Hi, I have a rather long form that I don't want people submitting unless they are absolutely sure they are ready. I figured the easiest way to prevent an accidental form submission by way of...
5
by: Don | last post by:
I have a need to submit a form, but don't need the user to click on a button. How do I do this? Is there some way, using JavaScript, to setup a <form> tag to do this? Thanks, Don ----==...
0
by: roxanaislam | last post by:
Hi: I have a search form in my application which has 4 dropdown list controls, one text box, one "GO" button, one "reset" button. When the "GO" button is clicked the form gets submitted and the...
4
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
4
by: Super Steve | last post by:
I'm trying to write a little script that disables the submit button on a form, changes the text of the button, and then submits the form. Can anyone tell me why this works ok: <input...
16
by: browntown | last post by:
so I have this application I'm nearly finished with. The only thing the client has requested is the ability to submit the form by pressing "enter". I didn't think this would be a huge pain in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.