gpt4 book ai didi

php js mysql下拉选择用db值填充文本框

转载 作者:行者123 更新时间:2023-11-30 01:23:07 26 4
gpt4 key购买 nike

亲爱的 stackoverflow 用户,我正在绞尽脑汁寻找一段不会太困难的代码,但不知何故我的大脑今天不工作,所以请帮忙。

我正在创建一个表单,我想从下拉列表中选择一个 ID(这是有效的),选择后我想在文本字段中查看与该 ID 相关的所有记录。我想手动创建字段,所以我不需要自动创建脚本。这是我到目前为止的代码:

 <!--Onderstaande gegevens worden NIET geprint!-->
<div id="non-printable">
<!---->
<?
// 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);
// Dealergegevens fields

?>
<!--Begin TOOLS-->
<div class="tools">
<div class="dealerselectie">
<?
echo "<select name='cb_dealerid'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['cb_dealerid'] . "'>" . $row['cb_dealerid'] . "</option>";
}
echo "</select>";
?>
</div><!--/.Dealerselectie-->
</div><!--/.Tools-->
<!--Einde TOOLS-->
<!--Begin DEALERGEGEVENS-->
<div class="dealergegevens">
<input type="text" name="cb_dealerid" value='" . $row['cb_dealerid'] . "'><br>
<input type="text" name="cb_dealerbedrijfsnaam" value='" . $row['cb_dealerbedrijfsnaam'] . "'>

</div><!--/.dealergegevens-->
<!--Einde DEALERGEGEVENS-->
</div><!--/#non-printable-->
<!--Bovenstaande gegevens worden NIET geprint!-->

我知道我做错了什么,但我不知道它是什么。我需要在不使用多页表单的情况下创建它,因此所有内容都需要保留在一页上。任何建议将不胜感激。

提前致谢。

编辑1:

我一直在努力让它明显地发挥作用,这是尝试之一,但也不起作用。

   <?echo "<input type=\"text\" value='" . $row['cb_dealerid'] . "'>";?>
<?echo "<input type=\"text\" value='" . $row['cb_dealerbedrijfsnaam'] . "'>";?>

编辑 2:<==有关我想要实现的目标的更多信息

我的数据库表中有以下详细信息

cb_dealerid = 100, 101, 102, 103
cb_dealerbedrijfsnaam = willem, henk, piet, klaas

当我在下拉列表中选择 id 101 时,我想在文本框中看到名称 henk。

编辑3:

我知道有 2 个文件 html.phpget_user_details.php:

HTML.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);
?>
<?
echo "<select name='cb_dealerid' id='user_ids' onchange='user_details(this.value)'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['cb_dealerid'] . "'>" . $row['cb_dealerid'] . " </option>";
}
echo "</select>";
?>
<input type="text" name="cb_dealerbedrijfsnaam" id="cb_dealerbedrijfsnaam" >
<script type="text/javascript">
function user_details(id)
{
$.get('get_user_details.php', {user_id:id}, // Response file get_user_details.php. This file is to bring all details against the id you selected from dropdown.. Make a JSON form by encode function.
function(data)
{
var jsonArr = jQuery.parseJSON(data); // here you received your JSON data
var username = jsonArr.user_name; // Now this variable user_name is the Array Index Name which you defined in your response file.
$('#Name').val(username); // Now assign this value to a textfield with ID Name.
});
}

获取用户详细信息.php

 <?
$User_Array = array($_GET['cb_dealerid']); // Just an example. You need to fetch data properly by $_GET['user_id'];
echo json_encode($User_Array); // You can check this response in your Console.
?>

最佳答案

这是一个示例代码。按照您的方式使用它。

首先调用一个函数:像这样:

//这是下拉菜单

<select name="user_ids" id="user_ids" onchange="user_details(this.value)">

//U 需要一个文本字段来显示所选 Id 的任何单个属性

<input type="text" name="Name" id="Name" >

//你需要一个响应文件:在其下面的代码中:get_user_details.php

$User_Array = array('user_name'=>Necromancer); // Just an example. You need to fetch data properly by $_GET['user_id'];

echo json_encode($User_Array); // You can check this response in your Console.

这是你的 JS 代码:

<script type="text/javascript">
function user_details(id)
{


$.get('get_user_details.php', {user_id:id}, // Response file get_user_details.php. This file is to bring all details against the id you selected from dropdown.. Make a JSON form by encode function.


function(data)
{

var jsonArr = jQuery.parseJSON(data); // here you received your JSON data
var username = jsonArr.user_name; // Now this varaible user_name is the Array Index Name which you defined in your response file.

$('#Name').val(username); // Now assign this value to a textfield with ID Name.





});
}
</script>

关于php js mysql下拉选择用db值填充文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18317361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com