Problem selecting record from dynamically generated html table

I want to generate a custom report from a database on veterans, based on a “last name” a user enters on a web form. If the last name is unique in the database, then all works well and the report is generated. If there are several last names the same (Smith, for instance) then a table is created listing all the "smith"s and I’m using a radio button on each record to allow the user to select the one they want. There is a submit button included to trigger the action. I can’t get this to work and was wondering if there is a more elegant way to handle this type of task. Sorry that this is so general, but I know that I’m far from the first to attempt this, and would like some guidance. Thanks.

Jim

In what way is it not working? I’m sure there are more javascript-y ways to make it more elegant, but you’re doing what I’d do - if there’s more than one result, display a list of the results including enough information for the user to choose the correct one, and the row id so you can get the correct one. I’d probably draw a table and either have an href link or a mini-form in the table rather than a radio button, but your way should work too. Set the value to be the id of the row you want to retrieve, submit the form, job done.

So, what isn’t working for you?

Code below is executed when multiple records are retrieved from the SQL database (Ie: last name = “smith”). The web page is generated correctly, but when a radio button is selected from a record in the table and the submit button “Select Checked” is clicked, nothing happens.

(html ----)

<?php
//include the header code from the web root
 include $_SERVER['DOCUMENT_ROOT'] . '/cforce/includes/header.inc.php';
//set up headings
echo "<h1>Search Results 'C' Force Members: $indivrpt ...</h1>";

echo "<p>Your search returned: $num results.</p>";
echo "<p>To display a report on an individual, select the last name</p>";


//set column headings
?>
<table class="tabledata90">
<tr>
<th>Select</th>
<th>Regimental Number</th>
<th>Rank</th>
<th>Last Name</th>
<th>First Name</th>
<th>Regiment</th>
<th>Platoon</th>
<th>Company</th>
<th>Date of Death</th>
</tr>
<?php
 if(isset($_POST['selectmember_submit'])){
      if(isset($_POST['selectmember'])){
   		$selectmember = $_POST['selectmember'];
        if(isset($selectmember[$i])){
     echo "I have selected $selectmember[$i]";
     }
     }
     }

foreach ($memlists as $memlist):
//var_dump($memlists);
 ?>
	<tr>
	<td><input type='radio' value='".$memlist."' name='selectmember[]' /></td>
	<td><?php htmlout($memlist['regtno']);?></td>
	<td><?php htmlout($memlist['rank']);?></td>
	<td><?php htmlout($memlist['lastname']);?></td>
	<td><?php htmlout($memlist['firstname']);?></td>
	<td><?php htmlout($memlist['regt']);?></td>
	<td><?php htmlout($memlist['platoon']);?></td>
	<td><?php htmlout($memlist['coy']);?></td>
	<td><?php htmlout($memlist['dod']);?></td>
	</tr>
<?php endforeach;
// We're out of the loop, let's add a submit button and close our table
echo "<tr><td colspan=9><input type='submit' value='Select Checked' style='float: right;' name='selectmember_submit' /></td></tr></table>";
//include the footer from the web root
include $_SERVER['DOCUMENT_ROOT'] . '/cforce/includes/footer.inc.php';
echo "</body></html>";
?>

There are a few things, no form tags, radio button as array, php variable in html etc. What you really need to do is post the array key. See how you do with this.

<?php
// Assuming $memlists array is built above

//echo "<pre>";
//print_r($memlists);
//echo "</pre>";

if(isset($_POST['selectmember_submit'])){
	if(isset($_POST['selectmember'])){
		$i = $_POST['selectmember'];
		if(array_key_exists($i,$memlists)){
			echo "I have selected: {$memlists[$i]['firstname']} {$memlists[$i]['lastname']}";
		}
	}
}
?>


<form action="" method="post">
	<table class="tabledata90">
		<tr>
			<th>Select</th>
			<th>Regimental Number</th>
			<th>Rank</th>
			<th>Last Name</th>
			<th>First Name</th>
			<th>Regiment</th>
			<th>Platoon</th>
			<th>Company</th>
			<th>Date of Death</th>
		</tr>
		<?php
		foreach ($memlists as $key => $memlist):
		?>
			<tr>
				<td><input type='radio' value='<?php htmlout($key);?>' name='selectmember' /></td>
				<td><?php htmlout($memlist['regtno']);?></td>
				<td><?php htmlout($memlist['rank']);?></td>
				<td><?php htmlout($memlist['lastname']);?></td>
				<td><?php htmlout($memlist['firstname']);?></td>
				<td><?php htmlout($memlist['regt']);?></td>
				<td><?php htmlout($memlist['platoon']);?></td>
				<td><?php htmlout($memlist['coy']);?></td>
				<td><?php htmlout($memlist['dod']);?></td>
			</tr>
		<?php endforeach;
		?>
		<!-- We're out of the loop, let's add a submit button and close our table -->
		<tr>
			<td colspan=9><input type='submit' value='Select Checked' style='float: right;' name='selectmember_submit' /></td>
		</tr>
	</table>
