使用 php 保存 SlickGrid 中的更改

发布于 2024-11-18 15:49:54 字数 476 浏览 8 评论 0原文

我设置了一个 SlickGrid,它正在使用 PHP 从我的数据库读取数据,当我尝试将数据保存回我的数据库时,我的问题出现了,我尝试使用 JSON 给我一个数组,然后我可以使用它写回数据库,我看到这个线程解释了这一点:

在 SlickGrid 中保存更改

所以我具有隐藏的表单元素在我的代码中,我使用 JSON 对数据变量进行编码,将其分配给表单上的数据隐藏输入,此表单发布到名为 save_price.php 的页面,问题是当我 print_r 或 var_dump 数据变量时,我得到 null 作为输出,我认为这可能与我如何使用 PHP 将内容添加到数据变量有关,或者我正在做一些明显错误的事情,希望你能看到问题是什么,网上没有大量关于这方面的文档使用 PHP 检索/保存到数据库,所以我在这方面有点陷入困境,这是我的代码:

I have a SlickGrid set up, it is reading data from my database with PHP, my problem is arising when i try to save the data back to my database, I am trying to use JSON to give me an array that I can then use to write back to the database, i have see this thread explaining this:

Saving changes in SlickGrid

So I have the hidden form element in my code, and am using JSON to encode the data variable, the assign it to the data hidden input on the form, this form posts to a page called save_price.php, the trouble is when I print_r, or var_dump the data variable, I get null as an output, I think it might be something to do with how I am using PHP to add the content into the data variable, either that or I am doing something really obviously wrong, hopefully you can see what the problem is, there isn't a great deal of documentation online about retrieving/saving to a db with PHP, so I'm kinda stuck banging my head against the wall on this one, here's my code:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-11-25 15:49:54

好吧,我发现了问题,以防万一有人努力让这一切正常工作,这是工作代码,它从数据库获取数据,然后将更改后的数据发送到另一个页面进行处理,它需要一些改进,一旦我全部实现,这就会发生:

<?php 
include("includes/check_session.php");
require_once('includes/functions.php');
require_once('includes/config.php');

$data = '';
$i = 0;

$query = "
    SELECT * FROM `prices`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $data .= '
        data['.$i.'] = {
            id: "'.$row['id'].'",
            title: "'.$row['title'].'",
            duration: "'.$row['duration'].'",
            percentComplete: "'.$row['percentComplete'].'",
            start: "'.$row['start'].'",
            finish: "'.$row['finish'].'",
            effortDriven: "'.$row['effortDriven'].'"
        };
    ';

    $i++;

echo $data;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset=utf-8>

    <?php // include("includes/cms_head_scripts.php"); ?>
    <link rel="stylesheet" href="css/slick.grid.css" type="text/css" media="screen" charset="utf-8" />
    <link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
    <link rel="stylesheet" href="css/examples.css" type="text/css" media="screen" charset="utf-8" />
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script language="javascript" src="js/jquery.json.js"></script>

</head>
<body>
    <div id="content_cont">

        <div id="main">

                <div style="position:relative">
                    <div style="width:600px;">
                        <div id="myGrid" style="width:100%;height:500px;"></div>
                    </div>
                </div>  



            pricing

        </div><!-- #main -->

    </div><!-- #content_cont -->

        <script src="lib/firebugx.js"></script>


        <script src="lib/jquery-ui-1.8.5.custom.min.js"></script>
        <script src="lib/jquery.event.drag-2.0.min.js"></script>

        <script src="slick.core.js"></script>
        <script src="plugins/slick.cellrangeselector.js"></script>
        <script src="plugins/slick.cellselectionmodel.js"></script>
        <script src="slick.editors.js"></script>
        <script src="slick.grid.js"></script>


        <script type="text/javascript">
            var grid;
            var data = [];
            var columns = [
                {id:"title", name:"Title", field:"title", editor:TextCellEditor},
                {id:"duration", name:"Duration", field:"duration", editor:TextCellEditor},
                {id:"%", name:"% Complete", field:"percentComplete", editor:TextCellEditor},
                {id:"start", name:"Start", field:"start", editor:TextCellEditor},
                {id:"finish", name:"Finish", field:"finish", editor:TextCellEditor},
                {id:"effort-driven", name:"Effort Driven", field:"effortDriven", editor:TextCellEditor}
            ];

            var options = {
                    editable: true,
                    enableCellNavigation: true,
                    asyncEditorLoading: false,
                    autoEdit: true
                };

            $(function() {

                <?php echo $data ?>
                grid = new Slick.Grid($("#myGrid"), data, columns, options);

            })



        </script>

