簡體   English   中英

php isset($_post[]) 和 javascript form.submit

[英]php isset($_post[]) and javascript form.submit

出於某種原因,當我使用 js form.submit() 時 isset($_post) 不起作用,我如何在不更改從按鈕到提交的輸入類型的情況下解決這個問題,並且只使用 vanilla javascript 因為我需要它們。

代碼 html:

<form name="user_verification" action="action.php" method="POST">
 Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="button" onclick="myFunction(this)" name="button" value="submit">
</form>

這是 php 腳本在運行。php 文件

if($_SERVER['REQUEST_METHOD']=="POST")
{
if(isset($_POST['button'])) 
{
echo 'yes';
}else{
echo 'no';
}
}

這是 action.js 文件中的 javascript

function myFunction(button)
{
//some code
const form=button.parentNode;
form.submit();}

其實這個問題不是從PHP開始的。按鈕的值是在他們直接提交表單時發送的(在這種情況下,這似乎是不可能的)。

建議1:使用另一個字段來檢查表單提交。

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['email'])) {
        echo 'yes';
    } else {
        echo 'no';
    }
}

建議2:在HTML增加隱藏輸入。

<form name="user_verification" action="test.php" method="POST">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="hidden" name="button" value="submit">
    <input type="button" onclick="myFunction(this)">
</form>

建議3:提交前將按鈕類型改為隱藏。

function myFunction(button) {
    // some code
    button.setAttribute('type', 'hidden');
    const form=button.parentNode;
    form.submit();
    button.setAttribute('type', 'button');
}

暫無
暫無

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

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