Php 如何在更改组合框选项时从sql数据库设置文本框值?

Php 如何在更改组合框选项时从sql数据库设置文本框值?,php,jquery,sql,ajax,Php,Jquery,Sql,Ajax,我有一个让我困惑的学校项目(虽然看起来很简单)。。 这里是交易,我需要根据combobox的选择设置文本框的值。这是我到目前为止得到的 <?php require_once( str_replace('//','/',dirname(__FILE__).'/') . 'library/SfCrud.php' ); $sfCrud = new SfCrud(); $resultSupplier = $sfCrud->read('select * f

我有一个让我困惑的学校项目(虽然看起来很简单)。。 这里是交易,我需要根据combobox的选择设置文本框的值。这是我到目前为止得到的

    <?php require_once( str_replace('//','/',dirname(__FILE__).'/') . 'library/SfCrud.php' );   
      $sfCrud = new SfCrud();
      $resultSupplier = $sfCrud->read('select * from supplier');
    ?>

     <form action="" method="post">
     <div align="center">
       <table>  
       <tr>
         <td colspan="3">&nbsp;</td>
       </tr>
       <tr>
         <td colspan="3">Supplier Information</td>
       </tr>
       <tr>
         <td>Supplier  </td>
         <td>:</td>
         <td>
           <select style="width:100%" id='slcSupplier' name="slcSupplier">
             <?php foreach($resultSupplier as $row){?>
         <option  value="<?php echo($row->supplierID);?>"> <?php echo($row->supplierID.' : '.$row->name);?>
             </option> <?php }?>
           </select>
         </td>
       </tr>
       <tr>
         <td>Address</td><td>:</td>
         <td> <textarea id="txtAddress" name="txtAddress"></textarea> </td>
       </tr>
       </table>
     </form>

供应商信息
供应商
:
AJAX

$(document).ready(function()
{
    $('#slcSupplier').change(function()
    {
        var id = $(this).val();

        $.ajax({
            url: 'your_php.php',
            type: 'POST',
            data: { id: id },
            success: function(data)
            {
                $('#txtAddress').text(data);
            }
        })
    });
});
PHP

if(isset($_POST['id']))
{
    $id = $_POST['id'];

    $query = $this->db->query("SELECT address FROM supplier WHERE id =". $id);
    if($query)
    {
        while($row = $query->fetch(PDO::FETCH_ASSOC))
        {
            echo $row['address'];
        }
    }
}

在标题中包含jquery

创建另一个名为
newpage.php
的文件,其中包含以下概念

$supplier_id = $_POST['supplier_id']; //read the supplier id from the post

 $query = "SELECT blah,blah FROM supplier WHERE id = " . $supplier_id; //build a query to retrieve the required field

 $row = fetch_row($query); //execute the query and get the results.

 $json = array('field' => $row['field1'], 'field2' => $row['field2']);

 echo json_encode($json);
然后在您当前的html页面中输入以下代码

$(document).ready(function()
{
    $('#clcSupplier').change(function()
    {
        var id = $(this).val();

        $.ajax({
            url: 'newpage.php',
            type: 'POST',
            dataType: 'json',
            data: "supplier_id=" + id,
            success: function(data)
            {
                $('#txtAddress').val(data.field + data.field2);
            }
        })
    });
});

javascript不起作用。。我写的代码可能有问题,但我想不出来。请参阅我的重新发布。@KENI将
方法
替换为
类型
,在
方法:“发布”
中将其更改为
类型:“发布”
$(document).ready(function()
{
    $('#clcSupplier').change(function()
    {
        var id = $(this).val();

        $.ajax({
            url: 'newpage.php',
            type: 'POST',
            dataType: 'json',
            data: "supplier_id=" + id,
            success: function(data)
            {
                $('#txtAddress').val(data.field + data.field2);
            }
        })
    });
});