javascript - To display data in the fields in the modal retrieved from the mysql database -


friends submitting form on clicking submit button , simultaneously displaying submitted data in modal.so hit submit button ,the data gets submitted , modal appears data submitted.everything working fine code problem data not gets displayed in modal. here code submitting form-

$("#savep").click(function(e){       e.preventdefault();         formdata = $('form.pform').serialize() + '&'          + encodeuri($(this).attr('name'))         + '='         + encodeuri($(this).attr('value'));               $.ajax({                type: "post",                url: "data1_post.php",                data: formdata,                success: function(msg){                 $('input[type="text"], textarea').val('');                  $('#entrysavedmodal').modal('show');                   },                error: function(){                 alert("failure");                }            });      }); 

here modal gets displayed when click on submit button-

<div id="entrysavedmodal" class="modal fade" role="dialog">   <div class="modal-dialog" style="width:1000px;">      <!-- modal content-->     <div class="modal-content">       <div class="modal-header">         <button type="button" class="close" data-dismiss="modal">&times;</button>         <h4 class="modal-title"><span class="glyphicon glyphicon-plus"></span> saved entry</h4>       </div>       <div class="modal-body">         <form  class="form-horizontal savedform" id="savedform">     <div class="form-group">       <label class="control-label col-xs-2">date:</label>       <div class="col-xs-4">         <input type="text" class="form-control" id="datepreview" name="datepreview"             value = "<?php                   include('db.php');                   $sql = "select `date` tran order id desc limit 1";                   $result = mysqli_query($conn,$sql);                   $rows = mysqli_fetch_assoc($result);                   $date = $rows['date'];                   echo $date;                    ?>" readonly /> //this field not show , none of fields show data.       </div>        <label class="control-label col-xs-2">v_no:</label>       <div class="col-xs-4">         <input type="text" class="form-control" id="v_nopreview" name="v_no"  autocomplete="off" readonly /> //same problem        </div>      </div>    </form>       </div>       <div class="modal-footer">          <button type="button" class="btn btn-info" id="print"  name="print" >print</button>          <button type="button" class="btn btn-danger" data-dismiss="modal">close</button>       </div>    </form>   </div> </div> </div> </div> 

date field not show date value database , v_nopreview field not show anything. have tried give details possible in case if need let me know.please let me know why data not being displayed inside input fields in modal. in advance. edited part here data1_post.php code-

<?php include('db.php'); $sql = "insert `table1` (`date`, `v_no`, `name`,`narration`,`stk_y_n`)          values ( '".$_post['date']."','".$_post['v_no']."', '".$_post['user']."','".$_post['narration']."', 'no')";  if ($conn->query($sql) === true){ echo "saved"; } else { echo "not saved"; } ?> 

as understand, expect execution of php code each time after js event. php server-side language, interprets once, when request pages.

so php code load content form db after refreshing, clear input's value before displaying model , can't executed again - see empty modal. recommend return saved data "data1_post.php" , process in success callback

update

if want present saved data, php code following

include('db.php');  $response = ["success" => false]; $sql = "insert `table1` (`date`, `v_no`, `name`,`narration`,`stk_y_n`)         values ( '".$_post['date']."','".$_post['v_no']."', '".$_post['user']."','".$_post['narration']."', 'no')"; if ($conn->query($sql) === true){     $sql = "select `date` tran order id desc limit 1";     $result = mysqli_query($conn,$sql);     $rows = mysqli_fetch_assoc($result);     $response = ["success" => true, "date" => $rows['date']]; }  header('content-type: application/json'); echo json_encode($response);   

and js

$("#savep").click(function(e){     e.preventdefault();      formdata = $('form.pform').serialize() + '&'         + encodeuri($(this).attr('name'))         + '='         + encodeuri($(this).attr('value'));      $.ajax({         type: "post",         url: "data1_post.php",         data: formdata,         success: function(response){             $('input[type="text"], textarea').val('');              if (response.success) {                 $('#datepreview').val(response.date);                 $('#entrysavedmodal').modal('show');             } else {                 alert("failure");             }         },         error: function () {             alert("failure");         }     }); }); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -