简体   繁体   English

在PHP中的选择选项上设置输入值

[英]Set input value upon select option in php

I have a form where I am selecting a product. 我有一个要选择产品的表格。 On the basis of the selected product, I want to update the rest of the fields in the form. 根据所选产品,我要更新表单中的其余字段。

The Form display with select option 带有选择选项的表单显示

On selecting the product name, I want rest of the details of the form to fill up from the database using php. 在选择产品名称时,我希望使用php从数据库中填写表格的其余详细信息。

Here is the code for the table created. 这是创建表的代码。

<table id="productTable" class="table-c">
                  <tr>
                    <th class="text-center" style="width: 5%;">SR No.</th>
                    <th class="text-center" style="width: 45%">DESCRIPTION</th>
                    <th class="text-center" style="width: 10%">HSN/SAC</th>
                    <th class="text-center" style="width: 10%">QTY IN-HAND</th>
                    <th class="text-center" style="width: 10%">ENTER OUTWARD QTY</th>
                    <th class="text-center" style="width: 5%">Delete</th>
                  </tr>
                  <tr style="text-align: center;" id="products">
                    <?php $j=0; $j++; ?>
                    <td><?php echo $j; ?></td>
                    <td><select class="form-control" name="code" id="productID" style="width: 429px;">
                      <?php

                      $sql = "SELECT * FROM `product`";
                      $result = $conn->query($sql);

                      if ($result->num_rows > 0) {
                          // output data of each row
                          while($row = $result->fetch_assoc()) {
                            echo "<option id='".$row['code']."' value='".$row['pname']."'>".$row['pname']."</option>";
                          }
                      } else {
                          echo "0 results";
                      }
                      ?>
                      </select>
                    </td>
                    <td><input type="number" name="hsnNo" id="hsnNo" readonly></td>
                    <td><input type="number" name="qty" id="qty" readonly></td>
                    <td class="coljoin"><input type="number" format="2" name="amount"></td>
                    <td>
                      <span class="fa fa-trash"></span>
                    </td>
                  </tr>
                </table>

How should I do this? 我应该怎么做?

using product id use ajax & call the get_product_details(id) & in get product convert response array in json & echo it .in ajax, response data you have to parse in json... then set you required field.. (add require js) 使用产品ID使用ajax并调用get_product_details(id),然后在json中获取产品转换响应数组并在.ajax中,您必须在json中解析响应数据...然后设置您的必填字段。。(添加require js )

**Ajax Call :**
    $(document).on('change','your_dropdown_filed_id',function()
    {
       var product_id = $(this).val();
       //Ajax call
       $.ajax({
        method: "POST",
        url: "/get_product_details", //your php file function path
        data: { product_id: product_id}
        }).done(function( response ) {
        alert( "Data Saved: " + response );
        var obj = JSON.parse(response);
        //obj hold all values
        alert(obj.qty);
        alert(obj.hsn);
        $('#hsn_id').val(obj.hsn);
        $('#qty_id').val(obj.qty);
       });
    });

**product.php**

    function get_product_details(product_id)
    {
    //Get product details in associative array format
    echo json_encode($product_details);
    }

This is PHP Code:- 这是PHP代码:-

<form>
    <select name="customer" id="customer_id">
        <option value="">-- Select customer Name -- </option>
        <option value="1">John</option>
        <option value="2">Smith</option>                               
    </select>
   Address: <input name="add" type="text" value="">
   Mobile:  <input name="mobile" type="text" value="">
    EMail-Id:<input name="email" type="text" value="">
 </form>

This is JS Code:- 这是JS代码:-

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#customer_id").change(function() {
                var id = $(this).val();
                var dataString = 'cid='+id;
                //alert(dataString);
                $.ajax({
                    url: 'dataAjax.php',
                    type: 'post',
                    data: dataString,
                    success: function(response) {

                      // Parse the jSON that is returned
                        var Vals    =   JSON.stringify(response);
                        // These are the inputs that will populate
                        $("input[name='add']").val(Vals.add);
                        $("input[name='mobile']").val(Vals.mobile);
                        $("input[name='email']").val(Vals.email);
                         }
            });
        });
    });
</script>

This is other PHP File Code:- 这是其他PHP文件代码:-

<?php
// This is where you would do any database call
if(!empty($_POST)) {
    // Send back a jSON array via echo
    echo json_encode(array("add"=>'India',"mobile"=>'1234567890','email'=>'demo@demo.com'));

}
?>

Once the product is selected execute ajax request and get data from the database with respective of the selected product. 一旦选择了产品,就执行ajax请求,并从数据库中获取所选产品的相关数据。

JAVASCRIPT CODE : JAVASCRIPT代码:

$('#products').change(function(){
   $.ajax({
     url: 'your php code page name which returns product details.php?product_id'+$('#products').val(),
     type: 'post',
     success: function(response) {
      var obj    =   JSON.parse(response);
      $("#hsn_sac").val(obj.hsn_sac); // hsn or sac values
      $("#qty_in_hand").val(obj.qty_in_hand); // qty_in_hand values
     }
}});

}); });

PHP CODE : PHP代码:

<?php 
  // first connect database and then fire the query to get product details  
   $connection = mysql_connect("your database host name","database username","database password","database name");
   $result = mysql_query("your query");  
   while ($row = mysql_fetch_array($result)) {  
     // get data and store in array 
   }  
   mysql_close($connection);  // close connection
   echo json_encode(database result);  // encode database result so that it will available to ajax request and assign back to view
   exit;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM