无法使用 jQuery Ajax Bootstrap 模型从数据库中获取更新数据的值 - json 数据

杰西约瑟夫

安慰我的主要问题是我的模式正在显示但警报显示 [object Object]。我有四个表,如stud、country_master_academic、master_city 和master_state。当我单击编辑时,会出现模态,但从数据库中获取的数据没有显示在其中。

home.php 页面中的 jQuery

$(document).ready(function(){
             $(document).on('click', '.edit_data', function(event){
               var stud_no = $(this).attr("id");
               $.ajax({  
                    url:"update.php",  
                    method:"POST",  
                    data:{stud_no:stud_no},  
                    dataType:"json",  
                    success:function(data){  
                    console.log(data);
                    $('#name').val(data.name);
                    $('#mob_no').val(data.mob_no);
                    $('#dob').val(data.dob);
                    $('#add').val(data.add);
                    $('#photo').val(data.photo);
                    $('#gender').val(data.gender);
                    $('#country').val(data.country);
                    $('#state').val(data.state);
                    $('#city').val(data.city);
                    $('#stud_no').val(data.stud_no);
                    $('#update_data_modal').modal('show');  


                    },  
                    });     
                });  
    });

home.php 页面中的更新模式

<div class="container">
<div class="modal fade" id="update_data_modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-heading" style="margin-top:30px;text-align:center">
                <button class="close" data-dismiss="modal" style="margin-right:20px;font-weight:bold;">x</button>
                <h4 class="modal-title"><span class="glyphicon glyphicon-edit"></span>Update Student</h4>
            </div>
            <div class="modal-body">
            <?php
                $img = "images/".trim($vrow["photo"]);
                echo '<img src='.$img.' class="image" style="margin-left:75%;margin-top:5%;width:120px;height:120px;border:2px solid #bbbbbb;border-radius:10px;">';
                ?>
                <br/>
                <input type="file" name="photo"  style="margin-left:70%;">
                <div class="form-group">
                <form class="form-horizontal" name="form" id="form" method="post" action="<?php $_PHP_SELF?>" enctype="multipart/form-data">
                    <label for="name" id="name"><span class="glyphicon glyphicon-user"></span><b> Student Name: </b></label>
                    <input type="text" class="form-control" name="name" id="name" pattern="[a-zA-Z]{3,}" title="Name should only contain letters and atleast 3 letters" required />
                </div>
                <div class="form-group">
                    <label for="no"><span class="glyphicon glyphicon-phone"></span><b> Mobile No: </b></label>
                    <input type="text" class="form-control" name="mob_no" id="mob_no" pattern="[0-9]{10}" title="Mobile number should be of 10 digits" required />
                </div>
                <div class="form-group">    
                    <label for="dob"><span class="glyphicon glyphicon-calendar"></span><b> Birth Date: </b></label>
                    <input type="date" class="form-control" name="dob" id="dob" required />
                </div>
                <div class="form-group">
                    <label for="add"><span class="glyphicon glyphicon-map-marker"></span><b> Address: </b></label>
                    <textarea rows="4" cols="33" class="form-control" name="add" id="add" required></textarea>
                </div>
                <div class="form-group">
                    <label for="photo"><span class="glyphicon glyphicon-camera"></span><b> Photo: </b></label>
                    <input type="file" name="photo" id="photo" required />
                </div>
                <div class="form-group">
                    <label for="gen"><b> Gender: </b></label>
                    <input type="radio" name="gender" id="gender" value="M" required="required">Male
                    <input type="radio" name="gender" id="gender" value="F" required="required">Female
                </div>
                <div class="form-group">
                    <label for="cntry"><span class="glyphicon glyphicon-map-marker"></span><b> Country: </b></label>
                <select name="country" id="country" class="form-control">
                <option value="0">Select</option>
                    <?php 
                    $country="SELECT * from country_master_academic";
                    $res= $conn->query($country);
                    if($res->num_rows>0){
                        while($row=$res->fetch_assoc()){        
                        if($row["country_name"]==$vcountry or $vrow['country'] == $row["country_code"] )
                        {   
                                echo '<option value='.$row["country_code"].' selected>'.$row["country_name"].'</option>';
                        }
                            else
                            {
                                echo '<option value='.$row["country_code"].'>'.$row["country_name"].'</option>';
                            }
                        }
                    }
                ?>
                </select>

                </div>
                <div class="form-group">
                        <label for="state"><span class="glyphicon glyphicon-map-marker"></span><b> State: </b></label>
                <select name="state" id="state" class="form-control">
                <option value="0">Select</option>
                    <?php 
                    $state="SELECT * from master_state";
                    $res= $conn->query($state);
                    if($res->num_rows>0){
                        while($row=$res->fetch_assoc()){        
                        if($row["state_name"]==$vstate or $vrow['state'] == $row["state_code"] )
                        {   
                                echo '<option value='.$row["state_code"].' selected>'.$row["state_name"].'</option>';
                        }
                            else
                            {
                                echo '<option value='.$row["state_code"].'>'.$row["state_name"].'</option>';
                            }
                        }
                    }
                ?>
                </select>
                    </div>
                <div class="form-group">
                <label for="city"><span class="glyphicon glyphicon-map-marker"></span><b> City: </b></label>
                <select name="city" id="city" class="form-control">
                <option value="0">Select</option>
                    <?php 
                    $city="SELECT * from master_city";
                    $res= $conn->query($city);
                    if($res->num_rows>0){
                        while($row=$res->fetch_assoc()){        
                        if($row["city_name"]==$vcity or $vrow['city'] == $row["city_code"] )
                        {   
                                echo '<option value='.$row["city_code"].' selected>'.$row["city_name"].'</option>';
                        }
                            else
                            {
                                echo '<option value='.$row["city_code"].'>'.$row["city_name"].'</option>';
                            }
                        }
                    }
                ?>
                </select>
                </div>

                <div class="form-group">
                <input type="hidden" name="stud_no" id="stud_no" />  
                    <button type="submit" name="update" id="update" class="btn btn-info">Update</button>
                </div>
                </form>
            </div>
            <div class="modal-footer">
            <button class="btn btn-danger" type="button" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div> 

