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

时间:2013-08-19 15:18:21

标签: php javascript mysql database

亲爱的stackoverflow用户,我正在敲打我的脑袋,寻找一段不会太难的代码,但不知怎的,我的大脑今天无法工作,所以请帮忙。

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

 <!--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:&lt; ==关于我想要实现的更多信息

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

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

当我在下拉列表中选择时,我希望在文本框中看到名称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.
 ?>

1 个答案:

答案 0 :(得分: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.

这是Ur 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>