【问题标题】:Retrieve a specific row value from table HTML and submit it to PHP从表格 HTML 中检索特定的行值并将其提交给 PHP
【发布时间】:2017-08-18 17:26:50
【问题描述】:

我正在从我的数据库中填充一个表,它看起来像这样:

<form name = "Form" role="form" action ="php/teilnehmen.php" method="POST">
    <fieldset>
                        <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Studienfach</th>
                                    <th>Teilnehmer/in</th>
                                    <th>Teilnehmen</th>
                                </tr>
                            </thead>
                            <tbody>
//<?php php code..... 
$i =0;
$sizeofstudienfaecherseperate = 
count($studienfaecherseperate);
for ($i; $i < $sizeofstudienfaecherseperate; $i++) {

?>
                                        <tr class="odd gradeX">
                                    <td ><?php echo($i+1);?></td>
        <td class="studienfach"><?php echo($studienfaecherseperate[$i]);?>
 <input   type="hidden" name="PARAM_STUDIENFACH" id="PARAM_STUDIENFACH" 
 value="<?php echo($studienfaecherseperate[$i]);?>"> </input>
        </td>
                                    <td ><?php echo($teilnehmer[$i]);?></td>
                                    <td width="10%">
         <?php if ($teilnahmestatus[$i] =="0"){ ?>
 <button type="submit" class="btn btn-success use-address" 
 name="teilnehmern"id="teilnehmen">Teilnehmen</button>
 <?php }else{?>
 <button type="submit" class="btn btn-danger use-address" name="teilnahme-beenden" 
 id="teilnahme-beenden">Teilnahme beenden</button>
 <?php }?>
                                    </td>
                                </tr>
                                <?php } ?>
                        </tbody>
                        </table>
      </fieldset>                  <!-- /.table-responsive -->

表格显示得很好,我的问题是当我尝试将特定行的第二列值 "PARAM_STUDIENFACH" 提交到我的 php web 服务时。它总是给我最后的价值。我知道,因为我在每一行中都使用相同的 id,所以它会被覆盖。我尝试使用 JavaScript 从论坛中的其他问题返回点击行的值,但它对我不起作用。如果有帮助,我正在使用引导表。

编辑 1:

感谢@Taplar 的回答,我设法找到了解决问题的方法。我使用这个 JavaScript 来检索数据和 ajax 来发送一个发布请求。这是我使用的代码:

$(".use-address").click(function() {

var item = $(this).closest("tr")   // Finds the closest row <tr> 
                   .find(".studienfach")     // Gets a descendent with class="nr"
                   .text();         // Retrieves the text within <td>
$.ajax({
        type: "POST",
        dataType: "json",
        url: "php/teilnehmen.php",
        data: {PARAM_STUDIENFACH:item},
        success: function(data){
            alert(item);
        },
        error: function(e){
            console.log(e.message);
        }
});

});

我现在的问题是在警报中“项目”正确显示,但在我的数据库中它被保存为以下示例:

item = a(在警报 a 中显示)

item = a \n(在数据库中就这样保存在\n之后)

我在发送之前尝试修剪该项目,但我得到了相同的结果

要获取 ajax 发送的项目,我正在使用这行代码:

$studienfach = null;

if(isset($_POST['PARAM_STUDIENFACH']))
    $studienfach = $mysqli->real_escape_string($_POST['PARAM_STUDIENFACH']);

编辑 2:

我设法通过这样做解决了我的第二个问题:

$pos= strpos($studienfach, "\\");
$studienfachtemp = substr($studienfach, 0,$pos);
trim($studienfachtemp);

如果有更优雅或更正确的方法!请张贴!谢谢大家。

【问题讨论】:

  • 这里的数据库代码为零。
  • 为什么 javascript 不适合你?
  • 这是一个重复的 id 问题,而不是使用类和进行上下文查找。 @MarcodeZeeuw
  • 请在下面查看我的答案。您不为此使用 id。你使用类。课程可以重复。

标签: javascript php jquery html


【解决方案1】:
<elem1>
    <elem2 class="getMe"></elem2>
    <elem3></elem3>
</elem1>

快速上下文查找参考。假设您在页面上的所有“elem3”上都绑定了一个点击事件。当您单击它时,您希望获得相关的“elem2”,而不是全部。使用该类,您可以通过执行以下操作根据上下文查找此元素...

//'this' being the elem3 that was clicked
$(this).closest('elem1').find('.getMe');

从您单击的元素中,它将找到“elem2”和“elem3”的共享“elem1”父级,然后仅找到属于该父级的“.getMe”。

更多阅读资料:http://learn.jquery.com/using-jquery-core/working-with-selections/

【讨论】:

  • 我设法通过您的回答找到了解决方案!请检查我编辑的问题,因为我还有一个小问题!提前谢谢你
  • 您可以考虑提出一个新问题,而不是改变您的问题。很难为不断变化的问题提供答案。 @user3816569
猜你喜欢
  • 1970-01-01
  • 2012-08-08
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2012-02-19
相关资源
最近更新 更多