3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
由于您現(xiàn)在使用 XAMPP 來使用 PHP,我們可以開始解決您的誤解。
在你的代碼中你有這一行
<form method="post" action="display()">
似乎您認(rèn)為該操作是要調(diào)用的 JavaScript 操作。但事實(shí)并非如此。
aaction的<form>不調(diào)用 JavaScript,但它會(huì)將包含所有表單數(shù)據(jù)的表單發(fā)送到此屬性中給定的 URL。display()由于您的屬性中有值action,因此瀏覽器會(huì)嘗試將數(shù)據(jù)發(fā)送到display()與您的表單 URL 相關(guān)的地址。但這是網(wǎng)絡(luò)服務(wù)器無法回答的地址,因此會(huì)發(fā)回 404 錯(cuò)誤。
我現(xiàn)在要問你的問題是:表格發(fā)送后會(huì)發(fā)生什么?表單數(shù)據(jù)應(yīng)該只寫入文檔嗎?我認(rèn)為這就是您想要實(shí)現(xiàn)的目標(biāo)。如果是,請(qǐng)嘗試此代碼。
<!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,因?yàn)槟恍枰?/p>
順便提一句。此代碼應(yīng)在 PHP 文件中,而不是 HTML 文件中

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
只需從表單中刪除 display() 并單擊 Execute 按鈕。它會(huì)正常工作

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
像這樣嘗試
<!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>
添加回答
舉報(bào)