簡體   English   中英

提交帶有多個提交按鈕的PHP表單

[英]Submitting a PHP form with multiple submit buttons

再次編輯:新修訂的版本

所以我已經糾正了表格中缺少標簽,我已將id添加到我的表單中,我為每個表單添加了一個隱藏字段,其值與表單ID相同(如建議的那樣)

我試圖修改我的原始if ifset包括表單id,但那是我來了一點點unstuck。

如上所述,這是頁面創建的HTML:

        <br><table border=1><tr><th>customer</th><th>product</th><th>employee</th><th>return_date</th><th>complete</th></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td>        <FORM id="1" method ="post" action ="g.php">
        <input type='hidden' name='x' value='1'>
        <Input type = 'Radio' Name ='ir' value= '47'> 47        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td>      <FORM id="2" method ="post" action ="g.php">
        <input type='hidden' name='x' value='2'>
        <Input type = 'Radio' Name ='ir' value= '48'> 48        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td>      <FORM id="3" method ="post" action ="g.php">
        <input type='hidden' name='x' value='3'>
        <Input type = 'Radio' Name ='ir' value= '49'> 49        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>29-09-12</td><td>      <FORM id="4" method ="post" action ="g.php">
        <input type='hidden' name='x' value='4'>
        <Input type = 'Radio' Name ='ir' value= '50'> 50        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td>      <FORM id="5" method ="post" action ="g.php">
        <input type='hidden' name='x' value='5'>
        <Input type = 'Radio' Name ='ir' value= '51'> 51        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>01-10-12</td><td>      <FORM id="6" method ="post" action ="g.php">
        <input type='hidden' name='x' value='6'>
        <Input type = 'Radio' Name ='ir' value= '52'> 52        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td>      <FORM id="7" method ="post" action ="g.php">
        <input type='hidden' name='x' value='7'>
        <Input type = 'Radio' Name ='ir' value= '60'> 60        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br><tr><td>marne1</td><td>basketball</td><td>user</td><td>02-10-12</td><td>      <FORM id="8" method ="post" action ="g.php">
        <input type='hidden' name='x' value='8'>
        <Input type = 'Radio' Name ='ir' value= '61'> 61        <input type="submit" name="return"/>
        </FORM>
        </td></tr><br></table> 

    <fieldset>
<legend>???</legend>
<form action="z.php" method="post">
Customer Name: <input type="text" name="ir" /> <br><br>
<input type="submit" name="return"/>
</form>

這是創建此HTML的PHP​​頁面(使用我嘗試過的表單id測試):

    <?php
$db_host = "localhost"; 
$db_username = "root";  
$db_pass = "usbw";  
$db_name = "test"; 
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database"); 
$result = mysql_query("SELECT * FROM rental WHERE customer = '$_POST[customer]' AND complete = '0' ") or die (mysql_error());
?> 

    <?php
    echo '<br>';
    echo '<table border=1>';
    echo '<tr><th>customer</th><th>product</th><th>employee</th><th>return_date</th><th>complete</th></tr>';
    echo "<br>";
    $x=1;
    while($row=mysql_fetch_array($result))
    {   
        echo '<tr>';
        echo '<td>';
        echo $row ['customer'];
        echo '</td><td>' ;
        echo $row ['product'];
        echo '</td><td>';
        echo $row ['employee'];
        echo '</td><td>';
        echo $row ['return_date'];
        echo '</td><td>';
        $cur_return = $row ['rental_id'];
        ?>
        <FORM id="<?php echo $x; ?>" method ="post" action ="g.php">
        <input type='hidden' name='x' value='<?php echo $x; ?>'>
        <Input type = 'Radio' Name ='ir' value= '<?php echo $cur_return ?>'> <?php echo $cur_return ?>
        <input type="submit" name="return"/>
        </FORM>
        <?php
    echo '</td></tr>'; 
    echo '<br>';
    $x++;
    }
    echo '</table>';
    ?> 

    <fieldset>
<legend>???</legend>
<form action="z.php" method="post">
Customer Name: <input type="text" name="ir" /> <br><br>
<input type="submit" name="return"/>
</form>

    <?php
    if (isset($_POST['return']))
    {
    if ($_POST['x']==1)
        {
        mysql_query("UPDATE rental SET complete = '1' WHERE rental_id = '$_POST[ir]'") or die (mysql_error());
        }   
    }
    ?>  

我真的非常感謝你們所建議的所有幫助,並且抱歉我花了這么多時間把它弄清楚:)

豐富

等等。 這里有多行數據,你想提交一個特定的數據行,但是它們都以相同的形式存在? 我有點困惑。 看起來你真正想要的是為每一行生成一個不同的表單元素。 但是你不能讓多個提交按鈕提交相同表單的不同部分,這不是表單的工作方式。 提交按鈕將提交屬於該表單的所有數據。

在這種情況下,只需將開始表單標記移動到while循環內部的最開頭,然后將提交按鈕和結束表單標記移動到while循環的底部。 它將生成每個表單並仍然保持表的結構。 但是你不能只有一個表單只提交自己的某些部分,這違反了基本的HTML原則。

強烈建議在每個表單上打一個ID,以備將來參考和更新。

祝好運。

你可以使用javascript,但不能用PHP。 但它無緣無故地過於復雜。 制作不同的表單並將它們分配給不同的ID。 您可以使用相同的方法=“post”和相同的action =“g.php”。 要在g.php上有所不同,只需將其放在每個表單中:

<input type='hidden' name='x' value='1'>

接着

<input type='hidden' name='x' value='2'>

然后它是一個簡單的如果$ _POST ['x'] == 1

另外,請確保保護g.php,這對於處理表單的文件來說是一個非常不安全的名稱。

暫無
暫無

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

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