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


How to pass form input value to php 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>

您需要研究Ajax;从这里开始,这是保持当前页面并能够向php发送输入的最佳方式。

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<h3>Start typing a name in the input field below:</h3>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 
</body>
</html>

这将获得用户在文本框上的输入并打开网页gethint.php?q=ja从这里php脚本可以用$_GET['q']做任何事情,并返回到页面James,Jason。。。。etc

这是非常基本的,只需放入要用于处理元素的php文件中即可。

例如

<form action="process.php" method="post">

然后在process.php中,您将使用$_POST['name of the variable] 获得表单值

否,操作应该是php文件的名称。点击时,您只能调用JavaScript。请注意,向用户隐藏代码会破坏信任。JS在浏览器上运行,因此需要一些信任。

您可以将php文件写入form元素的action属性
在php端,您可以通过$_POST['element_name']获取表单值。

您一定已经阅读了函数调用的相关内容。这里我给你举个例子。

<?php
 funtion pr($n)
{
echo $n;
 }
?>
<form action="<?php $f=$_POST['input'];pr($f);?>" method="POST">
<input name=input type=text></input>
</form>