By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

form textbox populated by clicking on a list of data

P: 40
I have a php page that lists the results of a search. I need the same page to be a form that the user can choose one of the rows of data listed to edit. Right now, due to my challenged newbee brain, I just have a form text box where the user would type in the key filed from the rows listed - but I'm sure there is a better way to simplify this for the user.

Is it possible to make the key fields in the listed rows hyperlinks that, if clicked on, would populate the key text box in the form for submission? Or is there a better approach to take?

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. $db = mysql_connect("localhost", "root", "yeahright"); mysql_select_db("etest",$db);
  2. $result = mysql_query("SELECT * FROM incidents WHERE rm_date = '1900-01-01' ORDER BY incident_id",$db);
  3.    echo "<table><tr>";
  4.     echo "<td><b>Incident</b></td>";
  5.     echo "<td><b>&nbsp Facility</b></td>";
  6.     echo "<td><b>&nbsp Location</b></td>";
  7.     echo "<td><b>&nbsp Person</b></td>";
  8.     echo "<td><b>&nbsp Injury</b></td>";
  9.     echo "<td><b>&nbsp Severity</b></td>";
  10.     echo "<td><b>&nbsp Incident Date</b></td>";
  11.  
  12.     while ($myrow = mysql_fetch_array($result))
  13.     {
  14.        echo "<tr><td>".$myrow["incident_id"]."</td>";
  15.        echo "<td>&nbsp ".$myrow["fac_id"]."</td>";
  16.        echo "<td>&nbsp ".$myrow["room_descr"]."</td>";
  17.        echo "<td>&nbsp ".$myrow["person_type"]."</td>";
  18.        echo "<td>&nbsp ".$myrow["injury"]."</td>";
  19.        echo "<td>&nbsp ".$myrow["severity"]."</td>";
  20.        echo "<td>&nbsp ".$myrow["inc_date"]."</td></tr>";
  21.     }
  22.        echo "</tr></table>";
  23.  
and:

Expand|Select|Wrap|Line Numbers
  1. <FORM METHOD="POST" ACTION="edit_ir.php">
  2. <h3>Enter the Incident ID for the Report You Need to Edit:</h3>
  3. <table border="0" cellpadding="0">
  4. <tr>
  5. <td><INPUT TYPE=TEXT NAME="incident_id" VALUE="" SIZE=6 MAXLENGTH=6></td>
  6. <td><INPUT TYPE=SUBMIT VALUE="Edit"></td>
  7. <td><INPUT TYPE=RESET VALUE="Reset"></td>
  8. </tr>
  9. </table>
  10. </FORM>
  11.  
I'd like to either have the $myrow["incident_id"] be a hyperlink that would fill in the ><INPUT TYPE=TEXT NAME="incident_id" ---- or ---- have the ><INPUT TYPE=TEXT NAME="incident_id" be a Dropdown field with all of the $myrow["incident_id"] fields in the options list.

TIA,

jej1216
May 31 '07 #1
Share this Question
Share on Google+
7 Replies


P: 93
There are several possible solutions....

One is to use a bit of the old javascript, so you could make each link put the incident_id into the form field:

<a href="javascript:document.your_form_name.incident_ id.value="<?php print $myrow["incident_id']; ?>;"><?php print $myrow["incident_id']; ?></a>

(I'm jumping in and out of the <?php ?> tags a lot there, just cos it makes it a bit easier to see what's going on)

You'd need to give your form a name to make that work though:
<FORM METHOD="POST" ACTION="edit_ir.php" NAME="your_form_name">

Or you could go a bit slicker and get it to submit the form as well. I'd use a javascript function to do it...

Put this in the <head> section of your page...

<head>
<script type="javascript">
function choose_incident(chosen_id){
document.your_form_name.incident_id.value=chosen_i d;
document.your_form_name.submit();
}
</head>

And then do each link like this...

<a href="javascript:choose_incident('<?php print $myrow["incident_id']; ?>');"><?php print $myrow["incident_id']; ?></a>

Actually that might be a bit dodgy cos php might not like the <?php tag coming straight after the ' quotation mark (I don't know why but it doesn't seem to like it), so you might need to do:
<?php print "<a href=\"javascript:choose_incident('". $incident_id ."');\">". $incident_id ."</a>"; ?>

I hope that helps.

(Note to forum moderators: when you make the code colouring readable I'll use the code tags!)
May 31 '07 #2

ak1dnar
Expert 100+
P: 1,584
From your first page try like this.

first_page.php
[PHP]echo '<a href="edit_ir.php?id='. $myrow["incident_id"].'">ID 1001</a>';
echo '<a href="edit_ir.php?id='. $myrow["incident_id"].'">ID 1002</a>';
[/PHP]

put them under your <td></td> tags.

then from your edit_ir.php
1. Use the GET method to fetch the id.
2. populate the record related to that id to a form with table values in it.
3. execute the form to edit that records.

edit_ir.php
[PHP]

<?php
$ID = $_GET['id'];
// code for the populate the Record to a form goes here.
?>[/PHP]
Jun 1 '07 #3

P: 40
From your first page try like this.

first_page.php
[PHP]echo '<a href="edit_ir.php?id='. $myrow["incident_id"].'">ID 1001</a>';
echo '<a href="edit_ir.php?id='. $myrow["incident_id"].'">ID 1002</a>';
[/PHP]

put them under your <td></td> tags.

then from your edit_ir.php
1. Use the GET method to fetch the id.
2. populate the record related to that id to a form with table values in it.
3. execute the form to edit that records.

edit_ir.php
[PHP]

<?php
$ID = $_GET['id'];
// code for the populate the Record to a form goes here.
?>[/PHP]


Thanks - I'll try this.

One question, though.

The list for the user to select from will change, so the incident ID will be different. For that reason, in the >ID 1001</a> part of the code above, I can't use the 'ID 1001' - the ID will be different from one time to another. Do I need that piece in the code? That's the text the user will see that is the link, right?

Thanks again,

jej1216
Jun 4 '07 #4

ak1dnar
Expert 100+
P: 1,584
Thanks - I'll try this.

One question, though.

The list for the user to select from will change, so the incident ID will be different. For that reason, in the >ID 1001</a> part of the code above, I can't use the 'ID 1001' - the ID will be different from one time to another. Do I need that piece in the code? That's the text the user will see that is the link, right?

Thanks again,

jej1216
Please Note that what ever the thing i have done in my coding, its only a example.I tried to explain about the magic of $_GET.

And its my mistake i didn't print the display name for the link dynamically.
You can print it in like this.

[PHP]
echo '<a href="next_page.php?id='.$result_row['id_column'].'">'.$result_row['id_column_or_display_name_column_goes_here'].'</a>
[/PHP]

Just give it a try and, if you failed we are here.
Jun 5 '07 #5

P: 40
Man, this forum rocks!

I tried your code, and it works!

My first page:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db = mysql_connect("localhost", "root", "yeahright"); mysql_select_db("mytest",$db);
  3. $result = mysql_query("SELECT * FROM incidents WHERE rm_date = '1900-01-01' ORDER BY incident_id",$db);
  4.     echo "<table><tr>";
  5.     echo "<td><b>Incident</b></td>";
  6.     echo "<td><b>&nbsp Facility</b></td>";
  7.     echo "<td><b>&nbsp Location</b></td>";
  8.     echo "<td><b>&nbsp Person</b></td>";
  9.     echo "<td><b>&nbsp Injury</b></td>";
  10.     echo "<td><b>&nbsp Severity</b></td>";
  11.     echo "<td><b>&nbsp Incident Date</b></td>";
  12.  
  13.     while ($myrow = mysql_fetch_array($result))
  14.     {
  15.        echo "<td><a href=edit_ir_test.php?id=".$myrow["incident_id"].">".$myrow["incident_id"]."</a></td>";
  16.        echo "<td>&nbsp ".$myrow["fac_id"]."</td>";
  17.        echo "<td>&nbsp ".$myrow["room_descr"]."</td>";
  18.        echo "<td>&nbsp ".$myrow["person_type"]."</td>";
  19.        echo "<td>&nbsp ".$myrow["injury"]."</td>";
  20.        echo "<td>&nbsp ".$myrow["severity"]."</td>";
  21.        echo "<td>&nbsp ".$myrow["inc_date"]."</td></tr>";
  22.     }
  23.        echo "</tr></table>";
  24. ?>
  25.  
and my second PHP page has:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db = mysql_connect("localhost", "root", "yeahright"); mysql_select_db("mytest",$db);
  3. $result = mysql_query("SELECT * FROM jos_incidents_combined WHERE incident_id = '$_GET[id]'",$db);
  4. $ID =  $_GET['id'];
  5.  
  6.     while ($myrow = mysql_fetch_array($result))
  7.     {
  8.     echo "<table>";
  9.        echo "<tr><td><b>Incident: &nbsp; &nbsp;</b><br>".$myrow["incident_id"]."</td>";
  10.        echo "<td><b>Facility: &nbsp; &nbsp;</b><br>".$myrow["fac_id"]."</td>";
  11.        echo "<td><b>Location: &nbsp; &nbsp;</b><br>".$myrow["room_descr"]."</td>";
  12.        echo "<td><b>Other: &nbsp; &nbsp;</b><br>".$myrow["oth_descr"]."</td></tr>";
  13.     echo "</table>";
  14. //.... etc....
  15. ?>
  16.  
Thanks!
jej1216
Jun 5 '07 #6

ak1dnar
Expert 100+
P: 1,584
Glad to hear that everything worked out for you.Keep in touch with TSDN !
Jun 6 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Glad to hear that everything worked out for you.Keep in touch with TSDN !
And thanks for posting the working code! I know it is a big help to others in the community (and Google) who have the same problem.
Jun 6 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.