0

我有 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.

谢谢你的帮助!

4

1 回答 1

0

您的表单具有一个选定字段,该表单由用户数据库填充,然后选择用户时,您希望该用户的公司名称出现在同一页面上而无需重新加载表单。

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);
?>
于 2013-08-19T19:45:55.607 回答