php - 动态下拉选择使用ajax填充数据库中的文本字段

标签 php mysql ajax

我有 2 个文件。 html.php 和 getuser.php

在 html.php 中,我有一个动态下拉列表,其中填充了来自 mysql 数据库的 ID。 在 getuser.php 中,我的代码应该采用所选的下拉列表值并返回公司名称,其中 id 与下拉列表中的 id 值相同。

你可以在jsfiddle中看到文件(在js区域!):

 html.php 

( http://jsfiddle.net/qHvZM/1/ )

 getuser.php 

( http://jsfiddle.net/Ez9Hs/ )

<小时/>

我想要实现什么: 该数据库具有以下详细信息:

cb_dealerid = 001, 002, 003, 004

cb_bedrijfsnaam = Joop BV、Kelly Ltd、Johan Ltd、Peter BV

当您从下拉值 002 中选择时,它应该在相应的区域显示 Kelly Ltd.

感谢您的帮助!

最佳答案

You have a form with a select field that is populated with a database of users and then when a user is selected, you want the company name for that user to appear on the same page without reloading the form.

html.php:

<script>
//AJAX request function
function createRequestObject(){
    var requestObject;
    requestObject = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    return requestObject;
}

//AJAX needed var for request object
var http = createRequestObject();

//AJAX check for exsiting value for column in table in the database
function showUser(str){
    if (str==""){
        document.getElementById("txtHint").innerHTML="";
        return;
    }
    http.open("get","getuser.php?q="+str);//you may need to put in full path to your page such as "http://youdomain.com/getuser.php?q="
    http.onreadystatechange = AJAXresponseAction;
    http.send(null);
}

//AJAX function to run on status change
function AJAXresponseAction(){
    if(http.readyState == 4){
        var response = http.responseText;
        //update element with AJAX response text
        document.getElementById("txtHint").innerHTML=response;
    }
}
</script>
<?php
// Load Joomla! configuration file
require_once('configuration.php');
// Create a JConfig object
$config = new JConfig();
// Get the required codes from the configuration file
$server = $config->host;
$username   = $config->user;
$password   = $config->password;
$database = $config->db;
// Tools dropdown
$con = mysql_connect($server,$username,$password);
mysql_select_db($database);
$sql = "SELECT cb_dealerid FROM cypg8_comprofiler";
$result = mysql_query($sql);
?>
<form>
    <select name='cb_dealerid' onchange='showUser(this.value)'>
        <?php while ($row = mysql_fetch_array($result)) { ?>
        <option value="<?php print $row['cb_dealerid'];?>"><?php print $row['cb_dealerid'];?></option>
        <?php } ?>
    </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

获取用户.php:

<?php
$q=$_GET["q"];
// Load Joomla! configuration file
require_once('configuration.php');
// Create a JConfig object
$config = new JConfig();
// Get the required codes from the configuration file
$server = $config->host;
$username   = $config->user;
$password   = $config->password;
$database = $config->db;

$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,$database);

//********************************************************************//
// ALWAYS MAKE SURE TO ESCPAE STRINGS WHEN USING VARIABLES IN QUERIES //
//********************************************************************//

$q = mysqli_real_escape_string($con,$q);

$sql="SELECT * FROM cypg8_comprofiler WHERE cb_dealerid = '".$q."' LIMIT 1";

$result = mysqli_query($con,$sql);

$row = mysqli_fetch_array($result);

//If a table is needed build it on the html page for easier coding.
print $row['cb_dealerbedrijfsnaam'];

mysqli_close($con);
?>

关于php - 动态下拉选择使用ajax填充数据库中的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18319013/

相关文章:

php - mysql特殊日期输出格式

jquery - 可以在 Android 上使用 AJAX 调用吗?

javascript - PHP _POST 与 Ajax 不工作

php - 在 codeigniter、querybuilder 和 "classical"中查询

php - 我如何使用 laravel 中的子数据数量获取 parent 的顺序

mysql - Auth0 创建用户脚本给我一个错误 "mysql is not a function"

mysql - 版本控制独立记录并根据配置列出所有记录

javascript - Yi2 使用 SendFile() 下载文件

php - 取消设置 mysql 结果

mysql - 为什么这些查询在 MySQL 5.5 和 5.6 中产生不同的结果?