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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15055115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:27:37  来源:igfitidea点击:

How to pass form input value to php function

phphtmlformsfunction

提问by Gio

I want to write a php page in which there is a html form. I want to send all input (number for example) of my form to a php function (instead of a javascript function; I make this to hide my javascript function code).

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

How can I send input value to php function?
Is it possible to call the php function through onclick="function(param1, param2)"?
I know that javascript is a client-side language while php is server-side.

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

If it is possible, how can I write the return of the function in an input field?
I want to remain in my page.
Is it correct - action="#"?
My code is:

如果可能,如何在输入字段中写入函数的返回值?
我想留在我的页面。
是否正确 - 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>

Help me in the implementation of the simple php function and in the passage of values from input to function! Thanks!

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

回答by Nicholas Pickering

Make your actionempty. You don't need to set the onclickattribute, that's only javascript. When you click your submit button, it will reload your page with input from the form. So write your PHP code at the top of the form.

让你的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>

回答by supajason

You need to look into Ajax; Start herethis is the best way to stay on the current page and be able to send inputs to php.

你需要研究 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>

This gets the users input on the textbox and opens the webpage gethint.php?q=ja from here the php script can do anything with $_GET['q'] and echo back to the page James, Jason....etc

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

回答by Christoph Grimmer-Dietrich

No, the action should be the name of php file. With on click you may only call JavaScript. And please be aware the hiding your code from the user undermines trust. JS runs on the browser so some trust is needed.

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

回答by mongy910

This is pretty basic, just put in the php file you want to use for processing in the element.

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

For example

例如

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

Then in process.php you would get the form values using $_POST['name of the variable]

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

回答by saidozcan

You can write your php file to the actionattr of form element.
At the php side you can get the form value by $_POST['element_name'].

您可以将 php 文件写入action表单元素的attr。
在 php 端,您可以通过$_POST['element_name'].

回答by jasinth premkumar

you must have read about function call . here i give you example of it.

你一定读过函数调用。我在这里给你举个例子。

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