【问题标题】:php js mysql dropdown selection fills textboxes with db valuephp js mysql下拉选择用db值填充文本框
【发布时间】:2013-08-19 15:18:21
【问题描述】:

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

【问题讨论】:

  • 你不应该使用 mysql_query。而是研究 PDO 甚至 ADODB。
  • 我猜不出你想做什么。
  • 我刚刚在原始问题中添加了更多信息。感谢您的回答。再见,我不知道 PDO 或 ADODB 是什么。而且我相信mysql是可能的。
  • mysql_query 不安全且已弃用。
  • 至于你想做什么,在google上查找jquery.org和$.post。

标签: php javascript mysql database


【解决方案1】:

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

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

// 这是下拉菜单

<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>

【讨论】:

  • 我已经尝试实现你所说的,但我做错了我相信我已经创建了 2 个 jsfiddle 页面,一个用于 html 页面,另一个用于获取用户详细信息页面。请看看他们 [链接]jsfiddle.net/zHzB9/1 [链接]jsfiddle.net/yb6vT
  • 是的,我知道我已经尝试按照 W3Schools(w3schools.com/php/php_ajax_database.asp) 的教程进行操作,这基本上是在做我想做的事情,但是当我将详细信息更改为服务器/数据库中的详细信息时,它会停止工作。
  • 你需要调试你的代码。使警报发挥作用。检查您的函数是否被调用。是带数据还是不带数据。
  • 你给我的示例代码。我想我用错了你说我需要更多地了解 Jquery。我在您的代码中没有看到任何 jquery(它确实在那里,但我不认识它)。感谢您提供的所有帮助,但有没有一种方法可以告诉我要复制/粘贴什么以及将其粘贴到哪个文件中。基本上是一步一步来的。当我有一个文本字段工作时,我知道我可以自己添加所有其他文本字段。我今天看了很多教程,其中大部分都没有定论,即使是来自 w3schools 的教程。如果你不想做“我”的工作,我可以理解,但我希望你能帮忙。
  • 我没有房间了!我不知道在哪里放置警报。我已将您提供的代码复制粘贴到 2 个文件中,因为这是我认为我应该做的,我将更新这两个文件现在的内容。它在 EDIT 3 下
猜你喜欢
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多