简体   繁体   English

在从mysql数据库检索的模式中的字段中显示数据

[英]To display data in the fields in the modal retrieved from the mysql database

Friends I am submitting the form on clicking the submit button and simultaneously i am displaying the just submitted data in the modal.So as soon as i hit the submit button ,the data gets submitted and a modal appears with the data just submitted.Everything is working fine with my code but the only problem that the data does not gets displayed in the modal. 朋友,我在单击“提交”按钮时提交表单,同时我在模态中显示刚提交的数据。因此,一旦我单击“提交”按钮,数据就被提交,并且模态随刚提交的数据一起出现。我的代码可以正常工作,但是唯一的问题是数据不会在模式中显示。 Here is the code for submitting the 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 is modal which gets displayed when i 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` FROM tran ORDER BY id DESC limit 1";
                  $result = mysqli_query($conn,$sql);
                  $rows = mysqli_fetch_assoc($result);
                  $date = $rows['date'];
                  echo $date;

                  ?>" readonly /> //this field does not show anything and none of the fields show any 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 does not show the date value from database and v_nopreview field also does not show anything. 日期字段不显示数据库中的日期值,而v_nopreview字段也不显示任何内容。 I have tried to give as much details as possible but in case if you need anything then let me know.Please let me know why the data is not being displayed inside the input fields in modal. 我尝试提供尽可能多的详细信息,但是如果您需要任何其他信息,请告诉我。请让我知道为什么数据没有以模式显示在输入字段中。 Thanks in advance. 提前致谢。 Edited part Here is the data1_post.php code- 编辑部分这是data1_post.php代码-

<?php
include('db.php');
$sql = "INSERT INTO `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 I understand, you expect execution of php code each time after js event. 据我了解,您希望每次js事件后都执行php代码。 But PHP is a server-side language, it interprets only once, when you request the pages. 但是PHP是一种服务器端语言,当您请求页面时,它仅解释一次。

So your php code will load some content form DB after refreshing, then you clear input's value before displaying model and as it can't be executed again - you see empty modal. 因此,您的php代码将在刷新后从数据库加载一些内容,然后在显示模型之前清除输入的值,并且由于无法再次执行-您会看到空的模态。 I recommend you to return saved data from "data1_post.php" and than process it in success callback 我建议您从“ data1_post.php”返回保存的数据,然后在成功回调中处理它

UPDATE 更新

if you want to present saved data, your php code would look like the following 如果要显示保存的数据,则您的php代码如下所示

include('db.php');

$response = ["success" => false];
$sql = "INSERT INTO `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` FROM tran ORDER BY 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 和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");
        }
    });
});

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

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