繁体   English   中英

在php中提交表单后,如何查看jquery成功消息?

[英]How can I view jquery success message after the form submit in php?

我想在php中提交表单后查看jquery成功消息。 我尝试了以下代码。 但是它没有出现。 但是当我用html编写代码时,它就可以了,但是提交后就没有了。 我该如何实现?

这是我在php中的javascript代码

    <?php
         if(isset($_POST['aaa']) and $_POST['aaa']=="Submit"){
    echo '<html>
    <style type="text/css" media="screen">
<!--
.container {width: 670px; margin: 10px auto;}
.messagebox {background-color: #F5F5F5;padding:5px;margin:10px 0px;border: 1px solid #DBDBDB;}
.errorbox {color:#000;background-color:#ffeded;padding:5px;margin:10px 0px;border:1px solid #f27c7c;}
.confirmbox {background-color:#F2FFDB;color:#151515;border:1px solid #9C6;margin:10px 0px;padding:5px;}
-->
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script >
<!--
$(document).ready(function() {
$("#messageBox").addClass("messagebox");setTimeout(function(){
$("#messageBox").fadeOut("slow")}, 4000);
});
function messagebox(){
$("#messageBox").removeClass().addClass("confirmbox").html("Item has been saved").fadeIn(2000).fadeOut(4000);
}
function alertbox(){
$("#messageBox").removeClass().addClass("errorbox").html("Oops, there was an error!").fadeIn(2000).fadeOut(4000);
}
-->

messagebox();
</script>
<body>
    <div class="messagebox" id="messageBox" style="display:none;"></div>
    </body>
    </html>';
    }
?>

您的代码有一些问题,但要回答您的问题,您需要使用ajax调用脚本,并在回调函数中显示以下消息:

$.ajax({
  url: "test.php",
  data: data,
  success: function(message){
   //this will be called once the php has returned an answer
   $("#messageBox").removeClass().addClass("confirmbox").html(message).fadeIn(2000).fadeOut(4000);
   }
})

的PHP

//after processing the data, echo a response:
echo "Message has been saved";

//if an error occured, echo it
echo "an error occured";

更新:

  1. 不要在PHP echo中输出整个页面。 只需关闭php标签并使用常规HTML:

      <?php if(isset($_POST['aaa']) and $_POST['aaa']=="Submit"):?> <html> //regular html code goes here <?php endif;?> 

    在这里查看更多示例。

  2. 您没有任何按钮或表格。 您应该拥有的是:

     <form action="myscript.php" method="post"> <input name="email" type="text"/> //some more inputs <input value="Submit" type="submit"/> </form> 
  3. 现在,您可以将事件绑定到表单提交并调用ajax:

     $('form').submit(function(){ $.ajax({ url: "test.php", data: $(this).serialize(), success: function(message){ //this will be called once the php has returned an answer $("#messageBox") .removeClass() .addClass("confirmbox") .html(message) .fadeIn(2000).fadeOut(4000); } }) }); 

听起来您确实需要学习一些有关Web开发的知识,我建议您从此处开始,并且Web上有很多很棒的资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM