简体   繁体   English

从表HTML中检索特定的行值并将其提交给PHP

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

I'm populating a table from my database and it looks like this : 我正在从数据库中填充一个表,它看起来像这样:

<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 -->

the table is shown great, and my problem is when i try to submit my second column value "PARAM_STUDIENFACH" of a specific row to my php webservice. 该表显示得很好,而我的问题是当我尝试将特定行的第二列值“ PARAM_STUDIENFACH”提交给我的php webservice时。 It always gives me back the last value. 它总是带给我最后的价值。 I know that because I'm using the same id in every row so it will be overwritten. 我知道,因为我在每一行中使用相同的ID ,所以它将被覆盖。 I tried using JavaScript to return the value of the clicked row from other questions in the forum but it didn't work for me. 我尝试使用JavaScript从论坛中的其他问题返回被单击的行的值,但对我而言不起作用。 I'm using a bootstrap table if that helps. 我正在使用引导表,如果有帮助。

EDIT 1 : 编辑1:

Thanks to @Taplar answer I managed to find a solution to my problem. 感谢@Taplar的回答,我设法找到了解决问题的方法。 I used this JavaScript to retrieve the data and ajax to send a post request. 我使用此JavaScript检索数据,并使用ajax发送发布请求。 This is the code I used : 这是我使用的代码:

$(".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);
        }
});

});

my problem now is in the alert the "item" shows correctly but in my database it is saved as the following example : 我的问题现在是在警报中,“项目”正确显示,但是在我的数据库中将其保存为以下示例:

item = a (shows in alert a) item = a(显示在警报a中)

item = a \\n (it's saved like that in the database with spaces afeter \\n) item =一个\\ n(它保存在数据库中,并带有空格\\ n)

i tried to trim the item before sending it but i got the same result 我试图在发送之前修剪掉该物品,但是得到了相同的结果

to get the item sent by ajax i'm using this line of code in : 要获取由ajax发送的项目,我在以下代码行中使用:

$studienfach = null;

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

EDIT 2: 编辑2:

i managed to solve my second problem by doing this : 我这样做可以解决第二个问题:

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

if there is more elegent or correct way to do it ! 如果有更优雅或正确的方法! please post it ! 请发布! thank you all. 谢谢你们。

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

Quick contextual lookup reference. 快速上下文查找参考。 Say you have a click event bound on all 'elem3' on your page. 假设您的页面上所有“ elem3”都有绑定的click事件。 When you click it you want to get the associated 'elem2', not all of them. 单击它时,您要获取关联的“ elem2”,而不是全部。 With the class you can contextually look this element up by doing... 通过该类,您可以通过以下方式从上下文中查找此元素:

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

From the element you clicked, it will find the shared 'elem1' parent of both 'elem2' and 'elem3' and then find only the '.getMe' that belongs to that parent. 从您单击的元素中,它将找到“ elem2”和“ elem3”的共享“ elem1”父对象,然后仅找到属于该父对象的“ .getMe”。

More reading material: http://learn.jquery.com/using-jquery-core/working-with-selections/ 更多阅读材料: http : //learn.jquery.com/using-jquery-core/working-with-selections/

暂无
暂无

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

相关问题 从另一个 HTML 文件中的表中检索特定行 - Javascript - Retrieve specific row from a table in another HTML file - Javascript 从特定行访问表单元格以检索其嵌入式输入值 - Accessing table cells from a specific row to retrieve their embedded input value 无法使用javascript从html表中检索除第一行以外的其他行的值 - not able to retrieve the value of other rows than the first row from a html table using javascript JQUERY .val无法正常工作…使用PHP创建的HTML表无法获取特定的行值 - JQUERY .val not working…HTML table created using PHP unable to get the specific row value HTML发布时提交的特定值 - HTML post specific value on submit 无法从特定行检索值<logic:iterate> - Not Able to retrieve value from specific row <logic:iterate> 如何使用PHP偏移HTML表以从不同列的第二行单元格值中减去第一行的单元格值 - How To offset an HTML table with PHP to Subtract cell value of 1st row from 2nd row cell value of different columns 如何读取HTML表格特定行并使用javascript中的值 - How to read a HTML table specific row and use the value in javascript 如何从html表中选择行并获取第一个单元格的值并将其放在PHP Session上 - How select row from html table and get the value of the first cell and put it on PHP Session 使用php或javascript从html表中检索(获取)当前天气数据 - Retrieve (get) current weather data from html table with php or javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM