【问题标题】:To display data in the fields in the modal retrieved from the mysql database在从 mysql 数据库检索的模式中的字段中显示数据
【发布时间】:2017-01-31 01:13:45
【问题描述】:

朋友们,我在单击提交按钮时提交表单,同时我在模态中显示刚刚提交的数据。所以,只要我点击提交按钮,数据就会被提交,并且会出现一个模态,其中包含刚刚提交的数据.我的代码一切正常,但唯一的问题是数据没有显示在模式中。 这是提交表单的代码-

$("#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");
               }
           });
     });

这是我点击提交按钮时显示的模式-

<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>

日期字段不显示数据库中的日期值,v_nopreview 字段也不显示任何内容。 我试图提供尽可能多的细节,但如果您需要任何东西,请告诉我。请让我知道为什么数据没有显示在模态输入字段中。 提前致谢。 编辑部分 这是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";
}
?>

【问题讨论】:

    标签: javascript php jquery mysql twitter-bootstrap


    【解决方案1】:

    据我了解,您希望每次在 js 事件之后执行 php 代码。但是 PHP 是一种服务器端语言,它只解释一次,当您请求页面时。

    因此,您的 php 代码将在刷新后加载一些内容表单 DB,然后在显示模型之前清除输入的值,因为它无法再次执行 - 您会看到空模式。 我建议您从“data1_post.php”返回保存的数据,然后在成功回调中处理它

    更新

    如果你想展示保存的数据,你的 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);  
    

    和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");
            }
        });
    });
    

    【讨论】:

    • 哦,我知道我不应该清除输入,是的,如果我删除了清除输入部分,那么它绝对可以正常工作,但我也想清除表单中的输入,因为表单已提交,那你有什么建议?
    • @payal_suthar 能否请您将 data1_post.php 的内容添加到问题中,我会为此写一个完整的解决方案
    • 我的意思是我已经尝试过返回的东西,然后在成功回调中处理数据,但效果不佳
    • 哦,我将对我的帖子进行编辑并添加 data1_post.php 代码
    • 实际上我想提交数据,提交时我想预览提交的数据。
    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多