簡體   English   中英

從 HTML 輸入按鈕調用 php 函數或文件

[英]call a php function or file from a HTML input button

在按下/單擊按鈕時實現 php 代碼運行的最佳方法是什么。 做了一些研究,但除了幾個展示 JavaScript 使用的例子外,找不到任何相關的東西。 我希望它純粹是在 PHP 中。 在這個精確的時刻,我以以下方式創建了按鈕<input type='button' value='Click Me'/>現在不知道該怎么做。 任何人都可以請幫忙。

<form action="index.php" method="post">
    <input type='button' name='btn_func' value='Click Me'/>
</form>

<?php
    if(isset($_POST['btn_func'])){
        //call the function here.
    }
?>

如果您有 10 個按鈕:

<form action="index.php" method="post">
    <input type='button' name='btn_func_1' value='Click Me 1'/>
    <input type='button' name='btn_func_2' value='Click Me 2'/>
    <input type='button' name='btn_func_3' value='Click Me 3'/>
    <input type='button' name='btn_func_4' value='Click Me 4'/>
    <input type='button' name='btn_func_5' value='Click Me 5'/>
    <input type='button' name='btn_func_6' value='Click Me 6'/>
    <input type='button' name='btn_func_7' value='Click Me 7'/>
    <input type='button' name='btn_func_8' value='Click Me 8'/>
    <input type='button' name='btn_func_9' value='Click Me 9'/>
    <input type='button' name='btn_func_10' value='Click Me 10'/>
</form>

<?php
    if(isset($_POST['btn_func_1'])){
        //call the function here.
    }
?>

或者使用條件/開關語句。

我認為您不能僅通過本機 HTML 運行 PHP 腳本。 使用 JavaScript 將是您能做的最簡單的解決方案。

表單和php的基礎知識。 當按鈕被按下時,表單調用一個動作(某些頁面,甚至它本身)。 然后 php 檢查按鈕是否被按下。 如果是這樣,做一些事情,如果不是,忽略它。 雖然不是創建一個按鈕,提交會更好地工作,但按鈕工作得很好。

<form method="post" action="./PHPScripts.php" />
     <input type='button' value='Click Me' name='button' /> <-- added a name
     <input type='submit' value='Click Me' name='submitButton' />
     <input type='submit' value='Click Me' name='submitButton2' />
</form>


// below would be on the page called by the action in the above form
// which can be on the same page with no problem, but larger amounts of
// code become more difficult to read, so having multiple files is recommended
<?php
     if($_POST['button']) {
        // php code (either execute it, or call function)
     }
     if($_POST['submitButton']) {
        // php code (either execute it, or call function)
     }
     if($_POST['submitButton2']) {
        // php code (either execute it, or call function)
     }
?>

如果在同一頁面上,最好將 php 代碼放在 html 的頂部,但出於演示原因,我把它放在了底部。 (更容易閱讀)

如果您打算使用input type="button" ,我假設您需要 JavaScript 來處理點擊事件? 然后向 PHP 文件或其他內容發出 Ajax 請求?

或者,您可以將輸入類型從“按鈕”更改為“提交” <input type="submit" name="submitButton" id="submitButton" value="Submit" />

然后……最簡單的(因為沒有更好的詞)是在頁面頂部檢查提交帖子變量。 如果它在那里,PHP 會處理它。 或者,如果未設置變量,它將顯示表單。

<?php
    if (isset($_POST['submitButton']))
    {
        // form has been submitted.
        // do something with PHP.

    }
?>
<form action="file.php" method="post">
    ...
    <input type="submit" name="submitButton" id="submitButton" value="Submit" />
</form>

這個想法是,您需要為按鈕指定名稱屬性。 根據表單提交的方式,在這種情況下,提交給表單本身或由表單操作屬性和方法確定的另一個頁面,然后腳本將運行。

<form action="submit.php" method="post"> 
<input type='button' value='Click Me' **name="submit"**/> 
</form>

if(isset($_POST['submit']) {
     //runs when the button is clicked
}

暫無
暫無

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

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