简体   繁体   English

获取HTML <TD> 值

[英]Get HTML's <TD> Value

I would like to get the static values from my table data (the tag) and and display it to another page. 我想从我的表数据(标记)中获取静态值,并将其显示在另一页上。 Should I use jQuery for this or possibly PHP? 我应该为此使用jQuery还是可能使用PHP?

Example.php Table: Example.php表:

                                <table width="100%" class="table table-striped table-bordered table-hover display" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th>Race</th>
                                        <th>Role</th>
                                        <th>2015</th>
                                        <th>2016</th>
                                        <th>2017</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr class="Testing">
                                        <td>BLAH</td>
                                        <td>ANOTHA_BLAH</td>
                                        <td>25</td>
                                        <td>26</td>
                                        <td>27</td>
                                    </tr>
</table>

I tried doing it in PHP similar to 我尝试用类似于

       <form action = "PutTheExampleTableValuesHere.php" method = "post">
           TABLE BLAH BLAH BLAH
       <input type="submit" name="submit" value="View Line Chart">

and using the $_POST function and simply echoing it to the other pages but my logic failed. 并使用$ _POST函数并将其简单地回显到其他页面,但是我的逻辑失败了。

Please guide. 请指导。 Thank you! 谢谢!

You may use AJAX to post the whole html code to the other page ( URL ): 您可以使用AJAX将整个html代码发布到另一页( URL ):

$.post(URL, {data: encodeURIComponent($("#dataTables-example").html())})

On the other page( URL ), you can use php's $_POST to get it: 在另一个页面( URL )上,可以使用php的$_POST来获取它:

<form action = "PutTheExampleTableValuesHere.php" method = "post">
    <table ...>
        <?php urldecode($_POST['data']) ?>
    </table>
<input type="submit" name="submit" value="View Line Chart">

UPDATE: 更新:
@SimoneWalter Then you can compose the data in $.post with the TD values. @SimoneWalter然后,您可以将$.postdata与TD值组成。 For example: 例如:

data = []
$.each($("#dataTables-example TR"), function(tr){
    // get td values
    tmp = []
    $.each($(tr).find('TD'), function(td){
        tmp.push($(td).html())
    })
    data.push(tmp)
})
// post the data
// data will be something like:
// [['Race', 'Role', '2015', ...], ...]
$.post(URL, {data: data})

Then on the other page: 然后在另一页上:

<form action = "PutTheExampleTableValuesHere.php" method = "post">
    <table ...>
        <thead>
            <?php foreach($_POST['data'] as $tr): ?>
            <tr>
                <?php foreach($tr as $td): ?>
                <td><?php echo $td ?></td>
                <?php endforeach ?>
            </tr>
            <?php endforeach ?>
        </thead>
    </table>
<input type="submit" name="submit" value="View Line Chart">

with the $_POST logic is way more simpler than using javascript/ajax for this solution. 与$ _POST逻辑相比,此解决方案比使用javascript / ajax更简单。 Just put your data in some paragraph or similar and give it a name so you can access it from post from another external page that you call in "method" parameter in form. 只需将数据放在某个段落或类似的段落中并给它起一个名字,这样您就可以从另一个外部页面(在表单中以“ method”参数调用)访问该数据。 example: 例:

index.php: index.php:

  <form action="test.php" method="post">
  <table>
      <td>
          <div id="blah" name="blah" value="blah">BLAH</div>
      </td>
      <td>
          <div id="anoblah" name="anoblah" value="anotherblah">BLAH</div>
      </td>
      <input type="submit" id="test" name="test">
  </table>
  </form>

test.php test.php

   <?php
        if(isset($_POST["test"]))
             echo "td1: ".$_POST["blah"]." td2: ".$_POST["anoblah"];


   ?>

You can probably use jquery to create an array (using input hidden) in the form structure. 您可能可以使用jquery在表单结构中创建一个数组(使用隐藏的输入)。 Then you can read it after the form submit, using $_POST variable. 然后,您可以在提交表单后使用$ _POST变量读取它。

<script>
$(document).ready(function(){
  $("input[type=submit]").click(function(e) {
    e.preventDefault();
    var form = $("form");
    $(".Testing td").each(function() {
      form.append('<input type="hidden" name="data[]" value="' + $(this).html() + '">');
    });
    form.submit();
  });
});
</script>

This will create an array from the TD values under class "Testing" that you can access over $_POST['data'] (type Array). 这将从“ Testing”类下的TD值创建一个数组,您可以通过$ _POST ['data'](类型Array)进行访问。

<?php 
if(isset($_POST['data'])) {
  foreach($_POST['data'] as $key=>$val) {
    echo $key.' = '.$val.'<br>';
  }
}
?>

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

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