无法通过 HTML 表单调用函数

我试图通过提交按钮调用一个函数,但我无法提交它。


<!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我得到以下页面

http://img4.mukewang.com/641d475c0001499f06590332.jpg

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

眼眸繁星
浏览 108回答 3
3回答

子衿沉夜

由于您现在使用 XAMPP 来使用 PHP,我们可以开始解决您的误解。在你的代码中你有这一行<form method="post" action="display()">似乎您认为该操作是要调用的 JavaScript 操作。但事实并非如此。aaction的<form>不调用 JavaScript,但它会将包含所有表单数据的表单发送到此属性中给定的 URL。display()由于您的属性中有值action,因此浏览器会尝试将数据发送到display()与您的表单 URL 相关的地址。但这是网络服务器无法回答的地址,因此会发回 404 错误。我现在要问你的问题是:表格发送后会发生什么?表单数据应该只写入文档吗?我认为这就是您想要实现的目标。如果是,请尝试此代码。<!DOCTYPE html><html><body><h2>API Call</h2><?php // Omit the action of the form.&nbsp;&nbsp; &nbsp; &nbsp; // Now your PHP script will be called again, when the form is submitted?><form method="post">&nbsp; <label for="gid">Global Device ID:</label><br>&nbsp; <input type="text" id="gid" name="gid" value="m99002021" readonly><br>&nbsp; <label for="type">Type:</label><br>&nbsp; <input type="text" id="type" name="type" value="EVNT" readonly><br><br>&nbsp; <label for="start">Start Date Time:</label><br>&nbsp; <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>&nbsp; <label for="end">End Date Time:</label><br>&nbsp; <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>&nbsp; <input type="submit" value="Execute"></form>&nbsp;<?phpif($_SERVER['REQUEST_METHOD']=='POST'){&nbsp; echo "hello".$_POST["gid"]."<br>";&nbsp; echo "hello".$_POST["type"]."<br>";&nbsp; echo "hello".$_POST["start"]."<br>";&nbsp; echo "hello".$_POST["end"]."<br>";}&nbsp;?></body></html>此代码中不涉及 JavaScript,因为您不需要它。顺便提一句。此代码应在 PHP 文件中,而不是 HTML 文件中

30秒到达战场

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

RISEBY

像这样尝试<!DOCTYPE html><html><body><h2>API Call</h2><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">&nbsp; <label for="gid">Global Device ID:</label><br>&nbsp; <input type="text" id="gid" name="gid" value="m99002021" readonly><br>&nbsp; <label for="type">Type:</label><br>&nbsp; <input type="text" id="type" name="type" value="EVNT" readonly><br><br>&nbsp; <label for="start">Start Date Time:</label><br>&nbsp; <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>&nbsp; <label for="end">End Date Time:</label><br>&nbsp; <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>&nbsp; <input type="submit" value="Execute"></form>&nbsp;<?phpfunction display(){&nbsp; if(isset($_POST['submit'])&nbsp; {&nbsp; &nbsp; echo "hello".$_POST["gid"]."<br>";&nbsp; &nbsp; echo "hello".$_POST["type"]."<br>";&nbsp; &nbsp; echo "hello".$_POST["start"]."<br>";&nbsp; &nbsp; echo "hello".$_POST["end"]."<br>";&nbsp; }}if($_SERVER['REQUEST_METHOD']=='POST'){&nbsp; &nbsp; &nbsp; &nbsp;display();}&nbsp;?></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript