0

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

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

我需要从数据库中获取所选供应商的地址,并将其显示在文本区域而不刷新整个页面..感谢您的帮助..

阅读答案后,我重新编写了代码

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

   ?>

   <script>
   $(document).ready(function(e)
   {
  $("#slcSupplier").change(function(){
    var id = $(this).val();

    $.ajax({
        url: 'supplier.php',
        method: 'POST',
        data: "id=" + id,
        success: function(data)
        {
            $('#txtAddress').html(data);
        }
    }); 
});
    });
    </script>

    <div> 

    <table> 
     <tr>
       <td colspan="3">
         <input type="button" name="a" id="a" value="Submit" /></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>

我不知道出了什么问题,但是javascript不起作用..我在这里很无能为力..

4

2 回答 2

0

在标题中包含 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);
            }
        })
    });
});
于 2013-06-21T07:03:25.050 回答
0

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'];
        }
    }
}
于 2013-06-21T06:59:07.593 回答