Home > Archive > PHP SQL > January 2005 > Submitting Multiple Form fields in an array / loop
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Submitting Multiple Form fields in an array / loop
|
|
| Craig Keightley 2005-01-14, 3:58 pm |
| 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
| |
| R. Rajesh Jeba Anbiah 2005-01-14, 3:58 pm |
| 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/
| |
| Roy W. Andersen 2005-01-14, 3:58 pm |
| 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
| |
| Craig Keightley 2005-01-17, 8:56 am |
| Brilliant thats exactly what i need
thanks for the advice
craig
"Roy W. Andersen" <roy-news@netgoth.org> wrote in message
news:34qeefF4ek5t9U1@individual.net...
> Craig Keightley wrote:
>
> 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
| |
| Markus Ernst 2005-01-17, 3:57 pm |
| Craig Keightley wrote:
[...]
> <input name="approve[]" type="checkbox" id="approve[]" value="<?php
> echo $row_rs['ID']; ?>">
[...]
As your actual question is already answered, just a point to an HTML issue:
other than the name attribute, the id attribute cannot contain an array. So
name="approve[]" is correct, while id="approve[]" is wrong. Also, for some
elements such as radio buttons, there might be several elements with the
same name, but if they have an id attribute, these must have different
values.
If you need to address the element with DOM, you have to change the id to a
unique value. If not, there is no need for the id attribute at all.
HTH
Markus
|
|
|
|
|