简单表单提交到 PHP 函数失败


Simple form submit to PHP function failing

我正在尝试在同一页面上向PHP函数提交两个表单字段值。该函数非常适合手动填充两个值。

<?PHP  // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>

在表单上,我一定缺少某些内容,或者我有语法错误。网页不会显示失败。 我在这里找到了以下示例:一个 1 年前的 Stackoverflow 帖子

    <?php
if( isset($_GET['submit']) ) {
    $val1 = htmlentities($_GET['val1']);
    $val2 = htmlentities($_GET['val2']);
    $result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
    Input position a-s: 
    <input type="text" name="val1" id="val1"></input>
    <br></br>
    Input quantity value:
    <input type="text" name="val2" id="val2"></input>
    <br></br>
    <input type="submit" value="send"></input>
</form>

可能是我的代码在表单上的位置吗?任何建议表示赞赏。

您需要

命名提交按钮,或在if(isset($_GET['submit']))部分检查其他内容:

<form action="" method="get">
    Input position a-s: 
    <input type="text" name="val1" id="val1" />
    <br></br>
    Input quantity value:
    <input type="text" name="val2" id="val2" />
    <br></br>
    <input type="submit" name="submit" value="send" />
</form>

或者保持相同的形式,但将 php 更改为:

<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
    $val1 = htmlentities($_GET['val1']);
    $val2 = htmlentities($_GET['val2']);
    $result = sav_newval_of($val1, $val2);
}
?>
您可以将

字段隐藏为:

<input type='hidden' name='action' value='add' >

并使用 isset 函数检查 php,该表单已提交。