Help on recordset and checkboxes

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
damon2003
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2003 2:37 pm

Help on recordset and checkboxes

Post by damon2003 »

Hi,
I have the following recordset that outputs a list of values from a recordset long with a checkbox next to each one as follows:

fieldaValue1 fieldbValue1 checkbox1
fieldaValue2 fieldbValue2 checkbox2
and so on....

the code I have is this:
?php echo $row_Recordset1['PDF_KEY_PRODUCT'];
$i = 1 + $varincrement;
$varincrement = $i++;
?>
</td>
<td width="150"><?php echo $row_Recordset1['PDF_LONG_DESCRIPTION']; ?></td>
<td width="50">&nbsp;</td>
<td width="50"><?php echo $row_Recordset1['PDF_SELLING_PRICE']; ?></td>
<td width="50"><input name="textfield" type="text" size="5"></td>
<td width="50"><?php echo "<input type=\"checkbox\" name=\"checkbox$varincrement\" value=\"checkbox\"></td>";?>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

I use an increment operator to produce new names for each checkbox in the recordset,
My problem is this: I want to let the user click on different checkboxes and then submit only the values rows where a checkbox is checked to another script,
does anyone have any ides how to do this??
thanks a lot
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

... and then submit only the values rows where a checkbox is checked
I don't think that is possible. Let hear a javascript guru comment on this (onclick/onsubmit issue?).

What exactly do you mean by 'rows'?

Code: Select all

<!-- result:
Array
(
    [checkbox1] => 1
    [checkbox3] => 3
    [checkbox5] => 5
    [checkbox7] => 7
    [checkbox9] => 9
    [checkbox11] => 11
    [checkbox13] => 13
    [checkbox15] => 15
    [checkbox17] => 17
    [checkbox19] => 19
)
-->
<pre>
<form method="post">
<?php
for ($i=1;$i<=20;$i++) {
    $status = ($i % 2 ? ' checked' : '');
    echo '<input type="checkbox" name="checkbox'.$i.'" value="'.$i.'"'.$status.' />';
}
?>
<input type="submit" />
</form>
<?php
    print_r($_POST);
?>
</pre>
damon2003
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2003 2:37 pm

Hi

Post by damon2003 »

thanks for reply
by rows, I mean each row in database, i.e. fieldaValue1 fieldbValue2 but I may be thinking about that the wrong way.
Anyway I have this code from another forum, but dont fully understand it:
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><input type="text" name="input_one">
<input type="checkbox" name="check_one"><br>
<input type="text" name="input_two">
<input type="checkbox" name="check_two"><br>
<input type="submit" name="submit" value="Go"></p>
</form>

<?PHP
if (isset($_POST['submit'])) {
$matches = array();
foreach ($_POST as $name => $value) {
if (preg_match("/^check_(w+)/", $name, $match)) {
$matches[] = "input_".$match[1];
}
}
foreach ($matches as $field) {
echo $_POST[$field]."<br>";
}
}
?>

looks like using the preg_match to check if a check box is checked but dont get this.
Whats oes your code do? It uses javascript to put values into arrays then sends to php?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Not sure if I've got my head round you're full procedure here but, as far as submitting ONLY the checked values, that's the normal behaviour.

If you have checkboxes with html input field name attributes like:

name="options[1]"
name="options[2]"
name="options[3]"

..on submission of the form, $_POST['options'] will be an array containing just the checked boxes.

If none were checked $_POST['options'] will not be set.
damon2003
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2003 2:37 pm

Hi

Post by damon2003 »

OK,
only the checked boxes will be submitted,
what about the values from the database in the crresponding row to the checked? Can you tell me how exactly to access these values from the $_POST array?
thanks
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I need to understand better what you want to achieve.

There seems to be a checkbox corrsponding to each text input field. What is the purpose of the checkboxes?
damon2003
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2003 2:37 pm

Hi

Post by damon2003 »

OK ignore that code with the two textfields

I have a recordset that outputs three columns and then a checkbox. I know how to dynamically rename the checkbox name and value if that is necessary.
When the user presses submit I only want to submit the rows of collumn data where the user has clicked on the coressponding checkbox. Now I know that only values from checked boxes will submit, but as it is none of the column data will submit because it is not in a hidden field or any other form component. I want to know the best way to submit this data. Either to submit only the checked data, or submit everything and then somehow compare these values to the submitted checkbox value so that I end up with only checked rows.
I have thought abut hidden field. Also I thought about concatenating te three column values and setting the value of the checkbox to this concatenated value,
any ideas?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Still not quite sure if I get it. Is this a form you use to edit data stored in a db?

If so, you don't need checkboxes at all.

(1) Set a value="<?php echo $field_value; ?>" attribute in the form input fields (you've got name and type but no preset values).

(2) When the form is submitted, update the db with the values returned in the POST array.

I'm sure there is a simple solution to this once I understand exactly what you're doing.
damon2003
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2003 2:37 pm

Hi

Post by damon2003 »

Actually checkboxes are necessary bacause that is part of the spec. Would the methods I outlined above work?
Post Reply