Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据下拉列表填写表单字段_Php_Jquery_Ajax - Fatal编程技术网

Php 如何根据下拉列表填写表单字段

Php 如何根据下拉列表填写表单字段,php,jquery,ajax,Php,Jquery,Ajax,我有一个带有用户名、地址和电子邮件的表格。 姓名列表来自数据库,我需要做的是,当我选择用户名时,地址和电子邮件信息会填充其他两个字段(我还有其他字段,但我只需要填充这些字段) 我将php与mysql数据库一起使用显然,您必须修改它以匹配您的元素ID等等,但这对我来说是可行的 这是您的HTML: <script type="text/javascript"> function grabInfo(str) { if (str=="") { document.getElementB

我有一个带有用户名、地址和电子邮件的表格。 姓名列表来自数据库,我需要做的是,当我选择用户名时,地址和电子邮件信息会填充其他两个字段(我还有其他字段,但我只需要填充这些字段)
我将php与mysql数据库一起使用

显然,您必须修改它以匹配您的元素ID等等,但这对我来说是可行的

这是您的HTML:

<script type="text/javascript">
function grabInfo(str)
{
if (str=="")
  {
  document.getElementById("contentDiv").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
       document.getElementById("contentDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getinfo.php?q="+str,true);
xmlhttp.send();
}
</script>

<select name="users" onchange="grabInfo(this.value)">
    <option value="" selected="selected">Select a person:</option>
    <option value="1">Person A</option>
    <option value="2">Person B</option>
    </select>

    <div id="contentDiv">

    </div>

函数grabInfo(str)
{
如果(str==“”)
{
document.getElementById(“contentDiv”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“contentDiv”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“getinfo.php?q=“+str,true”);
xmlhttp.send();
}
选择一个人:
人A
B人
和后端PHP(getinfo.PHP):



contentDiv
将显示两个文本框
getinfo.php
将是实际操作数据库的php。

这可能不是最优的-实际上我不确定它是否有效,因为我根本没有测试它,但我相信如果它不起作用,您会发现它:

假设您的表单有一个name=“personInfo”,则name下拉列表有name=“personName”,地址是一个名为“address”的文本输入字段

在personName中选择tag add

onChange="updateValues()"
然后您将需要一些javascript,如下所示:

函数updateValue(){
var addresses=新数组();
document.personInfo.address.value=地址[document.personInfo.personName.selectedIndex];
}

对电子邮件重复同样的内容

什么都不做,因为我看到的每个示例都会预填充第二个下拉列表,我不需要使用jQuery和ajax。在下拉列表中设置更改事件,从php脚本获取信息并填充字段。。。这应该让你开始。。。一旦你写了代码,如果你仍然有问题,回来让我们知道。。。我们很乐意帮助。。。
onChange="updateValues()"
function updateValue() {
var addresses = new Array();
<?php
//add some php code to populate the java array. Something like
for ($i=0; $i<$numAddresses; $i++) {
    echo 'addresses['.$i.'] ='.$addressFromDB[$i].';';
}
?>
document.personInfo.address.value = addresses[document.personInfo.personName.selectedIndex];   

}