【问题标题】:How to pass form input value to php function如何将表单输入值传递给php函数
【发布时间】:2013-02-24 18:35:07
【问题描述】:

我想写一个 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 函数以及从输入到函数的值传递! 谢谢!

【问题讨论】:

  • 谷歌搜索了吗? w3schools.com/php/php_forms.asp
  • 你搜索过 ajax 吗?
  • 要发布到同一页面,只需使用空操作,即 action=""。
  • @jimmy 你可能想看这个页面w3fools.com W3 Schools(很遗憾)不是可靠的信息来源

标签: php html forms function


【解决方案1】:

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

例如

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

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

【讨论】:

  • 如果我这样做,我会得到: Undefined index 'name of the variable '
【解决方案2】:

将您的 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>

【讨论】:

  • @jcubic 表单可以使用任何一种 - 特别是 POST 如果他们正在处理电子邮件地址、D.O.B 等敏感信息时
  • @tim.baker 我知道,但他写的是method="get"
  • @jcubic 我很困惑,他写 GET 有什么问题,你的第一条评论说使用 GET 现在你说他写 GET 是他不应该做的?真正的表单应该始终使用 POST,因为 GET 中的任何内容可以被搜索引擎抓取
  • @tim.baker 他使用 html 形式获取但在 php 中发布。
  • @NicholasPickering 我解决了!非常感谢!我编辑了你的代码,问题是输入按钮没有标签 name="submit"!
【解决方案3】:

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

【讨论】:

  • 好的@Christoph,但我想隐藏一些公式。如果我使用 javascript 公式,则可以通过网络浏览器看到。
【解决方案4】:

您需要研究 Ajax; Start here 这是留在当前页面并能够将输入发送到 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

【讨论】:

  • 认为这对他的需求来说太复杂了。
  • 在他的问题中他说“我想留在我的页面中”这对我说 Ajax。
  • 留在我的页面中,因为当我点击我的按钮后,结果在同一个 php 页面的输入字段中可见。
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

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

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2014-04-30
      • 2012-10-03
      • 1970-01-01
      • 2018-12-01
      • 2011-09-07
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多