</form>

Thanks for your help Drummin. I’m using the code with your suggested changes and I’ve made some progress. Looks like I now have a control structure issue. The file above is called from the main file index.php via an include statement. It is only called when there are multiple last names found that are the same, ie: “smith”. So, the first time the file is called by the include statement it performs as expected - a table is displayed showing the smiths in the database, each record has a radio button. However, when the radio button is selected and the submit button clicked the whole database is then displayed in the table layout, rather than a single record. Any ideas?

Jim

Do you mean something like this?

<?php
// Assuming $memlists array is built above

//echo "<pre>";
//print_r($memlists);
//echo "</pre>";

if(isset($_POST['selectmember_submit'])){
	if(isset($_POST['selectmember'])){
		$i = $_POST['selectmember'];
		if(array_key_exists($i,$memlists)){
			//echo "I have selected: {$memlists[$i]['firstname']} {$memlists[$i]['lastname']}";
		}
	}
}
?>


<form action="" method="post">
	<table class="tabledata90">
		<tr>
			<th>Select</th>
			<th>Regimental Number</th>
			<th>Rank</th>
			<th>Last Name</th>
			<th>First Name</th>
			<th>Regiment</th>
			<th>Platoon</th>
			<th>Company</th>
			<th>Date of Death</th>
		</tr>
		<?php
		
		if(isset($_POST['selectmember_submit']) && isset($_POST['selectmember']) && array_key_exists($_POST['selectmember'],$memlists)){
				$i = $_POST['selectmember'];
		?>	
				<tr>
					<td><input type='radio' value='<?php htmlout($key);?>' name='selectmember' /></td>
					<td><?php htmlout($memlists[$i]['regtno']);?></td>
					<td><?php htmlout($memlists[$i]['rank']);?></td>
					<td><?php htmlout($memlists[$i]['lastname']);?></td>
					<td><?php htmlout($memlists[$i]['firstname']);?></td>
					<td><?php htmlout($memlists[$i]['regt']);?></td>
					<td><?php htmlout($memlists[$i]['platoon']);?></td>
					<td><?php htmlout($memlists[$i]['coy']);?></td>
					<td><?php htmlout($memlists[$i]['dod']);?></td>
				</tr>	
		<?php	
		}else{		
		
			foreach ($memlists as $key => $memlist):
			?>
				<tr>
					<td><input type='radio' value='<?php htmlout($key);?>' name='selectmember' /></td>
					<td><?php htmlout($memlist['regtno']);?></td>
					<td><?php htmlout($memlist['rank']);?></td>
					<td><?php htmlout($memlist['lastname']);?></td>
					<td><?php htmlout($memlist['firstname']);?></td>
					<td><?php htmlout($memlist['regt']);?></td>
					<td><?php htmlout($memlist['platoon']);?></td>
					<td><?php htmlout($memlist['coy']);?></td>
					<td><?php htmlout($memlist['dod']);?></td>
				</tr>
			<?php endforeach;
		}
		?>
		<!-- We're out of the loop, let's add a submit button and close our table -->
		<tr>
			<td colspan=9><input type='submit' value='Select Checked' style='float: right;' name='selectmember_submit' /></td>
		</tr>
	</table>
</form>

OR do you mean show record without the form?

<?php
// Assuming $memlists array is built above

//echo "<pre>";
//print_r($memlists);
//echo "</pre>";

if(isset($_POST['selectmember_submit']) && isset($_POST['selectmember']) && array_key_exists($_POST['selectmember'],$memlists)){
		$i = $_POST['selectmember']; 
?>	
	<a href="?">Show all records</a><!-- Optional -->
	<table class="tabledata90">
		<tr>
			<th>Regimental Number</th>
			<th>Rank</th>
			<th>Last Name</th>
			<th>First Name</th>
			<th>Regiment</th>
			<th>Platoon</th>
			<th>Company</th>
			<th>Date of Death</th>
		</tr>
		<tr>
			<td><?php htmlout($memlists[$i]['regtno']);?></td>
			<td><?php htmlout($memlists[$i]['rank']);?></td>
			<td><?php htmlout($memlists[$i]['lastname']);?></td>
			<td><?php htmlout($memlists[$i]['firstname']);?></td>
			<td><?php htmlout($memlists[$i]['regt']);?></td>
			<td><?php htmlout($memlists[$i]['platoon']);?></td>
			<td><?php htmlout($memlists[$i]['coy']);?></td>
			<td><?php htmlout($memlists[$i]['dod']);?></td>
		</tr>
	</table>		
<?php	
}else{	
?>	
<form action="" method="post">
	<table class="tabledata90">
		<tr>
			<th>Select</th>
			<th>Regimental Number</th>
			<th>Rank</th>
			<th>Last Name</th>
			<th>First Name</th>
			<th>Regiment</th>
			<th>Platoon</th>
			<th>Company</th>
			<th>Date of Death</th>
		</tr>
		<?php
		foreach ($memlists as $key => $memlist):
		?>
			<tr>
				<td><input type='radio' value='<?php htmlout($key);?>' name='selectmember' /></td>
				<td><?php htmlout($memlist['regtno']);?></td>
				<td><?php htmlout($memlist['rank']);?></td>
				<td><?php htmlout($memlist['lastname']);?></td>
				<td><?php htmlout($memlist['firstname']);?></td>
				<td><?php htmlout($memlist['regt']);?></td>
				<td><?php htmlout($memlist['platoon']);?></td>
				<td><?php htmlout($memlist['coy']);?></td>
				<td><?php htmlout($memlist['dod']);?></td>
			</tr>
		<?php
		endforeach;
		?>
		<!-- We're out of the loop, let's add a submit button and close our table --> 
		<tr>
			<td colspan=9><input type='submit' value='Select Checked' style='float: right;' name='selectmember_submit' /></td>
		</tr>
	</table>
</form>
<?php
} 
?>

