3 回答
TA貢獻1828條經驗 獲得超4個贊
按鈕名稱未提交,因此$_POST['submit']未設置php 值。如in isset($_POST['submit'])評估為false。
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>
<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
TA貢獻2041條經驗 獲得超4個贊
使用此代替:
<input id='tea-submit' type='submit' name = 'submit' value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
TA貢獻1818條經驗 獲得超7個贊
就像其他人說的那樣,您可能會誤解了唯一ID的想法。我要補充的是,我不喜歡在這里使用“值”作為標識屬性的想法,因為它可能隨著時間而改變(即,如果您想提供多種語言)。
<input id='submit_tea' type='submit' name = 'submit_tea' value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />
并在您的PHP腳本中
if( array_key_exists( 'submit_tea', $_POST ) )
{
// handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
// handle coffee
}
另外,您可以添加類似的內容,例如if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )是否要檢查數據是否正確發布。
- 3 回答
- 0 關注
- 443 瀏覽
添加回答
舉報
