Page 1 of 1

Newbee - fill in a text field with data from DB

Posted: Mon Jan 30, 2006 3:54 am
by kalli
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,
I have a form where the user selects a piece of equipment from a list box and then the placement of this equipment is to be drawn from a database (Oracle) and written to a text field on the same form. Then there is a second list box where the user can select a new placement and submit it. I have a form action linking to another page but this is only to be invoked when the user presses SUBMIT.  The problem here is to draw the placement and write it to the text field as soon as the user has selected an equipment. Can someone suggest a solution.

I include a piece of the source just in case:

Code: Select all

<form action="taeki_handle_breytt_stadsetn.php" method="post" enctype="application/x-www-form-urlencoded" name="Viðhalda einingaskrá" id="Viðhalda einingaskrá">
  <fieldset>
  <legend>Veljið mælitæki og nýja staðsetningu hér að neðan:</legend>
  <p>&nbsp;</p>
  <table width="333" border="0" align="center">
    <tr>
      <td width="134">Mælitæki</td>
      <td width="143"><select name="s_taeki" id="s_taeki" onChange="alert('You Chose '+this.form.dir.value);">
        <?php for($N=0;$N<sizeof($Maelitaeki);$N++){?>
        <option value="<?php echo($Maelitaeki[$N][1]);?>"><?php echo($Maelitaeki[$N][0]);?></option>
        <?php }?>
      </select></td>
    </tr>
    <tr>
      <td>Núverandi staðsetning</td>
      <td><input name="t_nuv_stadur" type="text" id="t_nuv_stadur" /></td>
    </tr>
    <tr>
      <td>Ný staðsetning</td>
      <td>
	  <select name="s_nyr_stadur" id="s_nyr_stadur">
        <?php for($N=0;$N<sizeof($Stadarheiti);$N++){?>
        <option value="<?php echo($Stadarheiti[$N][1]);?>"><?php echo($Stadarheiti[$N][0]);?></option>
        <?php }?>
      </select> </td>
    </tr>
  </table>
  <br>
  <br>
  <p>
  <div align="center">
    <input name="b_skra" type="submit" id="b_skra" value="Framkv&aelig;ma" />
    <input name="b_haetta_vid" type="reset" id="b_haetta_vid" value="H&aelig;tta vi&eth;" />
  </div>
  </p>
  </fieldset>
</form>

Thanks,
Kalli


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Jan 30, 2006 4:19 am
by raghavan20
when the user selects something on the select box use, window.location=formname.selectname.value
append an URL variable to this like action=loadPosition&id=elementId
Check this action using your PHP code and use select query to get the position of the element and echo this value into the text area.

when you want to submit a new position, you can have that script in the same file as well. use action=savePosition&id=elementId&value=positionValue. Detect this action and use update statement to update the new position..

Posted: Tue Jan 31, 2006 4:25 am
by kalli
Thank you for the reply. Can I ask you to be more specific though? I don´t understand how to use and implement the "window.location=formname.selectname.value" and the action stuff.

Thanks,
Kalli

Posted: Tue Jan 31, 2006 4:54 am
by raghavan20
A part of the code which I replied in another thread....

Code: Select all

$query = "select * from `re_categories`"; 
if (is_resource($result = mysql_query($query))){ 
    if (mysql_num_rows($result) > 0){ 
?> 
        Main categories&nbsp;&nbsp; 
        <form name = 'frmChooseCategory' method = 'get'> 
        <select name = 'chooseCategory' onchange = 'document.window.location=frmChooseCategory.chooseCategory.value'> 
            <option>Main Categories</option> 
<?php         while ($row = mysql_fetch_assoc($result)){    ?> 
            <option value = "<?php echo $_SERVER[PHP_SELF]."?category=".$row["CategoryID"]; ?>"> 
                <?php echo $row["CategoryName"]; ?> 
            </option> 
<?php        }                        ?> 
        </select> 
        </form><br /> 
<?php 
    } 
}