Sorry to be so long-winded here, especially for a first post, but I've been banging my head against this problem for days...
I'm trying to send and receive data interactively to an html form and am having trouble processing the returned data. The idea is as follows...
Retrieve records from a SQL DB and populate an array to send four of the fields from each record to an html table.
As each row of the html table is built (using a loop) it includes a checkbox field (used temporarily, and not for return to the SQL D
so the user can select one or more of the displayed records.
My problem is getting the record ids of the selected rows (checked boxes) back to my php processor so I can use the selections to populate a new array. I can't seem to grab and use any of the data from the checked rows. In the end, I need the record ID and road name fields for later use.
So, my question is... how do I take the data I need from the checked rows, return it to the php processor, and put it in a new array that I will later use to present a list of selections to the user, on the same htm form.
I should point out that I have adapted the code I'm using from another php/html form. I was able to adapt it to display my data correctly, but I can't seem to get the new code right for the next steps.
Also, the application looks at a DB of roads, showing the road name, length, location and a link to a Google map of the road.
Anyway... hope this is making sense. :rolleyes:
Here is the php code, with comments to explain my thinking... (below that is the html code):
<?php
$selections=array();
// Above is one of several attempts to create an array up front to contain the returned POST data,
// but I get an "undefined variable" error when I try to check its contents later
do
{ $i++;
if (( isset($_GET[$i]) || isset($_POST[$i]))) {
$checked='1'; // conditional to see if any checkboxes were selected - seems to work ok
}
}
while ($i<=50);
// set to 50 because i don't know how to set the number to equal the number of rows pulled from the SQL DB
// and my table has less than 50 records :-/
$selecttotal=sizeof($selections);
// test to see whether, after this page is loaded again, my selections array has entries - so far, it's returning 0
/*****
I tried a number of variations, after the $checked='1' conditional above, to populate an array with data from the selected rows.
I figured the looping would be able to add an array element for each road name associated with a check
box in the htm form.
E.g., I tried
selections['rd_name'] = 'roadrow.ROADNAME'
OR
'R_NAME' => $rd_name
*****/
//
// Next four lines are related to CPG_NUKE module this is part of
//
$pagetitle .= 'TestRoads';
define('HEADER_INC', TRUE);
require_once("header.php");
OpenTable();
// Title headings for html table
$cpgtpl->assign_vars(array(
'L_NAME' => $roadlang['Road'],
'L_GOOGLE' => $roadlang['Map link'],
'L_LEN' => $roadlang['Miles'],
'L_CNTY' => $roadlang['County'],
'S_MODULENAME' => getlink('Module_Name'),
'G_CHECK' => 'Checked is set to ', // used to test results, shown on htm page
'G_FIVER' => $selecttotal, // used to test results, shown on htm page
'S_MODE_ACTION' => $home ? '' : getlink('Roads')
));
$sql = "SELECT rd_id, rd_name, map_link, rd_length, rd_county
FROM cms_groads
WHERE rd_id > 0
ORDER BY $order_by";
$result = $db->sql_query($sql);
if ($row = $db->sql_fetchrow($result)) {
$i = 0;
do {
$rd_id = $row['rd_id'];
$rd_name = $row['rd_name'];
$rd_length = $row['rd_length'];
$rd_county = $row['rd_county'];
$temp_url = $row['map_link'];
$map_link = '<a href="' . $temp_url . '">Click for Map</a>';
$cpgtpl->assign_block_vars('roadrow', array( //was 'memberrow'
'ROW_NUMBER' => $i + 1,
'RDID' => $rd_id,
'ROADNAME' => $rd_name,
'MAP' => $map_link,
'LENGTH' => $rd_length,
'COUNTY' => $rd_county)
);
$i++;
}
while ( $row = $db->sql_fetchrow($result) );
$db->sql_freeresult($result);
}
$cpgtpl->set_filenames(array('mymod' => 'roads.html'));
$cpgtpl->display('mymod');
CloseTable();
Following is the html with the post action. The table correctly displays all the selected rows from my SQL table and seems to capture whether a checkbox is checked. After post, it re-executes the php code. I haven't built in actions for the returned data, except to display the items captured in the new "selections" array at the bottom of the main htm table.
<form method="post" action="{S_MODE_ACTION}" {I18N}>
<table width="100%" cellpadding="3" cellspacing="1" border="0" class="forumline">
<tr>
<th height="25" class="thCornerL" nowrap="nowrap">Route</th>
<th class="thTop" nowrap="nowrap">{L_NAME}</th>
<th class="thTop" nowrap="nowrap">{L_GOOGLE}</th>
<th class="thTop" nowrap="nowrap">{L_LEN}</th>
<th class="thTop" nowrap="nowrap">{L_CNTY}</th>
</tr>
<!-- BEGIN roadrow -->
<tr>
<td class="{roadrow.ROW_CLASS}" align="center"><input type="checkbox" name=selections[] value="{roadrow.ROADNAME}" /></td>
<td class="{roadrow.ROW_CLASS}" align="center"> {roadrow.ROADNAME} </td>
<td class="{roadrow.ROW_CLASS}" align="center"> {roadrow.MAP} </td>
<td class="{roadrow.ROW_CLASS}" align="center" valign="middle"> {roadrow.LENGTH} </td>
<td class="{roadrow.ROW_CLASS}" align="center" valign="middle"><span class="gen">{roadrow.COUNTY}</span></td>
</tr>
<!-- END roadrow -->
<tr>
<td class="catbottom" colspan="8" height="28"> </td>
</tr>
<tr>
<td><input type="submit" name="build" value="build" class="liteoption" /></td>
</tr>
<tr>
<td>{G_CHECK}{G_FIVER}</td>
</tr>
<!-- BEGIN selections -->
<tr>
<td> {selections} </td>
</tr>
<!-- END selections -->
</table>
</form>