3 回答

TA貢獻1828條經驗 獲得超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.
// 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 文件中

TA貢獻1856條經驗 獲得超5個贊
像這樣嘗試
<!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>
添加回答
舉報