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

时间:2014-12-06 20:56:24

标签: php forms function variables

我正在尝试在同一页面上向PHP函数提交两个表单字段值。 该功能可以完美地手动填充这两个值。

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

在表单上我必须遗漏一些东西,否则我会遇到语法错误。网页无法显示。我在这里找到了以下示例:A 1 Year old Stackoverflow post

    <?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>

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

2 个答案:

答案 0 :(得分:1)

您需要为提交按钮命名,或在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);
}
?>

答案 1 :(得分:0)

你可以隐藏字段:

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

使用已提交表单的isset函数检查php。

相关问题