Php 基于下拉选择在文本框中显示值

Php 基于下拉选择在文本框中显示值,php,jquery,mysql,Php,Jquery,Mysql,我正在尝试根据选择的值在文本框中显示值。例如,如果我从select元素中选择了一个客户名称,那么他的详细信息(如地址)和电话号码需要在这些文本框中动态填充 这是MySQL表 表名:客户 MYSQL和PHP <?php $select_customer = "select * from customer"; $select_customer_query = mysqli_query($connection, $select_customer); // Customer

我正在尝试根据选择的值在文本框中显示值。例如,如果我从select元素中选择了一个客户名称,那么他的详细信息(如地址)和电话号码需要在这些文本框中动态填充

这是MySQL表

表名:客户

MYSQL和PHP

<?php
$select_customer = "select * from customer";
$select_customer_query = mysqli_query($connection, $select_customer); // Customer Data
?>

      <form method="post" action="">
      <div class="form-group">
      <select class="form-control" name="customer_name">
      <option>Customer Name</option>
      <?php while($customer_result = mysqli_fetch_assoc($select_customer_query)) { ?>
      <option><?php echo $customer_result['name']; ?></option>
      <?php } ?>
      </select>
      </div>
      <div class="form-group">
      <input type="text" class="form-control" placeholder="Phone Number">
      </div>
      <div class="form-group">
      <input type="text" class="form-control" placeholder="Website">
      </div>
      </form>

客户名称

当所选值更改时,您需要使用onChange获取值

要获取电话号码和网站等值,您可以将其他值作为属性添加到选项中,然后进行更改

现在,当调用onChange时,您需要从所选选项中获取属性值,并使用jQuery在文本框中进行设置

  <form method="post" action="">
      <div class="form-group">
      <select class="form-control" name="customer_name">
      <option>Customer Name</option>
      <?php while($customer_result = mysqli_fetch_assoc($select_customer_query)) { ?>
      <option website="<?php echo $customer_result['website']; ?>" phone_number="<?php echo $customer_result['phone_number']; ?>" ><?php echo $customer_result['name']; ?></option>
      <?php } ?>
      </select>
      </div>
      <div class="form-group">
      <input id="phone_number" type="text" class="form-control" placeholder="Phone Number">
      </div>
      <div class="form-group">
      <input id="website" type="text" class="form-control" placeholder="Website">
      </div>
      </form>
<script>
$(document).ready(function () {     
$('select[name="customer_name"]').change(function(){
   var website = $('option:selected', this).attr('website');
   $("#website").val(website);
   var phone_number = $('option:selected', this).attr('phone_number');
   $("#phone_number").val(phone_number);
});
});
</script>
$('option:selected',this.attr('website')


`这将通过名称获取属性,我们将其传入,就像当前网站从所选选项中一样

您尝试了什么?一些javascript?我知道这将在jquery和ajax的帮助下实现!但是对于jquery@The_Death_raw,我不太擅长使用jquery AJAX,或者如果您对jquery不太熟悉,请使用javascript根据选定的客户名称获取数据,并相应地填充表单。您必须在选择时使用
onchange=“methodName()”
,获取所选选项的值。谢谢@HassanMalik,我找到了!
`