【问题标题】:Get value of table and insert into database using jQuery使用jQuery获取表的值并插入数据库
【发布时间】:2015-09-13 00:49:23
【问题描述】:

我正在使用 codeigniter。我创建了一个表,它的 html 是这样的:

<table>
   <tr>
     <td>
        <center><label style="font-weight:normal" class="t_date"><?php echo date('Y-m-d'); ?></label></center>
     </td>
     <td>
        <center><label style="font-weight:normal" class="b_id"><?php echo $result[0]->branch_id; ?></label></center>
     </td>
   </tr>
   <tr>
      <td></td>
      <td><input type="button" onClick="window.print();" value="Print" name="print" id="print"/></td>
   </tr>
</table>

现在我想获取 td 的值,所以我写了这样的 jQuery:

        <script>
            jQuery(document).ready(function(){
                jQuery("#print").click(function(){
                    //alert(123);

                    var to_date = jQuery(".t_date").text();
                    var to_date = jQuery(".b_id").text();

                    });
                });
        </script>

我得到了它的价值。现在我想将这些所有值插入数据库,那么我应该编写什么代码?

【问题讨论】:

  • 将该值存储在隐藏并提交表单中。
  • 您可以在jquery中使用post提交下一页的数据,并将其保存到下一页的数据库中。
  • @priya786 所以你说使用 ajax 对吗?
  • 是的,你也可以像 $.post('yourpagename','values1:value1',function(){}) 这样在 vlaue 中使用它,你可以发送变量 ok
  • @priya786 哦,谢谢。让我试试。

标签: php jquery mysql codeigniter


【解决方案1】:
jQuery(document).ready(function () {
        jQuery("#print").click(function () {
            alert(123);
            var to_date = jQuery(".t_date").text();
            var to_id = jQuery(".b_id").text();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>index.php/controller/function",
                data: {to_date: to_date, to_id: to_id},
                success: function (html) //we're calling the response json array 'permissions'
                {
                    // action
                }
            });
        });
    });

此 ajax 将在控制器中调用您的函数。在您的函数中调用模型函数以将值插入到 db。

【讨论】:

    【解决方案2】:

    您可以使用 AJAX 来做到这一点。

    创建一个网页以接受输入作为参数并将它们保存到数据库中。 并研究这个链接:http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

    这样做的代码:

    $.get("web page address with parameters ", function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
    

    例如:如果您创建了一个名为“savedata.php”的网页,它将输入作为参数并保存到数据库中。

    然后

     $.get("[root_path]/savedata.php?[paramaters]", function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
    

    [root_path]是你网页的根路径,[parameters]是“&”分隔的参数。

    【讨论】:

      【解决方案3】:

      参考 - AJAX

      <script>
      jQuery(document).ready(function () {
          jQuery("#print").click(function () {
              $.ajax({
                  url: '/path/to/file',
                  type: 'default GET (Other values: POST)',
                  dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                  data: {
                      t_date: jQuery(".t_date").text(),
                      b_id: jQuery(".b_id").text(),
                  },
                  success: function (response) {
                      alert(response);
                  }
              });
          });
      });    
      </script>
      

      这样就可以了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-16
        • 2019-03-05
        • 2017-05-16
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多