提交表单后JQuery会显示警告吗?

时间:2017-12-15 21:45:40

标签: javascript php jquery

我创建了一个正在运作的表单;我想在用户提交说他们的请求已成功添加时创建警报。这是我的代码:

<form id="target" method="post">
   <label for="firstName">First Name*:</label>
   <input type="text" name="firstName" id="firstName">
   <input type="submit" name="submit" value="Submit">
</form>
<?php 
   if (isset($_POST['submit']) && $statement) 
   { ?>

   <script>
    $("#target").submit(function(event){
    alert("Entry has been succesfully added!");
    event.preventDefault();
    });
   </script>

 <?php 
 } ?>

3 个答案:

答案 0 :(得分:0)

您可以使用bootstrap模式来实现

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
         rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>


 <div class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">form submission</h4>
      </div>
      <div class="modal-body">
        <p> your data submitted successfully </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
   $(document).ready(function(){
       $("#target").submit(function(event){ 
           event.preventDefault();
           $('#myModal').modal('show');
       }):
   })
</script>

答案 1 :(得分:0)

尝试这样..

<form id="target" method="post">
<label for="firstName">First Name*:</label>
<input type="text" name="firstName" id="firstName">
<input type="submit" name="submit" value="Submit">
</form>

<script>
$("#target").submit(function(event){
alert("Entry has been succesfully added!");
event.preventDefault();
});
</script>

答案 2 :(得分:0)

您不需要将警报放在.submit()处理程序中。提交表单后运行PHP代码。提交表单时,页面会重新加载PHP返回的内容,因此它应该返回一个只执行警报的脚本。

<form id="target" method="post">
   <label for="firstName">First Name*:</label>
   <input type="text" name="firstName" id="firstName">
   <input type="submit" name="submit" value="Submit">
</form>
<?php 
   if (isset($_POST['submit']) && $statement) 
   { ?>
   <script>
    alert("Entry has been succesfully added!");
   </script>

 <?php 
 } ?>