简体   繁体   English

使用ajax和dataType:“json”从数据库中获取数据以html形式显示

[英]Fetching data from database using ajax and dataType:“json” to display in html form

I have been trying this over night but I cant seem to make it work.我一夜之间一直在尝试这个,但我似乎无法让它发挥作用。 I tired various methods to let the data values retrieved from the database to be displayed onto the form but none seem to work.我厌倦了各种方法来让从数据库中检索到的数据值显示在表单上,​​但似乎都不起作用。 When users click on the table row they should be able to see the data values they inserted for that particular row.当用户单击表格行时,他们应该能够看到他们为该特定行插入的数据值。 This is basically a database where users can store vendor details and edit them when needed.这基本上是一个数据库,用户可以在其中存储供应商详细信息并在需要时对其进行编辑。 Right now I am working on editing part.现在我正在编辑部分。

This is my html form code Where the data is supposed to be displayed.这是我的 html 表单代码应该显示数据的位置。

 <div id="VendorTable" class="VendorTable">
 <div id="vendorformedit" class="vendorformedit" >
 <i class="fa fa-expand"></i>
 <form id="form" name="form" method="POST" action="updatevendorlist.php" >
    <div id="formparttwo" >
    <h3>Other Information</h3><hr>

    <label>Category:</label>
    <input type="text" name="category" id="category" /><br/>
    <label>Notes:</label>
    <input type="text" name="remarks" id="remarks" /><br/><br/>

    <h3>2nd Contact Person</h3><hr>
    <label>Contact Person 2:</label>
    <input type="text" name="contactPerson2" id="contactPerson2"><br/>
    <label>Contact Number 2:</label>
    <input type="text" name="contact2" id="contact2"/><br/>

    <button type="submit" form="form" value="Submit"><i class="fa fa-plus-square fa-3x" aria-hidden="true"></i></button>
    </div> 

    <div id="formpartone">
    <h3>Vendor Details</h3><hr>

    <label>Vendor Name:</label>
    <input type="text" name="vendorName" id="vendorName"/><br/>
    <label>Vendor Address:</label>
    <input type="text" name="vendorAddress" id="vendorAddress"/><br/>
    <label>Vendor E-mail:</label>
    <input type="email" name="vendorEmail" id="vendorEmail"/><br/><br/>

    <h3>1st Contact Person</h3><hr>
    <label>Contact Person 1:</label>
    <input type="text" name="contactPerson1" id="contactPerson1" ><br/>
    <label>Contact Number 1:</label>
    <input type="text" name="contact1" id="contact1" /><br/>
    </div>
    </form>
</div><!--End vendorformedit Form-->
</div><!--End VendorTable Table-->

I need the data retrieved from the database to be displayed in the input textarea.我需要从数据库中检索到的数据显示在输入文本区域中。 The ultimate aim is for users to edit the data.最终目的是让用户编辑数据。

This is the javascript code using Ajax and dataType:Jason这是使用 Ajax 和 dataType:Jason 的 javascript 代码

     $(document).on("click", ".individualrow", function(){ 
       var id=$(this).attr("vendor_id");  
       if(confirm("Are you sure you want to retrieve this?"))  
       {  confirm(id);
            $.ajax({ 
                method:"POST",   
                url:"retrievevendorlist.php",  
                data:{id:id},  
                dataType:"json",  
                success:function(data){ 
                 $('#category').val(data.category);  
                 $('#remarks').val(data.remarks);  
                 $('#contactPerson2').val(data.contactPerson2);  
                 $('#contact2').val(data.contact2);  
                 $('#vendorName').val(data.vendorName);  
                 $('#vendorAddress').val(data.vendorAddress);  
                 $('#vendorEmail').val(data.vendorEmail);  
                 $('#contactPerson1').val(data.contactPerson1);
                 $('#contact1').val(data.contact1); 

                 }  
            });  
            $(".individualrow").addClass("form_one");
            $(".vendorformedit").addClass("hide_form");
       }   
  });  

I am trying to retrieve the data from the database using the row id.我正在尝试使用行 ID 从数据库中检索数据。 I believe the code is working successfully in retrieving the data from the retrievevendorlist.php.我相信代码在从retrievevendorlist.php 中检索数据方面工作成功。 However it is not displaying in the html form.但是它没有以 html 形式显示。

    <?php
    include("../LoginRegistrationSystem/connect.php");
    include("../LoginRegistrationSystem/functions.php");
    // attempt insert query execution
    if(isset($_POST['id'])){
    $sql = "SELECT * FROM vendor WHERE vendor_id = '".$_POST['id']."'";
    $result = mysqli_query($con, $sql);
    $row = mysqli_fetch_array($result);  
    echo json_encode($row);
    ?>

I am not sure where is the mistake.我不确定错误在哪里。 Kindly help me review and tell me where I am going wrong.请帮我审查并告诉我哪里出错了。 Thank you in advance提前谢谢你

I notice you're retrieving the data from the database as an array and it might be getting encoded to a json array.我注意到您从数据库中检索数据作为一个数组,它可能被编码为一个 json 数组。 Try accessing the data on the client side as follows尝试访问客户端的数据如下

data[0].category

I managed to solve it.我设法解决了它。 Thanks to those who helped.感谢那些提供帮助的人。

I had to spell out all the data needed from database and also I used accoc instead of array.我必须从数据库中拼出所有需要的数据,而且我使用了 accoc 而不是数组。

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

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