Accessing form fields for update [not solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Accessing form fields for update [not solved]

Post by Stryks »

Hi all,

I have a form whci I want to use to update information about set of rows. Initially I needed a checkbox to select which rows would be affected, which I have done by setting the checkboes up like this:

Code: Select all

<input type="checkbox" name="isSelectedї]" id="isSelected" value="{inserting row id here}">
The trouble is that there are two other textboxes for each row, so if a checkbox is ticked for a row then the textboxes are updated in the database.

I can find what rews are ticked as I said, by using:

Code: Select all

foreach ($_POST['isSelected'] as $id) {
	//action
}
But how can I access the value of that rows textbox using the $id value if I set the textbox up like:

Code: Select all

<input type="text" name="definitionї]" id="definition" value="All Areas" size="80" maxlength="128">
I also need to do some javascripting on these fields. I am assuming that javascript will automatically make an array of the id's so that I can work with them on a row by row basis. Anyhow, thats another problem, I have to make this work first.

Thanks for any input you can give.
Last edited by Stryks on Tue Dec 21, 2004 10:48 pm, edited 2 times in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I think you mean something like this: (but not sure ;))

Code: Select all

foreach ($_POST['isSelected'] as $key=>$val) {
    //just a test, if the echo is correct then you know how to get at the value
    echo $_POST['definition'][$key].'<br />';
}
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Ahhh .... thats what I was after.

I know I've done it before somewhere but I just couldn't for the life of me remember how it was done.

Cheers 8)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

On finalising the form I am finding that I cant quite make it work the way I had hoped that it would.

I have two fields, ifSelected a checkbox and definition a textbox. They are defined as:

Code: Select all

&lt;input type="checkbox" name="isSelected&#1111;]" id="isSelected" value="1"&gt;
&lt;input type="text" name="definition&#1111;]" id="definition" value="All Areas" size="80" maxlength="128"&gt;
The form displays lets say, three results with the checkbox value being 1, 2 and 3. I hit submit, accessing the data with the following code:

Code: Select all

foreach ($_POST['isSelected'] as $key=>$val) {
    //just a test, if the echo is correct then you know how to get at the value
    echo $_POST['definition'][$key].'<br />';
}
If I have clicked the first textbox, I get the right result. If I click say, the first and the third option however, it brings back the text in the first and the second text box. Alternately, if I click the second or third checkbox, it then brings back the text from the first text box.

Perhaps I should manually key the textboxes with the value of each checkbox so that the control arrays match. It just seems to me that the number of elements in the checkbox array depends on how many were checked, while the elements in the textbox array depend on how many iterations of it there were. Therefore the keys do not match between the two.

Am I way off base here? I'm thinking something along the lines of the following:

Code: Select all

&lt;input type="checkbox" name="isSelected&#1111;]" id="isSelected" value="345"&gt;
&lt;input type="text" name="definition&#1111;345]" id="definition" value="All Areas" size="80" maxlength="128"&gt;
Will this work? I'm kinda scared of breaking what is currently working now by altering it with an unknown.

Thanks.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

thats exactly what you should do, assign keys to them

unless im missing something, i think the solution is to just add matching keys to both the text field and the checkbox.

Code: Select all

<input type="checkbox" name="isSelected&#1111;345]" id="isSelected">
<input type="text" name="definition&#1111;345]" id="definition" value="All Areas" size="80" maxlength="128">
you could prob also do something like this

Code: Select all

<input type="checkbox" name="id&#1111;345]&#1111;selected]" id="isSelected">
<input type="text" name="id&#1111;345]&#1111;definition]" id="definition" value="All Areas" size="80" maxlength="128">
by adding keys, php will not reindex the array.

so if the browser only submits parts 1 and 3, you will still have indices 1 and 3 in your array, instead of 0 and 1. they will match up still because you litterally declared thier indices. only if you dont declare the indices will php 0 index the array for you.

hope that makes sense.
Post Reply