update.php 页面,但我仍在尝试获取数据,因此只编写了用于从数据库中选择记录的代码。

我的编辑按钮保持循环

    echo '<td><button name="edit" style="font-weight:bold;" type="submit" id='.$row["stud_no"].' class="btn btn-warning edit_data" data-target="#update_data_modal" data-toggle="modal"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>';
穆罕默德·比拉尔

如快照所示,您的对象具有数字索引,因此data.photo不会返回任何内容,因为photo您的对象中没有索引

您可以使用 访问图像名称data[5]

同样,您可以获得任何索引值。

喜欢名字而不是使用data.name你需要使用data[1]

所以你的代码应该是这样的:

$('#name').val(data[1]);
$('#mob_no').val(data[2]);
$('#dob').val(data[3]);
$('#add').val(data[4]);
$('#photo').val(data[5]);

您还为标签和输入提供了相同的 ID:

<label for="name" id="name">

您需要从所有标签中删除 id:

<label for="name">

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用 jQuery ajax() 从远程 json 获取数据

无法使用 jQuery 显示来自 AJAX 调用 JSON 数据的数据

如何使用Jquery和Ajax将Json数据发送到数据库中?

如何使用 bootstrap 和 Ajax 根据选定的 ID 从 MySQL 数据库中获取数据?

jQuery ajax使用php json编码获取数据库结果

使用Ajax和jQuery无法在div部分显示JSON数据

使用RGraph轮询Postgres数据库中的数据并使用AJAX / JSON更新

在Laravel中使用Ajax / jQuery从数据库中获取数据

jQuery ajax获取json数据

无法使用JSON从JQuery Ajax发布中获取PHP中的数据

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

使用jquery,ajax,php,mysqli和bootstrap modal更新数据

如何使用 jquery/ajax 将值插入数据库?

如何使用jQuery Ajax显示JSON数据?

如何使用 jQuery AJAX POST JSON 数据

如何使用ajax从jquery数据表中嵌套的json子数组中获取数据

无法使用json jquery获取数据

无法将AJAX调用中的jQuery DataTable与JSON数据绑定

无法使用 AJAX 和 JQuery 更新 CRUD 中的数据

如何使用jQuery Ajax传递表单数据以在数据库中插入数据?

Ajax使用codeigniter更新数据库中的数据

Laravel-使用Ajax从数据库中获取数据

在Codeigniter中使用jQuery Ajax更新数据库中的列

使用jQuery Ajax获取JSON数据不起作用

我在引导模式中使用 jQuery 和 AJAX 从数据库中获取一些数据,但是当我尝试更新某些记录时,它给出了不正确的值

使用AJAX和JSON从数据库中获取和显示数据时,在innerHTML中变得未定义

不刷新页面如何使用ajax / jQuery显示数据库中的值

jQuery Ajax无法使用数据参数发布

使用JSON Jquery的数据库