2015-09-06 67 views
0

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

回答

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."; 
} 
?> 
0

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

1

表单提交将重定向到当前页面或action。 所以从我这里是一个PHP方法。

为您的提交按钮命名。 e.g <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!'; 
}