473,471 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Submitting Multiple Form fields in an array / loop

I have the following form

<FORM NAME="form1" METHOD="POST">
<?php do { ?>
<input name="approve[]" type="checkbox" id="approve[]" value="<?php echo
$row_rs['ID']; ?>">
<select name="select">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>

<?php } while ($row_rs = mysql_fetch_assoc($rs)); ?>
</FORM>
I want to acheive a loop that inserts a record into a database that enters
the id and the value they have selected (if the approve input box was
ticked)

so far I have this:
<?
foreach($approve as $aID) {
$insertSQL = "INSERT INTO TABLE('refID','optionSelected') VALUES
('$aID','')";


}

?>

How do I enter the select value within this loop?
I assumed that it would be just
$insertSQL = "INSERT INTO TABLE('refID','optionSelected') VALUES
('$aID','$select')";

But how does the form know what select item is associated with what ID

Hope this makes sense

any help would be grateful

Craig
Jul 17 '05 #1
3 10026
Craig Keightley wrote:
I have the following form

<FORM NAME="form1" METHOD="POST">
<?php do { ?>
<input name="approve[]" type="checkbox" id="approve[]" value="<?php echo $row_rs['ID']; ?>">
<select name="select">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>

<?php } while ($row_rs = mysql_fetch_assoc($rs)); ?>
</FORM>
I want to acheive a loop that inserts a record into a database that enters the id and the value they have selected (if the approve input box was ticked)

so far I have this:
<?
foreach($approve as $aID) {
$insertSQL = "INSERT INTO TABLE('refID','optionSelected') VALUES
('$aID','')";
}

?>

How do I enter the select value within this loop?
I assumed that it would be just
$insertSQL = "INSERT INTO TABLE('refID','optionSelected') VALUES
('$aID','$select')";

But how does the form know what select item is associated with what

ID

When you render the HTML, try to make the fields associated with
the IDs like <select name="foo[1]"> where 1 is the ID. Similarly for
checkbox, etc. So, you make the fields associated with the ID. Then do
a print_r($_POST) and see, how these values are submitted. You can then
write the code to extract the values possibly using foreach().

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #2
Craig Keightley wrote:
How do I enter the select value within this loop?
I assumed that it would be just
$insertSQL = "INSERT INTO TABLE('refID','optionSelected') VALUES
('$aID','$select')";


Not sure if this is what you're asking, but it sounds to me like you
want a checkbox associated with a select-list, correct?

In that case, give them names that you can use to match them to
eachother. What I'd do is give the checkboxes names like checkbox[1] and
the select-lists select[1], and checkbox[2] and select[2] and so on.

Then, when the form is submitted, all checked checkboxes will be
received as an array where the keys are the same as the array with the
select-lists, so you can just go through all the select-lists as so:

foreach ($_POST['select'] as $key=>$value) {
if ($_POST['checkbox'][$key]) {
do_sql_stuff;
}
}

This will only do the SQL stuff if the checkbox with the same number as
the current select-field was checked (an unckecked box doesn't get sent
when you submit the form, so if a box is unchecked, the value of
$_POST['checkbox'][number] is false).

To see what actually gets sent, do a print_r($_POST) and examine the output.

Also, you should grab the variables using $_POST['varname'] instead of
just $varname, as that'll make your life much easier the day you realize
having register_globals off is a good thing ;)
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #3
Brilliant thats exactly what i need
thanks for the advice

craig
"Roy W. Andersen" <ro******@netgoth.org> wrote in message
news:34*************@individual.net...
Craig Keightley wrote:
How do I enter the select value within this loop?
I assumed that it would be just
$insertSQL = "INSERT INTO TABLE('refID','optionSelected') VALUES
('$aID','$select')";


Not sure if this is what you're asking, but it sounds to me like you want
a checkbox associated with a select-list, correct?

In that case, give them names that you can use to match them to eachother.
What I'd do is give the checkboxes names like checkbox[1] and the
select-lists select[1], and checkbox[2] and select[2] and so on.

Then, when the form is submitted, all checked checkboxes will be received
as an array where the keys are the same as the array with the
select-lists, so you can just go through all the select-lists as so:

foreach ($_POST['select'] as $key=>$value) {
if ($_POST['checkbox'][$key]) {
do_sql_stuff;
}
}

This will only do the SQL stuff if the checkbox with the same number as
the current select-field was checked (an unckecked box doesn't get sent
when you submit the form, so if a box is unchecked, the value of
$_POST['checkbox'][number] is false).

To see what actually gets sent, do a print_r($_POST) and examine the
output.

Also, you should grab the variables using $_POST['varname'] instead of
just $varname, as that'll make your life much easier the day you realize
having register_globals off is a good thing ;)
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama

Jul 17 '05 #4

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

Similar topics

4
by: SJG | last post by:
I'm having some problems with a php mail form where I am wanting to send information from 10 form fields in a e-mail. the way I have it structured for the mail command is mail...
4
by: CJM | last post by:
I have a problem more to do with style than ability; I can solve this problem in a number of ways, but I'm keen to find the best way of doing this... We have a facility on our intranet where...
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 ----==...
4
by: Rick | last post by:
Hello, I'm having trouble with submitting my form when checking to see if data is present in the user-inputted fields. What I want to happen is for the user to input various pieces of data,...
8
by: Adam | last post by:
Hey, I'm using JS to submit a form with image submit buttons, using the following code... (Page is here... http://www.cards2do.co.uk/addcard.php?card_id=292 ) ...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
2
by: Neo Geshel | last post by:
Greetings, I have a form with a telephone field. It is very specific, as it has four text boxes - the country code, area code, prefix and suffix. I can validate each of them individually, but...
26
by: pepper.gabriela | last post by:
Hello, a stupid question but... page_A.php is a page with a form. The user inserts text in four fields, then he clicks a submit button. The data goes to page_B.php: this page controls the data...
1
by: Rick Owen | last post by:
Greetings, I have a form that, when submitted, calls a plsql procedure. The form has a number of fields (text, hidden, select, radio) but the particular field that is giving me problems is a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...
1
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
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...
1
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.