【问题标题】:Unable to call function via HTML form无法通过 HTML 表单调用函数
【发布时间】:2020-09-25 11:57:53
【问题描述】:

我正在尝试通过提交按钮调用函数,但无法提交。

<!DOCTYPE html>
<html>
<body>

<h2>API Call</h2>

<form method="post" action="display()">
  <label for="gid">Global Device ID:</label><br>
  <input type="text" id="gid" name="gid" value="m99002021" readonly><br>
  <label for="type">Type:</label><br>
  <input type="text" id="type" name="type" value="EVNT" readonly><br><br>
  <label for="start">Start Date Time:</label><br>
  <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>
  <label for="end">End Date Time:</label><br>
  <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>
  <input type="submit" value="Execute">
</form> 

<?php
function display()
{
  echo "hello".$_POST["gid"]."<br>";
  echo "hello".$_POST["type"]."<br>";
  echo "hello".$_POST["start"]."<br>";
  echo "hello".$_POST["end"]."<br>";
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
       display();
} 

?>

</body>
</html>

当点击 execute 按钮时,我得到下面的页面

网址是file:///C:/Users/Faisal/Desktop/display()

更新 1

我已将xampp 用于我的网络服务器。现在我正在尝试通过http://localhost/udil/backend/main.php 访问它,它给了我下面的页面

当我点击execute 按钮时,我正在页面下方

如何正确提交?

任何帮助将不胜感激。

【问题讨论】:

  • 您以错误的方式混合 PHP 和 JavaScript。并且您不会将网络服务器与 PHP 一起使用。
  • @yunzen 我已经尝试关注this解决方案?
  • @Moeez 首先你需要一个运行 PHP 的网络服务器。没有网络服务器,使用 PHP 没有意义
  • @yunzen 我可以用xampp吗?

标签: javascript php html forms


【解决方案1】:

由于您现在使用 XAMPP 来使用 PHP,我们可以开始解决您的误解。

在您的代码中,您有这一行
&lt;form method="post" action="display()"&gt;

似乎您认为该操作是要调用的 JavaScript 操作。但事实并非如此。

&lt;form&gt;action 不调用 JavaScript,但它将带有所有表单数据的表单发送到此属性中给定的 URL。由于您的action 属性中有display() 的值,因此浏览器会尝试将数据发送到相对于您的表单URL 的地址display()。但这是一个网络服务器无法回答的地址,因此会发回 404 错误。

我现在的问题是:表格发送后会发生什么?表单数据是否应该只写入文档?我认为这就是您想要实现的目标。如果是,试试这个代码。

<!DOCTYPE html>
<html>
<body>

<h2>API Call</h2>

<?php // Omit the action of the form. 
      // Now your PHP script will be called again, when the form is submitted
?>
<form method="post">
  <label for="gid">Global Device ID:</label><br>
  <input type="text" id="gid" name="gid" value="m99002021" readonly><br>
  <label for="type">Type:</label><br>
  <input type="text" id="type" name="type" value="EVNT" readonly><br><br>
  <label for="start">Start Date Time:</label><br>
  <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>
  <label for="end">End Date Time:</label><br>
  <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>
  <input type="submit" value="Execute">
</form> 

<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
  echo "hello".$_POST["gid"]."<br>";
  echo "hello".$_POST["type"]."<br>";
  echo "hello".$_POST["start"]."<br>";
  echo "hello".$_POST["end"]."<br>";
} 

?>

</body>
</html>

此代码中不涉及 JavaScript,因为您不需要它。

顺便说一句。此代码应位于 PHP 文件中,而不是 HTML 文件中

【讨论】:

    【解决方案2】:

    只需从表单中删除 display() 并单击“执行”按钮。它会正常工作

    【讨论】:

      【解决方案3】:

      这样试试

      <!DOCTYPE html>
      <html>
      <body>
      
      <h2>API Call</h2>
      
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="gid">Global Device ID:</label><br>
        <input type="text" id="gid" name="gid" value="m99002021" readonly><br>
        <label for="type">Type:</label><br>
        <input type="text" id="type" name="type" value="EVNT" readonly><br><br>
        <label for="start">Start Date Time:</label><br>
        <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>
        <label for="end">End Date Time:</label><br>
        <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>
        <input type="submit" value="Execute">
      </form> 
      
      <?php
      function display()
      {
        if(isset($_POST['submit'])
        {
          echo "hello".$_POST["gid"]."<br>";
          echo "hello".$_POST["type"]."<br>";
          echo "hello".$_POST["start"]."<br>";
          echo "hello".$_POST["end"]."<br>";
        }
      }
      if($_SERVER['REQUEST_METHOD']=='POST')
      {
             display();
      } 
      
      ?>
      
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 2019-03-11
        相关资源
        最近更新 更多