简体   繁体   English

如何获取HTML表格的值并分配到选择和输入文本字段中?

[英]How to Get HTML Tables Values and Asign into Select and Input Text Fields?

I hope you all of fine, I've been facing problem to get data from HTML tables into Select Box. 希望您一切都好,我一直面临着将HTML表中的数据放入“选择框”的问题。

I'm using Php MySQLi and Ajax Using Jquery to Perform the operations such as Insert, Update, Delete and View. 我正在使用PHP MySQLi和Ajax使用Jquery执行诸如插入,更新,删除和查看之类的操作。 I completed two processes such as Insert, and Display Data on the Tables, But I can't getting HTML tables and to assign in HTML Tags Select Box. 我完成了两个过程,例如“插入”和“在表上显示数据”,但是我无法获取HTML表,也无法在HTML标记中选择框。

I have used Foreign Key References to Make Relationship between Category and Also Product Tables. 我已经使用外键引用来建立类别和产品表之间的关系。 I want to get Category_Name from product table and assign a selected category_name into HTML Select Box. 我想从产品表中获取Category_Name并将选定的category_name分配给HTML Select Box。

Here is Php Function 这是PHP功能

  // Get Product Data function get_product_record() { global $con; $Pro_Update_Id = $_POST['Pro_Update_ID']; $query = "select * from product where Product_ID='$Pro_Update_Id'"; $result = mysqli_query($con,$query); while($row=mysqli_fetch_assoc($result)) { $data = ""; $data[0]=$row['Product_ID']; $data[1]=$row['Category_ID']; $data[2]=$row['Product_Name']; $data[3]=$row['Product_Qty']; $data[4]=$row['Price']; $data[5]=$row['Description']; } echo json_encode($data); } 

Here is jQuery File 这是jQuery文件

 function get_pro_record() { $(document).on('click','#p_btn_edit',function() { var GetPID = $(this).attr('data-id'); $.ajax( { url: 'includes/products/get_pro_record.php', method: 'post', data:{Pro_Update_ID:GetPID}, dataType: 'JSON', success:function(data) { $('#product_id').val(data[0]); $('#cat_up_id').val(data[1]); $('#product_up_name').val(data[2]); $('#up_Qty').val(data[3]); $('#up_Price').val(data[4]); $('#up_description').val(data[5]); $('#products_update').modal('show'); } }) }) } 

**Here is HTML Form ** **这里是HTML表格**

 <form> <input type="hidden" id="product_id" class="form-control my-2" placeholder=" Product Name"> <select name="cat_up_id"></select> <input type="text" id="product_up_name" class="form-control my-2" placeholder=" Product Name"> <input type="text" id="up_Qty" class="form-control my-2" placeholder=" QTY"> <input type="text" id="up_Price" class="form-control my-2" placeholder=" Price "> <textarea id="up_description" class="form-control" placeholder="Description"></textarea> </form> 

I'm receiving complete data into console screen, but I can't see category_name I'm receiving category_id. 我正在控制台屏幕中接收完整的数据,但是看不到category_name我正在接收category_id。 So Please tell me What Can I do to solve the problem. 因此,请告诉我该怎么做才能解决问题。

Here is Output of the Project in Console Project Output in Console 这是控制台中 项目的输出 控制台中 项目的输出

First you need to query database from both Product and Category table using Inner Join 首先,您需要使用内部联接从“产品”和“类别”表中查询数据库

$query = "select a.*, b.Category_Name from product a, category b where a.Category_Id = 
          b.Category_Id and Product_ID='$Pro_Update_Id'";

then you need to add $data with one more element for Category Name 那么您需要为类别名称添加$ data和一个元素

 $data[6]=$row['Category_Name'];

then you need to add options item to select 那么您需要添加选项项以进行选择

 $('#cat_up_id').html('<option>' + data[6] + '</option>');    

and, replace attribute name="cat_up_id" with id="cat_up_id" in select tag 并且,在选择标记中将属性名称=“ cat_up_id”替换为id =“ cat_up_id”

Sonal Bonkar是正确的,您需要在这里进行表联接。在这种情况下,您需要将值作为ID来存储在表中

$('#cat_up_id').html('<option value="'+ data[1] +'">' + data[6] + '</option>'); 

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

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