form textbox populated by clicking on a list of data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jej1216
    New Member
    • Aug 2006
    • 40

    form textbox populated by clicking on a list of data

    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:

    [code=php]
    $db = mysql_connect(" localhost", "root", "yeahright" ); mysql_select_db ("etest",$db );
    $result = mysql_query("SE LECT * FROM incidents WHERE rm_date = '1900-01-01' ORDER BY incident_id",$d b);
    echo "<table><tr >";
    echo "<td><b>Inciden t</b></td>";
    echo "<td><b>&nb sp Facility</b></td>";
    echo "<td><b>&nb sp Location</b></td>";
    echo "<td><b>&nb sp Person</b></td>";
    echo "<td><b>&nb sp Injury</b></td>";
    echo "<td><b>&nb sp Severity</b></td>";
    echo "<td><b>&nb sp Incident Date</b></td>";

    while ($myrow = mysql_fetch_arr ay($result))
    {
    echo "<tr><td>".$myr ow["incident_i d"]."</td>";
    echo "<td>&nbsp ".$myrow["fac_id"]."</td>";
    echo "<td>&nbsp ".$myrow["room_descr "]."</td>";
    echo "<td>&nbsp ".$myrow["person_typ e"]."</td>";
    echo "<td>&nbsp ".$myrow["injury"]."</td>";
    echo "<td>&nbsp ".$myrow["severity"]."</td>";
    echo "<td>&nbsp ".$myrow["inc_date"]."</td></tr>";
    }
    echo "</tr></table>";
    [/code]

    and:

    [code=html]
    <FORM METHOD="POST" ACTION="edit_ir .php">
    <h3>Enter the Incident ID for the Report You Need to Edit:</h3>
    <table border="0" cellpadding="0" >
    <tr>
    <td><INPUT TYPE=TEXT NAME="incident_ id" VALUE="" SIZE=6 MAXLENGTH=6></td>
    <td><INPUT TYPE=SUBMIT VALUE="Edit"></td>
    <td><INPUT TYPE=RESET VALUE="Reset"></td>
    </tr>
    </table>
    </FORM>
    [/code]

    I'd like to either have the $myrow["incident_i d"] 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_i d"] fields in the options list.

    TIA,

    jej1216
  • adamalton
    New Member
    • Feb 2007
    • 93

    #2
    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="javascrip t:document.your _form_name.inci dent_id.value=" <?php print $myrow["incident_i d']; ?>;"><?php print $myrow["incident_i d']; ?></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="javascrip t">
    function choose_incident (chosen_id){
    document.your_f orm_name.incide nt_id.value=cho sen_id;
    document.your_f orm_name.submit ();
    }
    </head>

    And then do each link like this...

    <a href="javascrip t:choose_incide nt('<?php print $myrow["incident_i d']; ?>');"><?php print $myrow["incident_i d']; ?></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=\"javascri pt:choose_incid ent('". $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!)

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      From your first page try like this.

      first_page.php
      [PHP]echo '<a href="edit_ir.p hp?id='. $myrow["incident_i d"].'">ID 1001</a>';
      echo '<a href="edit_ir.p hp?id='. $myrow["incident_i d"].'">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]

      Comment

      • jej1216
        New Member
        • Aug 2006
        • 40

        #4
        Originally posted by ajaxrand
        From your first page try like this.

        first_page.php
        [PHP]echo '<a href="edit_ir.p hp?id='. $myrow["incident_i d"].'">ID 1001</a>';
        echo '<a href="edit_ir.p hp?id='. $myrow["incident_i d"].'">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

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by jej1216
          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='.$resu lt_row['id_column'].'">'.$result_r ow['id_column_or_d isplay_name_col umn_goes_here'].'</a>
          [/PHP]

          Just give it a try and, if you failed we are here.

          Comment

          • jej1216
            New Member
            • Aug 2006
            • 40

            #6
            Man, this forum rocks!

            I tried your code, and it works!

            My first page:
            [code=php]
            <?php
            $db = mysql_connect(" localhost", "root", "yeahright" ); mysql_select_db ("mytest",$d b);
            $result = mysql_query("SE LECT * FROM incidents WHERE rm_date = '1900-01-01' ORDER BY incident_id",$d b);
            echo "<table><tr >";
            echo "<td><b>Inciden t</b></td>";
            echo "<td><b>&nb sp Facility</b></td>";
            echo "<td><b>&nb sp Location</b></td>";
            echo "<td><b>&nb sp Person</b></td>";
            echo "<td><b>&nb sp Injury</b></td>";
            echo "<td><b>&nb sp Severity</b></td>";
            echo "<td><b>&nb sp Incident Date</b></td>";

            while ($myrow = mysql_fetch_arr ay($result))
            {
            echo "<td><a href=edit_ir_te st.php?id=".$my row["incident_i d"].">".$myrow["incident_i d"]."</a></td>";
            echo "<td>&nbsp ".$myrow["fac_id"]."</td>";
            echo "<td>&nbsp ".$myrow["room_descr "]."</td>";
            echo "<td>&nbsp ".$myrow["person_typ e"]."</td>";
            echo "<td>&nbsp ".$myrow["injury"]."</td>";
            echo "<td>&nbsp ".$myrow["severity"]."</td>";
            echo "<td>&nbsp ".$myrow["inc_date"]."</td></tr>";
            }
            echo "</tr></table>";
            ?>
            [/code]
            and my second PHP page has:
            [code=php]
            <?php
            $db = mysql_connect(" localhost", "root", "yeahright" ); mysql_select_db ("mytest",$d b);
            $result = mysql_query("SE LECT * FROM jos_incidents_c ombined WHERE incident_id = '$_GET[id]'",$db);
            $ID = $_GET['id'];

            while ($myrow = mysql_fetch_arr ay($result))
            {
            echo "<table>";
            echo "<tr><td><b>Inc ident: &nbsp; &nbsp;</b><br>".$myrow["incident_i d"]."</td>";
            echo "<td><b>Facilit y: &nbsp; &nbsp;</b><br>".$myrow["fac_id"]."</td>";
            echo "<td><b>Locatio n: &nbsp; &nbsp;</b><br>".$myrow["room_descr "]."</td>";
            echo "<td><b>Oth er: &nbsp; &nbsp;</b><br>".$myrow["oth_descr"]."</td></tr>";
            echo "</table>";
            //.... etc....
            ?>
            [/code]
            Thanks!
            jej1216

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Glad to hear that everything worked out for you.Keep in touch with TSDN !

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by ajaxrand
                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.

                Comment

                Working...