提交表格时目前的成功信息

时间:2015-09-06 14:22:45

标签: javascript php html5

我有一个带有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>

3 个答案:

答案 0 :(得分:1)

看起来您正在将表单数据发布到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.";
}
?>

答案 1 :(得分:1)

表单提交会重定向到当前页面或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!';
}

答案 2 :(得分:0)

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;