php - Loading detail from database with ajax -
i'm trying simple yet can't make work , have no idea why. i'm trying load details on bootstrap modal windows database can edit them. button looks like
<button onclick="getuserdetails('.$row['row_id'].')" class="btn btn-warning">update</button>
then php part should load data
include("../../misc/database.inc.php"); error_reporting(e_all); ini_set('display_errors', 1); $pdo = database::connect(); $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); if(isset($_post['row_id']) && isset($_post['row_id']) != "") { $row_id = $_post['row_id']; $value = $pdo->prepare('select row_id, row_content, row_email excel_table row_id = ?'); $value->bindparam(1, $id, pdo::param_int); $value->execute(); $response = array(); if($value->rowcount() > 0){ while ($rs = $value->fetch(pdo::fetch_assoc)) { $response = $row; } } else { $response['status'] = 200; $response['message'] = "data not found!"; } echo json_encode($response); //var_dump($_post['row_id']); // return correct id }
on console->network see correct response
{"row_id":"1","row_content":"asd","row_email":"sad"}
here js part should load data
function getuserdetails(row_id) { $("#row_id").val(row_id); $.post("ajax/readuserdetails.php", { row_id: row_id }, function (data, status) { // parse json data var excel_table = json.parse(data); // assing existing values modal popup fields $("#row_content").val(excel_table.row_content); $("#row_email").val(excel_table.row_email); } ); // open modal popup $("#update_user_modal").modal("show"); }
and modal
<div class="modal fade" id="update_user_modal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel">update</h4> </div> <div class="modal-body"> <div class="form-group"> <label for="row_content">desc</label> <input type="text" id="row_content" placeholder="Описание" class="form-control"/> </div> <div class="form-group"> <label for="row_email">email</label> <input type="text" id="row_email" placeholder="email" class="form-control"/> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">cancel</button> <button type="button" class="btn btn-primary" onclick="updateuserdetails()" >save changes</button> <input type="hidden" id="row_id"> </div> </div> </div> </div>
can tell me why modal isn't populated data?
well guess need show modal after ajax asynchronous call end.
function getuserdetails(row_id) { $("#row_id").val(row_id); $.post("ajax/readuserdetails.php", { row_id: row_id }, function (data, status) { // parse json data var excel_table = json.parse(data); // assing existing values modal popup fields //add console.log make sure have value console.log('row_content: ' + excel_table.row_content); $("#row_content").val(excel_table.row_content); $("#row_email").val(excel_table.row_email); // add make sure update correctly. console.log('#row_content: ' + $("#row_content").val()); // open modal popup after update $("#update_user_modal").modal("show"); } ); }
Comments
Post a Comment