0

亲爱的 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

当我在下拉列表中选择 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.
  });
}

get_user_details.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.
 ?>
4

1 回答 1

0

这是一个示例代码。以你的方式使用它。

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

// 这是下拉菜单

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

// 你需要一个 texfield 来显示所选 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>
于 2013-08-19T16:03:49.323 回答