dougengqiu8031
dougengqiu8031
2014-07-28 09:57

从下拉框中获取选定的值,然后使用填充文本框

  • php
  • html
  • javascript

I have a web form that contains a drop down box that is populated by a request to a MySQL server with php, and a text box. The value in the text box is currently passed off to another page for processing. This all works fine but as there are a lot of submissions with the same value being passed through I've decided to implement the drop down box within the form to allow a user to select from the options that have been submitted previously.

What I would like is when the user clicks on a value in the drop down box that the value is used to populate the text box in the webform. I'm having trouble pulling this value out. I'm assuming that it requires an onClick/onSelect listener to be implemented but I'm not entirely sure how to do this. Any help would be greatly appreciated!

edit for code, currently I'm just trying to get the value to echo to the page as a proof of concept, but it will eventually need to go into the text box.

 <script type="text/javascript">
function moveToTextBox()
    echo $("#dropDownBox option:selected").text(); 
</script>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

        Enter Asset Type<br/>
        <select name="currentItems" onUpdate="moveToTextBox()" id="dropDownBox">
        <?php  
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "SELECT item FROM all_Items";
            $result=$pdo->query($sql);
            $array=null;
            while($row=$result->fetch(PDO::FETCH_ASSOC))
            {
                echo "<option value=\"item1\">" . $row['item'] . "</option>";
            }
            Database::disconnect(); 
        ?>
        </select><br/>
        <input type="text" name="newItem" id="textId"><span class="error"> <?php echo $newItemError;?></span>
    <br/>
    <input type="submit" name="submit" value="Submit"> 
</form>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