简体   繁体   English

从连续第一个单元格获取值以使用Javascript提交

[英]Getting the value from first cell in a row to submit using Javascript

I create a table using PHP from a database, meaning the size of the table and values vary. 我使用PHP从数据库中创建一个表,这意味着表的大小和值各不相同。

The table creation: 表创建:

<table>     
            <thead>
                <tr>
                    <th>Ticket</th>
                    <th>Empresa</th>
                    <th>Requerente</th>
                    <th>Motivo</th>
                    <th>Data</th>
                    <th>Hora</th>
                </tr>
            </thead>

        <tbody>
            <form name="goTicket" action="resolver.php" method="POST" >         
<?php   
    $sql = "QUERY";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
        echo "<tr onmouseover=\"this.style.background='#EFF5FB';this.style.cursor='pointer'\"
        onmouseout=\"this.style.background='white';\" onclick=\"javascript: submitform()\">";
        echo "<td>$row[Ticket]</td>";
        echo "<input type='hidden' name='_ticket' value='$row[Ticket]' />";
        echo "<td>$row[Empresa]</td>";
        echo "<td>$row[Requerente]</td>";
        echo "<td>$row[Motivo]</td>";
        echo "<td>$row[Data]</td>";
        echo "<td>$row[Hora]</td>";
        echo "</tr>";
    }
    echo "</form>";
    echo "<tr><td colspan='6' style='text-align: center;'><form action='adminmenu.php' ><br /><input type='submit' name='woot' value='Main Menu' /></form></td></tr>";  
    echo "<tbody>";
    echo "</table>";
?>

The Javacript function used to submit is this one: 用于提交的Javacript函数是此函数:

<script type="text/javascript">
        function submitform()
        {
            document.forms["goTicket"].submit();          
        }
</script>

Now whenever I click on a row it takes to the appropriate page "resolver.php" but always sends the most recent data, from last time the while was executed. 现在,每当我单击一行时,它都会进入相应的页面“ resolver.php”,但始终发送上一次执行while以来的最新数据。

I need the value from the first cell, or the hidden input tag, that contains what I need, from the row I click. 我需要从单击的行中的第一个单元格或包含所需输入的隐藏输入标记中获取值。

Thank you. 谢谢。

Full suggestion 完整建议

 <script type="text/javascript">
 function submitform(ticket) {
   document.goTicket.elements["_ticket"].value=ticket;
   document.goTicket.submit();
 }
 </script>
 <table>     
        <thead>
            <tr>
                <th>Ticket</th>
                <th>Empresa</th>
                <th>Requerente</th>
                <th>Motivo</th>
                <th>Data</th>
                <th>Hora</th>
            </tr>
        </thead>

    <tbody>
<form name="goTicket" action="resolver.php" method="POST" >
<input type="hidden" name="_ticket" value="" />

<?php   
    $sql = "QUERY";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
?>    

    <tr onmouseover="this.style.background='#EFF5FB';this.style.cursor='pointer'"
    onmouseout="this.style.background='white';" 
    onclick="submitform('<?php echo $row[Ticket]; ?>')">
    <td><?php echo $row[Ticket];     ?></td>
    <td><?php echo $row[Empresa];    ?></td>
    <td><?php echo $row[Requerente]; ?></td>
    <td><?php echo $row[Motivo];     ?></td>
    <td><?php echo $row[Data];       ?></td>
    <td><?php echo $row[Hora];       ?></td>
    </tr>
<?php } ?>

</form><!-- not really nice -->
   <tr><td colspan="6" style="text-align: center;"><form action="adminmenu.php" >
   <br /><input type="submit" name="woot" value="Main Menu" /></form></td></tr>
    </tbody>
   </table>

Your problem is that you're just submitting the form without any reference to what was clicked. 您的问题是您只是在提交表单时未对单击的内容进行任何引用。 All the hidden inputs have the same name so the form just sends the last one. 所有隐藏的输入都具有相同的名称,因此表单仅发送最后一个。

Try the following: 请尝试以下操作:

echo "<tr onmouseover=\"this.style.background='#EFF5FB';this.style.cursor='pointer'\" onmouseout=\"this.style.background='white';\" onclick=\"javascript: submitform('$row[Ticket]')\">";

// delete this from while:
// echo "<input type='hidden' name='_ticket' value='$row[Ticket]'/>";
// and instead add before </form> with value=""

Then change your javascript to change the value before submitting: 然后在提交之前更改您的JavaScript来更改值:

function submitform(val) {
    document.forms["goTicket"].elements["_ticket"].value = val;
    document.forms["goTicket"].submit();          
}

Kudos to mplungjan also got this as I was typing! 当我打字时,mplungjan的荣誉也得到了!

如果我觉得很好...您将表单设置为具有许多同名的隐藏输入...您想要的内容...它当然会返回最后一个值...您必须在循环中更改名称然后调用确切名称输入的

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

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