簡體   English   中英

提交表格后提交成功信息

[英]Present Success Message Upon Form Submission

我有一個HTML格式的PHP文件,我想添加一條成功消息。 提交表單后,將使用PHP或JavaScript顯示成功消息。 代碼如下。

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="layout.css"> <script language="javascript" type="text/javascript"> function getConfirm() { var retVal = confirm("Do you want to continue ?"); if (retVal == true) { document.write("User wants to continue!"); return true; } else { document.write("User does not want to continue!"); location.reload(); return false; } } </script> <title>Title of the document</title> </head> <div> <body> <section id="sidebar"> </section> <section id="content"> <div id="form-div"> <form class="form" action="insert.php" method="post" name="access_form" onSubmit="return getConfirm();"> <ul> <li> <h2>Please Fill The Form</h2> </li> <li> <label for="firstname">First Name</label> <input name="firstname" id="keyword" type="text" placeholder="type first name (required)" required /> </li> <li> <label for="lastname">Last Name</label> <input name="lastname" id="lastname" type="text" placeholder="type second name (required)" required /> </li> <li> <label for="department">Department</label> <textarea name="department" id="department" placeholder="type your department or office name (required)" required ></textarea> </li> <li> <label for="unit">Section/Unit</label> <input name="unit" id="unit" type="text" placeholder="type section/unit name (required)" required /> </li> <li> <label for="request" id="officallabel">Type of Request</label> <input name="request" id="request" list="request1" /> <datalist id="request1" > <?php foreach ($requests as $request): ?> <option value="<?php echo $request; ?>" /> <?php endforeach; ?> </datalist> </li> <li> <label for="purposebuttons" id="officallabel">Purpose</label> <div class="radio"> <input type = "radio" name = "purposebuttons" id = "official" value = "Official" /> <label id="official" for="official">Official</label> <input type = "radio" name = "purposebuttons" id = "unofficial" checked = "checked" value = "Unofficial" /> <label id="unofficial" for="unofficial">Unofficial</label> </div> </li> <li> <label for="personbuttons" id="officallabel">Person</label> <div class="radio"> <input type = "radio" name = "personbuttons" id = "staff" checked = "checked" value = "Staff" /> <label id="staff" for="staff">Staff</label> <input type = "radio" name = "personbuttons" id = "consultant" value = "Consultant" /> <label id="consultant" for="consultant">Consultant</label> </div> </li> <li> <label for="description">Description</label> <textarea name="description" id="description" placeholder="type description (required)" required ></textarea> </li> <li> <label for="date-time">Access Date And Time</label> <input name="date-time" type="datetime-local" id="date-time"/> </li> <p id="form-buttons"> <input type = "reset" class="reset"/> <input type ="submit" class="submit" /> </p> </ul> </form> </div> </section> </body> </div> </html> 

看來您正在將表單數據發布到insert.php 如果與表單位於同一頁面,則可以在其中顯示希望成功消息的地方:

<?php
if(isset($_POST)){
   echo "Success! Thanks.";
}?>

如果insert.php是另一個文件(執行處理或其他操作),則可以將它們重定向回formfile.php?success=true並顯示以下成功消息:

<?php
if(isset($_GET['success']) AND $_GET['success'] == 'true'){
   echo "Success! Thanks.";
}
?>

提交表單將重定向到當前頁面或action 因此,我提供了一種PHP方法。

命名您的提交按鈕。 例如<input type="submit" name="form-pj-submit" />

然后在單擊時設置$_POST['form-pj-submit']

在PHP insert.php您可以提到類似@ blazerunner44的內容。

if (isset($_POST['form-pj-submit'])) {
  // Do whatever
  echo 'SUCCESS!';
}

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="layout.css"> <script language="javascript" type="text/javascript"> function getConfirm() { var retVal = confirm("Do you want to continue ?"); if (retVal == true) { element = document.getElementById("msg"); element.innerText="User wants to continue!"; return true; } else { element = document.getElementById("msg"); element.innerText="User does not want to continue!"; location.reload(); return false; } } </script> <title>Title of the document</title> </head> <div> <body> <section id="sidebar"> </section> <section id="content"> <div id="msg"></div> <div id="form-div"> <form class="form" action="insert.php" method="post" name="access_form" onSubmit="return getConfirm();"> <ul> <li> <h2>Please Fill The Form</h2> </li> <li> <label for="firstname">First Name</label> <input name="firstname" id="keyword" type="text" placeholder="type first name (required)" required /> </li> <li> <label for="lastname">Last Name</label> <input name="lastname" id="lastname" type="text" placeholder="type second name (required)" required /> </li> <li> <label for="department">Department</label> <textarea name="department" id="department" placeholder="type your department or office name (required)" required ></textarea> </li> <li> <label for="unit">Section/Unit</label> <input name="unit" id="unit" type="text" placeholder="type section/unit name (required)" required /> </li> <li> <label for="request" id="officallabel">Type of Request</label> <input name="request" id="request" list="request1" /> <datalist id="request1" > <?php foreach ($requests as $request): ?> <option value="<?php echo $request; ?>" /> <?php endforeach; ?> </datalist> </li> <li> <label for="purposebuttons" id="officallabel">Purpose</label> <div class="radio"> <input type = "radio" name = "purposebuttons" id = "official" value = "Official" /> <label id="official" for="official">Official</label> <input type = "radio" name = "purposebuttons" id = "unofficial" checked = "checked" value = "Unofficial" /> <label id="unofficial" for="unofficial">Unofficial</label> </div> </li> <li> <label for="personbuttons" id="officallabel">Person</label> <div class="radio"> <input type = "radio" name = "personbuttons" id = "staff" checked = "checked" value = "Staff" /> <label id="staff" for="staff">Staff</label> <input type = "radio" name = "personbuttons" id = "consultant" value = "Consultant" /> <label id="consultant" for="consultant">Consultant</label> </div> </li> <li> <label for="description">Description</label> <textarea name="description" id="description" placeholder="type description (required)" required ></textarea> </li> <li> <label for="date-time">Access Date And Time</label> <input name="date-time" type="datetime-local" id="date-time"/> </li> <p id="form-buttons"> <input type = "reset" class="reset"/> <input type ="submit" class="submit" /> </p> </ul> </form> </div> </section> </body> </div> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM