473,507 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2609

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
1765
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
1348
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
1407
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
2210
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
1884
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
1643
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
3859
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
2783
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
2836
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
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
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...
1
7034
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...
0
7488
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
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...
0
4702
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1544
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 ...

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.