我想写一个 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/