简体   繁体   English

为什么我的脚本只获取我的php表的第一行? 下面是我的代码

[英]Why is my script is only getting the first row of my php table? below is my code

This is my JavaScript code 这是我的JavaScript代码

$('input#name-submit').on('click', function() {
    var name = $('input#name-submit').val();
    if($.trim(name) != ''){
        $.post('getmodalreasonUT.php', {name: name}, function(data) {
            alert(data);
        });
    }
});

and this is my php code 这是我的PHP代码

<?php

if(isset($_POST['Pending'])){
    echo "<header class='panel-heading'> Undertime Pending</header>";
    echo "<table class='table table-bordered'>
    <thead>
    <tr class='tbl-record'>
    <th>ID</th>
    <th>Name of Employee</th>
    <th>Position</th>
    <th>Date Filed</th>
    <th>Number of hours</th>
    </tr>
    </thead>
    <tbody>";

    $sql =   "SELECT * FROM utrequestform WHERE user_eid = '$employeenumber' and check_managerandsspv = '0'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {

        while($row = $result->fetch_assoc()){                             
            echo "<tr class='tbl-info text-center'>                            
            <td>".$row['user_eid']."</td>
            <td>".$row['nameofemployee']." </td>
            <td>".$row['position']."</td>
            <td>".$row['datefiled']."</td>
            <td>".$row['noofhours']."</td>    
            <td><input type='submit' id='name-submit' value='$row[id]'></td>
            </tr> ";

        }
    }else{
        echo "<tr><td colspan='10'>No pending UT request.</td></tr>";
    }
    echo "</tbody></table>";
}
?> 

This is my echo php code. 这是我的echo php代码。

<?php 
    include ('connection.php');
    $please = $_POST['name'];
    echo $please;
?>

这是我的数据库结构

You are generating the HTML like this: 您正在生成这样的HTML:

<input type='submit' id='name-submit' value='$row[id]'>

Which will render the same input several times with the same ID. 这将使用相同的ID多次呈现相同的输入。 This is invalid markup. 这是无效的标记。 You need to change that line for: 您需要更改该行:

<input type='submit' id='name-submit-$row[id]' name='request-val' value='$row[id]'>

Then change your jQuery for: 然后更改你的jQuery:

$('input[name="request-val"]').on('click', function() {
    var name = $(this).val();
    if($.trim(name) != ''){
        $.post('getmodalreasonUT.php', {name: name}, function(data) {
            alert(data);
        });
    }
});

Avoid having duplicated IDs at all costs . 不惜一切代价避免重复使用ID That will save you from a lot of frustration later down the road. 这样可以在以后的路上避免很多挫折。

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

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