[英]Unable to call function via HTML form
我试图通过提交按钮调用一个函数,但我无法提交它。
<!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
按钮时,我收到以下页面
URL 是file:///C:/Users/Faisal/Desktop/display()
更新 1
我已经将xampp
用于我的网络服务器。 现在我试图通过http://localhost/udil/backend/main.php
访问它,它给了我波纹管页面
当我点击execute
按钮时,我进入页面下方
如何正确提交?
任何帮助将不胜感激。
由于您现在正在使用 XAMPP 来使用 PHP,我们可以开始解决您的误解。
在您的代码中,您有这一行<form method="post" action="display()">
似乎您认为该操作是要调用的 JavaScript 操作。 但事实并非如此。
在action
上<form>
不调用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 文件中
只需从表单中删除 display() 并单击“执行”按钮。 它会正常工作
像这样尝试
<!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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.