簡體   English   中英

從連續第一個單元格獲取值以使用Javascript提交

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

我使用PHP從數據庫中創建一個表,這意味着表的大小和值各不相同。

表創建:

<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>";
?>

用於提交的Javacript函數是此函數:

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

現在,每當我單擊一行時,它都會進入相應的頁面“ resolver.php”,但始終發送上一次執行while以來的最新數據。

我需要從單擊的行中的第一個單元格或包含所需輸入的隱藏輸入標記中獲取值。

謝謝。

完整建議

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

您的問題是您只是在提交表單時未對單擊的內容進行任何引用。 所有隱藏的輸入都具有相同的名稱,因此表單僅發送最后一個。

請嘗試以下操作:

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

然后在提交之前更改您的JavaScript來更改值:

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

當我打字時,mplungjan的榮譽也得到了!

如果我覺得很好...您將表單設置為具有許多同名的隱藏輸入...您想要的內容...它當然會返回最后一個值...您必須在循環中更改名稱然后調用確切名稱輸入的

暫無
暫無

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

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