亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法通過 HTML 表單調用函數

無法通過 HTML 表單調用函數

眼眸繁星 2023-03-24 14:46:00
我試圖通過提交按鈕調用一個函數,但我無法提交它。<!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> <?phpfunction 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()
查看完整描述

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 文件中


查看完整回答
反對 回復 2023-03-24
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

只需從表單中刪除 display() 并單擊 Execute 按鈕。它會正常工作



查看完整回答
反對 回復 2023-03-24
?
RISEBY

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>


查看完整回答
反對 回復 2023-03-24
  • 3 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號