簡體   English   中英

從表HTML中檢索特定的行值並將其提交給PHP

[英]Retrieve a specific row value from table HTML and submit it to PHP

我正在從數據庫中填充一個表,它看起來像這樣:

<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 webservice時。 它總是帶給我最后的價值。 我知道,因為我在每一行中使用相同的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 =一個\\ 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);

如果有更優雅或正確的方法! 請發布! 謝謝你們。

<elem1>
    <elem2 class="getMe"></elem2>
    <elem3></elem3>
</elem1>

快速上下文查找參考。 假設您的頁面上所有“ elem3”都有綁定的click事件。 單擊它時,您要獲取關聯的“ 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM