简体   繁体   English

获取表的值并使用jQuery插入数据库

[英]Get value of table and insert into database using jQuery

I am working in codeigniter. 我在codeigniter中工作。 I have created one table and its html is like this: 我创建了一个表,其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>

Now I want to get the value of td so I have written jQuery like this: 现在,我想获取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>

And I got value of it. 我得到了它的价值。 Now i want to insert these all value into database so what code should i have to write? 现在我想将所有这些值插入数据库,所以我应该写什么代码?

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

this ajax will call your function in controller.. in your function call a model function to insert the values to db. 此ajax将在控制器中调用您的函数。在您的函数中调用模型函数以将值插入db。

Refer - AJAX 参考-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>

this will do the work 这会做的工作

you can do so using AJAX. 您可以使用AJAX进行操作。

Create a webpage to accept inputs as parameters and save them into database. 创建一个网页以接受输入作为参数并将其保存到数据库中。 And study this link: http://www.w3schools.com/jquery/jquery_ajax_get_post.asp 并研究此链接: http : //www.w3schools.com/jquery/jquery_ajax_get_post.asp

code to do so: 这样做的代码:

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

For example: if you created a webpage named "savedata.php" that takes inputs as parameters and saves into database. 例如:如果您创建了一个名为“ savedata.php”的网页,该网页将输入作为参数并保存到数据库中。

Then 然后

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

[root_path] is the root path of you webpage and [parameters] is "&" separated parameters. [root_path]是您网页的根路径,[parameters]是由“&”分隔的参数。

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

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