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

Submitting Multiple Form fields in an array / loop

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.