如何在PHP中将数据从下拉列表检索到文本框?

时间:2019-01-18 06:54:48

标签: php html mysql

我不得不从数据库(MySQL)检索数据到PHP的下拉列表。现在,我想从下拉列表中检索数据到文本框中。

这是我的代码。

<form method="POST">
    <select name="searchtitle" id="drp_dwn">
      <?php
        $records = $conn->query("SELECT titles from products");
        if ($records->num_rows > 0) {
          while ($row = $records->fetch_assoc()) {
            echo "<option value=\"\"> ".$row['titles']."</option>";
            $titles1 = $row['titles'];
          }
        }
      ?>  
    </select>     
    <input type="submit" class="button" value="Show">
    <input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='<?php echo $row['titles']; ?>' />
</form>

我附上图片。 my images

希望您能支持我。 谢谢

4 个答案:

答案 0 :(得分:1)

因此,在form上添加一个空的input属性,并在提交表单后用选定的标题填充新的searchtitle。还要更改最后一个输入名称,因为select已经使用了selected,并向option元素添加了<form action="" method="post"> <select name="searchtitle" id="drp_dwn"> <?php $records = $conn->query("SELECT titles from products"); if ($records->num_rows > 0) { while ($row = $records->fetch_assoc()) { $selected = ''; if ($row['titles'] === $_POST['searchtitle']) { $selected = 'selected'; } echo "<option " . $selected . " value=\"" . $row['titles'] . "\"> " . $row['titles'] . "</option>"; } } ?> </select> <input type="submit" class="button" value="Show"> <?php if (!empty($_POST)) { ?> <input type="text" id="textArea" class="product-new-textbox" name="searchtitle1" value="<?php echo $_POST['searchtitle']; ?>" /> <?php } ?> </form> 属性:

function readCell() {
  var spreadsheet=SpreadsheetApp.getActive();
  var sheet=spreadsheet.getSheetByName('test'); 
  var G1_value=sheet.getRange('G1').getValue();
  var E_values=sheet.getRange('E1:E').getValues();

  for (var i=0; i<E_values.length; i++) {
     if (Number(E_values[i][0]) > Number(G1_value)) {
        MailApp.sendEmail('to@gmail.com', 'Subject', 'Body');
     }
  }
}

答案 1 :(得分:0)

    <form method="POST">
        <select name="searchtitle" id="drp_dwn">
          <?php
            $records = $conn->query("SELECT titles from products");
            if ($records->num_rows > 0) {
              while ($row = $records->fetch_assoc()) {
                echo "<option value=\"\"> ".$row['titles']."</option>";
                $titles1 = $row['titles'];
              }
            }
          ?>  
        </select>     
        <input type="submit" id="submit" class="button" value="Show">
        <input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='<?php echo $row['titles']; ?>' />
    </form>

简单的JS代码

<script>
$(document).on("click","#submit",function(){
var x = $("#drp_dwn").val();
$("#textArea").val(x);
});


</script>

答案 2 :(得分:0)

尝试一下。您要在文本框中显示下拉值吗?更改下拉值并单击显示按钮后,此代码将在文本框中显示下拉值。 我创建了一个showText()函数,您可以在单击显示按钮或更改下拉列表的任何位置调用该函数。您可以使用php while循环更改下拉选项的值。这是示例代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
    <select name="searchtitle" id="drp_dwn" onchange="showText()">
      <option value="one" >One</option>
      <option value="Two" >Two</option>
      <option value="Three" >Three</option>
    </select>     
    <input type="button" onclick="showText()" class="button" value="Show">
    <input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='' />
</form>
<script>
    function showText()
  {
    $("#textArea").val($("#drp_dwn").val()); 
  }
</script>

答案 3 :(得分:0)

您也可以使用

<form method="POST"  action="" >
    <select name="searchtitle" id="drp_dwn">
      <option value="one" >One</option>
      <option value="Two" >Two</option>
      <option value="Three" >Three</option>
    </select>     
    <input type="submit" onclick="showText()" class="button" value="Show">
    <input type="text" id="textArea" value="<?php if(isset($_POST['searchtitle'])){ echo $_POST['searchtitle']; } ?>" class="product-new-textbox" name="searchtitle" value='' />
</form>

或者您也可以使用get方法

<form action="" >
    <select name="searchtitle" id="drp_dwn">
      <option value="one" >One</option>
      <option value="Two" >Two</option>
      <option value="Three" >Three</option>
    </select>     
    <input type="submit" onclick="showText()" class="button" value="Show">
    <input type="text" id="textArea" value="<?php if(isset(@$_GET['searchtitle'])){ echo $_GET['searchtitle']; } ?>" class="product-new-textbox" name="searchtitle" value='' />
</form>