简体   繁体   English

isset($ _ POST ['x'])仅在提交按钮名称=“提交”时有效

[英]isset($_POST['x']) only works if the submit button name=“submit”

I'm having trouble using postback because I've more than one form and isset only works with name="submit" buttons. 我在使用回发时遇到了麻烦,因为我有多个表单而且isset仅适用于name="submit"按钮。

My code will work fine if I use name="submit" but if I changed the name to something else, isset($_POST['somethingelse']) will always be false, why does this happen? 如果我使用name="submit"我的代码将正常工作,但如果我将名称更改为其他名称,则isset($_POST['somethingelse'])将始终为false,为什么会发生这种情况?

EDIT: 编辑:

<input type="submit" name="submit" value="Submit" onclick ="validate(document.getElementById('form')); return false;" />

if (isset($_POST['submit'])) <-- works as expected



<input type="submit" name="asdf" value="Submit" onclick ="validate(document.getElementById('form')); return false;" />


if (isset($_POST['asdf'])) <-- does not works as expected

EDIT2: EDIT2:

<?php
$validated = false;
if (isset($_POST['submit'])) {
    // preserve form values by storing the values from $_POST into variables
    $test = $_POST['test'];
    // validated is now true as submit button only submits if validated (script)
    $validated = true;
} else {
    // make variables empty as there's no values to preserve
        $test = '';
}

if (!$validated) {
?>
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p>Test: <input type="text" name="test" size="20" value="<?php echo $test ?>" /></p>
<p><input type="submit" name="submit" value="Submit" onclick ="validate(); return false;" /></p>
<?php } ?>

For example here, works as expected (I can keep the values) as name="submit" and if (isset($_POST['submit'])) is true when the button submits but if I change both the name="" and the if (isset($_POST[''])) then it will not work as the isset is returning false (even though the button submits). 例如,在这里,按预期工作(我可以保留值)name =“submit”和if(isset($ _ POST ['submit']))在按钮提交时为true但如果我更改了name =“”和if(isset($ _ POST ['']))然后它将无法工作,因为isset返回false(即使按钮提交)。

It's normal because only the clicked submit button is set and not the second, if you click on submit button with name submit isset($_POST['submit']) will give you true and isset($_POST['asdf']) false , and if you click on asdf isset($_POST['submit']) gives false and isset($_POST['asdf']) true , i tested it and it works without any problem. 这是正常的,因为只有点击的提交按钮被设置而不是第二个,如果你单击提交按钮,名称提交 isset($_POST['submit'])将给你trueisset($_POST['asdf']) false ,如果你点击asdf isset($_POST['submit'])给出falseisset($_POST['asdf']) ,我测试了它,它没有任何问题。

Test code: 测试代码:

<?php
$validated = false;
if (isset($_POST['submit'])) {
    // preserve form values by storing the values from $_POST into variables
    $test = "submit";
    // validated is now true as submit button only submits if validated (script)
    $validated = true;
} 
else if(isset($_POST['asdf'])){
        $test = "asdf";
    // validated is now true as submit button only submits if validated (script)
    $validated = true;
}
else {
    // make variables empty as there's no values to preserve
        $test = '';
}
if (!$validated) {
echo $test;
} ?>
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p>Test: <input type="text" name="test" size="20" value="<?php echo $test ?>" /></p>
<p><input type="submit" name="submit" value="Submit" onclick ="validate(); return false;" /></p>
<p><input type="submit" name="asdf" value="Submit" onclick ="validate(document.getElementById('form')); return false;" />
</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM