473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ 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">optio n 1</option>
<option value="2">optio n 2</option>
<option value="3">optio n 3</option>
</select>

<?php } while ($row_rs = mysql_fetch_ass oc($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($approv e 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','$selec t')";

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 10037
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">optio n 1</option>
<option value="2">optio n 2</option>
<option value="3">optio n 3</option>
</select>

<?php } while ($row_rs = mysql_fetch_ass oc($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($approv e 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','$selec t')";

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','$selec t')";


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_global s 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******@netgo th.org> wrote in message
news:34******** *****@individua l.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','$selec t')";


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_global s 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
8548
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 ($to,$subject,$message); where message is made up from the multiple fields from the form e.g $message = "Name: ".$strName."\r\n"; "Company: ".$strCompany."\r\n";
4
2043
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 users can log problems, comments & suggestions about the site to a DB. I'm building a page that lists the outstanding entries, and allows use to mark any number of entries as being 'done' That is, we have a number of rows, with the last field in...
5
1901
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 ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
4
2287
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, submit the form, and then have a javascript function that checks to see if data is entered, and if not, have an alert window tell the user which field to enter data into and then revert back to that same page. If all data is entered, it would...
8
5723
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 ) ************************************************************************************** <form action="https://www.cards2do.co.uk/addcard.php" method="post"
7
3801
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 database search that I am unable to figure out how to access. (The search is implemented in Php/MySQL.) The user enters search values for: name, address1, city, .... etc., ..... and for each of these they also select whether the search should...
2
2387
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 I am stumped as to how to validate them as a group (as one final validation). I need to check to see if all (at one time) are filled or empty. The other individual validations handle cases where the text boxes are filled with letters or ...
26
3884
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 submitted, then echoes a message. If there were problems with the submitted data the message says: "a problem occurred with your data. Click here to come back to the form" (page_A.php). When the user comes back, he finds the fields white.
1
3397
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 <selectwhich allows multiple selection. When I receive the form values in my procedure I getting only the first value of those that are selected. For example if I select 1000, 1100, 1200, 1300 in the list I only get 1000 in my procedure. If I...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2612
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 we have to send another system
1
1796
muto222
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.