简体   繁体   English

提交表格后提交成功信息

[英]Present Success Message Upon Form Submission

I have a PHP file with an HTML form, and I want add a success message. 我有一个HTML格式的PHP文件,我想添加一条成功消息。 The success message will be displayed upon form submission, either by use of PHP or JavaScript. 提交表单后,将使用PHP或JavaScript显示成功消息。 The code is presented below. 代码如下。

 <!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> 

It looks like you are POSTing the form data to insert.php . 看来您正在将表单数据发布到insert.php If that is the same page as the form, you could include something like this where you want your success message to show: 如果与表单位于同一页面,则可以在其中显示希望成功消息的地方:

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

If insert.php is another file (that does the processing or something) you could redirect them back to formfile.php?success=true and show the success message with: 如果insert.php是另一个文件(执行处理或其他操作),则可以将它们重定向回formfile.php?success=true并显示以下成功消息:

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

A form submit would redirect to current page or to the action . 提交表单将重定向到当前页面或action So a PHP approach from me it is. 因此,我提供了一种PHP方法。

Name your submit button. 命名您的提交按钮。 eg <input type="submit" name="form-pj-submit" /> 例如<input type="submit" name="form-pj-submit" />

Then when clicked it sets $_POST['form-pj-submit'] 然后在单击时设置$_POST['form-pj-submit']

And in PHP insert.php you can have something like @blazerunner44 mentioned. 在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