【问题标题】:Posting to a php file via datatables and displaying result通过数据表发布到 php 文件并显示结果
【发布时间】:2014-07-14 06:18:40
【问题描述】:

您好,我是数据表的新手,但我发现它们非常有趣。

我使用数据表创建了这个网页:

http://www.berlitzmalta.com/ELTONTEST/examples/basic_init/zero_configuration.html

这通过 php / mysql 收集数据。一切都很好,但是我发现很难做到以下几点:

当用户单击查看摘要按钮时,我需要浏览器打开一个新页面或新选项卡并显示数据内容 [5]。这是我下面的代码:

<html> <head>   <meta charset="utf-8">  <link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">    <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">

    <title>Department of English Linguistics Theses</title>     <link rel="stylesheet" type="text/css" href="../../media/css/jquery.dataTables.css">    <link rel="stylesheet" type="text/css" href="../resources/syntax/shCore.css">   <link rel="stylesheet" type="text/css" href="../resources/demo.css">    <style type="text/css" class="init">

    </style>    <script type="text/javascript" language="javascript" src="../../media/js/jquery.js"></script>   <script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js"></script>    <script type="text/javascript" language="javascript" src="../resources/syntax/shCore.js"></script>  <script type="text/javascript" language="javascript" src="../resources/demo.js"></script>   <script type="text/javascript" language="javascript" class="init">

$(document).ready(function() {  var table = $('#example').DataTable( {      //"ajax": "data/arrays.txt",        "columnDefs": [ {           "targets": -1,          "data": null,           "defaultContent": "<button>View Abstract</button>"      } ]     } );

$('#example tbody').on( 'click', 'button', function () {        var data = table.row( $(this).parents('tr') ).data();       $.post( "abstract.php", { name: data[5] } );        window.open ("abstract.php");       } ); } );

    </script> </head>

            <table id="example" class="display" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Author</th>
                        <th>Title</th>
                        <th>Degree</th>
                        <th>Year</th>
                        <th>Abstract</th>
                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>ID</th>
                        <th>Author</th>
                        <th>Title</th>
                        <th>Degree</th>
                        <th>Year</th>
                        <th>Abstract</th>
                    </tr>
                </tfoot>

                <tbody>

                <?php $con = mysql_connect('192.168.10.223',"user","passxxxx");


if (!$con)   {   die('Could not connect: ' . mysql_error());   }

mysql_select_db("eltontest", $con);

$result = mysql_query( "SELECT * FROM `Main`");


while($row = mysql_fetch_array($result))   {

    echo "<tr>";   echo "<td>" . $row['ID'] . "</td>";     echo "<td>" . $row['AUTHOR'] . "</td>";   echo "<td>" . $row['TITLE'] . "</td>";   echo "<td>" . $row['DEGREE'] . "</td>";   echo "<td>" . $row['YEAR'] . "</td>";   echo "<td>" . $row['ABSTRACT'] . "</td>";
     echo "</tr>";
    }
     ?>


                    </tr>
                </tbody>            </table>

    </section> </body> </html>

如你所见,我正在使用

$.post( "abstract.php", { name: data[5] } ); window.open ("abstract.php"); } );

在一个名为 abstract.php 的 php 文件中发布 data[5] 的内容 ...但我得到一个空白页!!

这只是(目前)我的 abstract.php 文件的内容:

<?php echo $_POST["name"]; ?>

我想要的是,当用户点击查看摘要按钮时,页面将“摘要”提交到 abstract.php 文件并显示信息...

请帮忙。

谢谢

【问题讨论】:

    标签: php jquery mysql datatable datatables


    【解决方案1】:

    您尝试 POST 数据的方式实际上并没有达到您想要的效果。它将数据发布到一个线程中的抽象文件中,然后作为另一个线程打开带有空白数据的抽象文件。

    所以,而不是

    $.post( "abstract.php", { name: data[5] } ); window.open ("abstract.php"); } );
    

    就这样吧:

    window.open ("abstract.php?name="+data[5]);
    

    这将打开抽象文件并在获取参数中使用查询字符串传递数据。

    并且在 PHP 代码中而不是通过 POST 方法获取数据

    <?php echo $_POST["name"]; ?>
    

    使用这个(GET):

    <?php echo $_GET["name"]; ?>
    

    【讨论】:

    • 谢谢@Syed Qarib - 但不走运,它仍然给我一个空白页。我可以在 URL 中看到 [name] 的内容,但在页面上看不到。
    • 非常感谢,现在正在运行!但是,[name] 字段中会有相当多的文本,并且在 URL 中显示所有这些文本并不好。但是,我想我可以通过调用作为 ID 的 data [0] 来更改这一点,然后在 php.ini 中显示摘要(data [5])。将返回结果...再次感谢
    • @elstiv:不客气……是的,(id)方法会很好……接受答案,这样线程就会关闭……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多