Thanks for your patience Drummin. Based on your feedback and some study I now have the following code. Please note that if the user has previously selected a record, then the report gets generated automatically. I still have the problem that when I select the record and press submit, the whole database is accessed. Let me explain from the beginning: Let’s assume that the user accessed a form on another web page and typed in the name that they want a report on. Let’s assume it is “smith”. This text is passed as the variable $indivrpt. A PHP (index.php) file then runs, accessing the database and generates an array of records containing last name = smith (. Control is then passed to the included file (the one below). Because there are several smiths, their information must be displayed so the user can choose the exact smith they want to have the report on.

A symptom of the problem is that when the smiths are displayed in the table and I select one (say the third one in the list), the report generated does not use that selection, and instead displays all database records.

I need a way to stop accessing the calling file (index.php) a second time

I hope that you have some ideas.

Thanks,.

PS: I tried your latest code, but the problem is exhibited there as well.

<?php

// Assuming $memlists array is built above

?>

<form action="" method="post">

<?php
//if first time through allow selection of record from table; if record already selected (2nd time through), include the report

        if(isset($_POST['selectmember_submit']) && isset($_POST['selectmember']) && array_key_exists($_POST['selectmember'],$memlists)){
                $i = $_POST['selectmember'];
         include 'indivdetailed.html.php';

        }else{
             //set up headings
             //include the header code from the web root
			 include $_SERVER['DOCUMENT_ROOT'] . '/cforce/includes/header.inc.php';

			echo "<h1>Search Results 'C' Force Members: $indivrpt ...</h1>";

			echo "<p>Your search returned: $num results.</p>";
			echo "<p>To display a report on an individual, select using the radio button, then click box</p>";
			?>
			<table class="tabledata90">
			<tr>
            <th>Select</th>
            <th>Regimental Number</th>
            <th>Rank</th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Regiment</th>
            <th>Platoon</th>
            <th>Company</th>
            <th>Date of Death</th>
        </tr>
        <?php
            foreach ($memlists as $key => $memlist):
            ?>

                <tr>
                    <td><input type='radio' value='<?php htmlout($key);?>' name='selectmember' /></td>
                    <td><?php htmlout($memlist['regtno']);?></td>
                    <td><?php htmlout($memlist['rank']);?></td>
                    <td><?php htmlout($memlist['lastname']);?></td>
                    <td><?php htmlout($memlist['firstname']);?></td>
                    <td><?php htmlout($memlist['regt']);?></td>
                    <td><?php htmlout($memlist['platoon']);?></td>
                    <td><?php htmlout($memlist['coy']);?></td>
                    <td><?php htmlout($memlist['dod']);?></td>
                </tr>
            <?php endforeach;
        }
        ?>
        <!-- We're out of the loop, let's add a submit button and close our table -->
        <tr>
            <td colspan=9><input type='submit' value='Select Checked' style='float: right;' name='selectmember_submit' /></td>
        </tr>
    </table>
</form>

Well it’s a little unclear what these include files are doing. But based on what I understand, it would be best to pass the database record number, which I assume is regtno.

		<?php
		foreach ($memlists as $memlist):
		?>
			<tr>
				<td><input type='radio' value='<?php htmlout($memlist['regtno']);?>' name='selectmember' /></td>
				<td><?php htmlout($memlist['regtno']);?></td>
				<td><?php htmlout($memlist['rank']);?></td>
				<td><?php htmlout($memlist['lastname']);?></td>
				<td><?php htmlout($memlist['firstname']);?></td>
				<td><?php htmlout($memlist['regt']);?></td>
				<td><?php htmlout($memlist['platoon']);?></td>
				<td><?php htmlout($memlist['coy']);?></td>
				<td><?php htmlout($memlist['dod']);?></td>
			</tr>
		<?php
		endforeach;
		?>

The IF ELSE statement then should be changed, removing the array_key_exists() section and just looking for the first two POST keys you have.

if(isset($_POST['selectmember_submit']) && isset($_POST['selectmember'])){

Then query the database for that single record based on $_POST[‘selectmember’] which should be regtno.