简体   繁体   English

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

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

for some reason isset($_post) doesn't work when i use js form.submit() how i can resolve that without changing type of input from button to submit and just with using vanilla javascript because i need both of them.出于某种原因,当我使用 js form.submit() 时 isset($_post) 不起作用,我如何在不更改从按钮到提交的输入类型的情况下解决这个问题,并且只使用 vanilla javascript 因为我需要它们。

code html:代码 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>

and here is the php script in action.php file这是 php 脚本在运行。php 文件

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

and here is the javascript in action.js file这是 action.js 文件中的 javascript

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

Actually the problem didn't raise from the PHP. Buttons' value is sent when the form is directly submitted by them (In this case, it seems impossible).其实这个问题不是从PHP开始的。按钮的值是在他们直接提交表单时发送的(在这种情况下,这似乎是不可能的)。

Suggestion 1: use another field to check form submission.建议1:使用另一个字段来检查表单提交。

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

Suggestion 2: add hidden input in the HTML.建议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>

Suggestion 3: change button type to hidden before submitting.建议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