亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

表單發布后發送提交按鈕的值

表單發布后發送提交按鈕的值

PHP
瀟瀟雨雨 2019-12-04 14:41:42
我有一個名稱列表和一些帶有產品名稱的按鈕。當單擊其中一個按鈕時,列表信息將發送到PHP腳本,但是我無法點擊“提交”按鈕來發送其值。怎么做?我將代碼簡化為以下內容:發送頁面:<html><form action="buy.php" method="post">    <select name="name">        <option>John</option>        <option>Henry</option>    <select>    <input id='submit' type='submit' name = 'Tea'    value = 'Tea'>    <input id='submit' type='submit' name = 'Coffee' value = 'Coffee'></form></html>接收頁面:buy.php<?php    $name = $_POST['name'];    $purchase = $_POST['submit'];    //here some database magic happens?>除了發送提交按鈕值外,其他所有東西都可以正常工作。
查看完整描述

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 />';

}

?>


查看完整回答
反對 回復 2019-12-04
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

使用此代替:


<input id='tea-submit' type='submit' name = 'submit'    value = 'Tea'>

<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>


查看完整回答
反對 回復 2019-12-04
?
qq_笑_17

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' ] )是否要檢查數據是否正確發布。


查看完整回答
反對 回復 2019-12-04
  • 3 回答
  • 0 關注
  • 443 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號