<form method="POST" action="save_price.php">
    <input type="submit" value="Save">
    <input type="hidden" name="data" value="">
</form>
<script type="text/javascript">
  $(function() {
    $("form").submit(
      function() {
        $("input[name='data']").val($.JSON.encode(data));

      }
    );
  });
</script>

</body>
</html>

Ok so I found the problem, just incase anyone is struggling to get this all to work, here is the working code, it gets data from a database, then sends the changed data to another page for processing, it nees a little bit of refinements, that will happen once I've got it all implemented:

<?php 
include("includes/check_session.php");
require_once('includes/functions.php');
require_once('includes/config.php');

$data = '';
$i = 0;

$query = "
    SELECT * FROM `prices`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $data .= '
        data['.$i.'] = {
            id: "'.$row['id'].'",
            title: "'.$row['title'].'",
            duration: "'.$row['duration'].'",
            percentComplete: "'.$row['percentComplete'].'",
            start: "'.$row['start'].'",
            finish: "'.$row['finish'].'",
            effortDriven: "'.$row['effortDriven'].'"
        };
    ';

    $i++;

echo $data;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset=utf-8>

    <?php // include("includes/cms_head_scripts.php"); ?>
    <link rel="stylesheet" href="css/slick.grid.css" type="text/css" media="screen" charset="utf-8" />
    <link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
    <link rel="stylesheet" href="css/examples.css" type="text/css" media="screen" charset="utf-8" />
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script language="javascript" src="js/jquery.json.js"></script>

</head>
<body>
    <div id="content_cont">

        <div id="main">

                <div style="position:relative">
                    <div style="width:600px;">
                        <div id="myGrid" style="width:100%;height:500px;"></div>
                    </div>
                </div>  



            pricing

        </div><!-- #main -->

    </div><!-- #content_cont -->

        <script src="lib/firebugx.js"></script>


        <script src="lib/jquery-ui-1.8.5.custom.min.js"></script>
        <script src="lib/jquery.event.drag-2.0.min.js"></script>

        <script src="slick.core.js"></script>
        <script src="plugins/slick.cellrangeselector.js"></script>
        <script src="plugins/slick.cellselectionmodel.js"></script>
        <script src="slick.editors.js"></script>
        <script src="slick.grid.js"></script>


        <script type="text/javascript">
            var grid;
            var data = [];
            var columns = [
                {id:"title", name:"Title", field:"title", editor:TextCellEditor},
                {id:"duration", name:"Duration", field:"duration", editor:TextCellEditor},
                {id:"%", name:"% Complete", field:"percentComplete", editor:TextCellEditor},
                {id:"start", name:"Start", field:"start", editor:TextCellEditor},
                {id:"finish", name:"Finish", field:"finish", editor:TextCellEditor},
                {id:"effort-driven", name:"Effort Driven", field:"effortDriven", editor:TextCellEditor}
            ];

            var options = {
                    editable: true,
                    enableCellNavigation: true,
                    asyncEditorLoading: false,
                    autoEdit: true
                };

            $(function() {

                <?php echo $data ?>
                grid = new Slick.Grid($("#myGrid"), data, columns, options);

            })



        </script>

<form method="POST" action="save_price.php">
    <input type="submit" value="Save">
    <input type="hidden" name="data" value="">
</form>
<script type="text/javascript">
  $(function() {
    $("form").submit(
      function() {
        $("input[name='data']").val($.JSON.encode(data));

      }
    );
  });
</script>

</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文