douluozhan4370 2013-08-28 22:01
浏览 45
已采纳

使用AJAX从SQL更新多个字段

I'm sure there's an easy answer to this but I've looked everywhere and can't seem to find an answer. I have a dropdown box at the start of a form for office names being populated from an sql table. Depending on which office the user selects, I want the other fields to be filled out with the corresponding information for that record. I used the w3schools php ajax database page as a my guide but it only shows how to update one id in the page and I need to update the input field for address, city, state, zip, and contact.

Here's the relevant code which isn't working. The Code for to trigger the script for the dropdown:

<select name="users" onchange="showOffice(this.value)" class="field select" tabindex="1" >

The Script on that page:

<script>
function showOffice(str)
{
if (str=="")
{
document.getElementById("practice_name").innerHTML="";
document.getElementById("contact").innerHTML="";
document.getElementById("address").innerHTML="";
document.getElementById("city").innerHTML="";
document.getElementById("state").innerHTML="";
document.getElementById("zip").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("practice_name").innerHTML=xmlhttp.practice_name;
document.getElementById("contact").innerHTML=xmlhttp.contact;
document.getElementById("address").innerHTML=xmlhttp.address;
document.getElementById("city").innerHTML=xmlhttp.city;
document.getElementById("state").innerHTML=xmlhttp.state;
document.getElementById("zip").innerHTML=xmlhttp.zip;
}
}
xmlhttp.open("GET","getoffice.php?q="+str,true);
xmlhttp.send();
}
</script>

And then my getoffice.php code:

<?php
$q=$_GET["q"];


$host="********"; // Host name 
$db_username="******"; // Mysql username 
$db_password="******"; // Mysql password 

// Connect to server and select database.
$con = mysqli_connect("$host", "$db_username", "$db_password"); 
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"*****");
$sql="SELECT * FROM initial_practice WHERE id = '".$q."'";

$result = mysqli_query($con,$sql);
$row=mysql_fetch_array($result);
?>
var practice_name = <? echo $row['practice_name']; ?>
var contact = <? echo $row['contact']; ?>
var address = <? echo $row['address']; ?>
var city = <? echo $row['city']; ?>
var state = <? echo $row['state']; ?>
var zip = <? echo $row['zip']; ?>
<?
mysqli_close($con);
?>

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dpiz9879 2013-08-29 15:44
    关注

    Finally figured it out. For any of you having the same trouble here's a fix.

    php code:

    $row=mysqli_fetch_assoc($result);
    
    $name = $row['practice_name'];
    $contact = $row['contact_name'];
    $address = $row['address'];
    $city = $row['city'];
    $state = $row['state'];
    $zip = $row['zip'];
    
    echo $name."#".$contact."#".$address."#".$city."#".$state."#".$zip;
    

    On-page Script:

    function showOffice(str)
    {
    if (str=="")
    {
    document.getElementById("practice_name").innerHTML="";
    document.getElementById("contact").innerHTML="";
    document.getElementById("address").innerHTML="";
    document.getElementById("city").innerHTML="";
    document.getElementById("state").innerHTML="";
    document.getElementById("zip").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)
    {
    
        if(xmlhttp.status==200)
        {
            var data = xmlhttp.responseText.split("#");
            var name = decodeURIComponent(data[0]);
            var contact = decodeURIComponent(data[1]);
            var address = decodeURIComponent(data[2]);
            var city = decodeURIComponent(data[3]);
            var state = decodeURIComponent(data[4]);
            var zip = decodeURIComponent(data[5]);
            document.initialpractice.practice_name.value = name;
            document.initialpractice.contact.value = contact;
            document.initialpractice.address.value = address;
            document.initialpractice.city.value = city;
            document.initialpractice.state.value = state;
            document.initialpractice.zip.value = zip;
        }
    }
    };
    xmlhttp.open("GET","getoffice.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化