简体   繁体   English

如何根据PHP中选择框选项的值填充文本框

[英]How to fill the text boxes based on the value of select box options in PHP

I have a form like this 我有这样的形式

<form >
<select name = "person">
  <option name ="first" value= "first">@$_SESSION['person']</optio>
  .
  .
  .
 </select>

and two text boxes 和两个文本框

<input type="text" name = "name" value ="" >
<input type ="text" name ="address" value= "">
</form>

Please explain with syntax ,how to fill the text boxes base on the option selected in the drop down list. 请使用语法说明,如何根据下拉列表中选择的选项填充文本框。

Another way to do this is dynamic with javascript or jquery so you don't have to reload the page. 做到这一点的另一种方法是使用javascript或jquery动态化,因此您不必重新加载页面。 like this: 像这样:

<select name="mySelect" onchange="document.getElementById('myTextBox').value = this.value">
 <option value="first">first</option>
 <option value="second">second</option>
</select>

<input type="text" id="myTextBox" value="" />

If you want to have it done without a page reload this is the code: 如果您希望在不重新加载页面的情况下完成此操作,请使用以下代码:

<select name="mySelect" onchange="document.getElementById('myTextBoxAddress').value = this.value.substr(0, this.value.indexOf('_')); document.getElementById('myTextBoxName').value = this.value.substr(this.value.indexOf('_') + 1, this.value.length);">
 <option value="<?=$_SESSION[0]['addr'] . '_' . $_SESSION[0]['name'];?>"><?=$_SESSION[0]['name'];?></option>
 <option value="<?=$_SESSION[1]['addr'] . '_' . $_SESSION[1]['name'];?>"><?=$_SESSION[1]['name'];?></option>
</select>

<input type="text" id="myTextBoxAddress" value="" />
<input type="text" id="myTextBoxName" value="" />

I put both the address and the name values in the value of the options seperated by a '_'. 我将地址和名称值都放在由'_'分隔的选项的值中。 In the onchange i divided the value string again and filled the inputs with them. 在onchange中,我再次分割了值字符串,并用它们填充了输入。

<select name="person" onchange="this.form.submit()">
<option value="Peter">@$_SESSION['person']</option>
.
.
.
</select>

<input type="text" name = "name" value ="<?php echo $_POST['person'] ?>" >

This would echo the value Peter 这将呼应彼得的价值

Hope this helps ;) 希望这可以帮助 ;)

you need a function to get person data when you sumbit the person id to it and return it as an object to use it and the options of the "select" tag should hold the person id as value like this : 您需要一个函数来获取个人数据,当您将个人ID汇总到位并将其作为使用对象返回时,“ select”标签的选项应将个人ID保留为以下值:

<select name="person" onchange="this.form.submit()">
       <option name ="first" value= "first">@$_SESSION['person']</option>
       .
       .
       .
    </select>

<input type="text" name = "name" value ="<?php echo $person->name ?>" >
<input type ="text" name ="address" value= "<?php echo $person->address?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM