php - 如何将表单输入值传递给php函数

标签 php html forms function

我想写一个 php 页面,其中有一个 html 表单。我想将表单的所有输入(例如数字)发送到 php 函数(而不是 javascript 函数;我这样做是为了隐藏我的 javascript 函数代码)。

如何将输入值发送到 php 函数?
是否可以通过onclick="function(param1, param2)"调用php函数?
我知道 javascript 是客户端语言,而 php 是服务器端语言。

如果可以的话,函数的返回怎么写在输入框里?
我想留在我的页面。
是否正确 - action="#"
我的代码是:

<form action="#" method="get">
    Inserisci number1: 
    <input type="text" name="val1" id="val1"></input>

    <?php echo "ciaoooo"; ?>

    <br></br>
    Inserisci number2:
    <input type="text" name="val2" id="val2"></input>

    <br></br>

    <input type="submit" value="send"></input>
</form>

帮助我实现简单的 php 函数以及从输入到函数的值传递! 谢谢!

最佳答案

让您的action 为空。您不需要设置 onclick 属性,那只是 javascript。当您单击提交按钮时,它将使用表单中的输入重新加载您的页面。因此,请在表单顶部编写您的 PHP 代码。

<?php
if( isset($_GET['submit']) )
{
    //be sure to validate and clean your variables
    $val1 = htmlentities($_GET['val1']);
    $val2 = htmlentities($_GET['val2']);

    //then you can use them in a PHP function. 
    $result = myFunction($val1, $val2);
}
?>

<?php if( isset($result) ) echo $result; //print the result above the form ?>

<form action="" method="get">
    Inserisci number1: 
    <input type="text" name="val1" id="val1"></input>

    <?php echo "ciaoooo"; ?>

    <br></br>
    Inserisci number2:
    <input type="text" name="val2" id="val2"></input>

    <br></br>

    <input type="submit" name="submit" value="send"></input>
</form>

关于php - 如何将表单输入值传递给php函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15055115/

相关文章:

php - 试图从 mysql 查询创建多维数组,php?

php - Symfony2 : app_dev. php 只允许访问IP?

html - 如何将正文背景图像设置为 50%?

css - 纯HTML5画图

forms - 在 Qt Creator 中检查 UI 设计的外观

php - Laravel 5.1 在表单请求验证之前修改输入

PHP 新关键字与目录?

html - SVG 文本 getComputedTextLength 对于阿拉伯字体来说太短

javascript - 未捕获类型错误 : undefined is not a function on . GetElementByID

javascript - ajax Rails 表单计算器中的奇怪行为(预填充)