简体   繁体   English

如何在弹出窗口jquery中显示错误消息

[英]How to display error message in popup jquery

How to display error message in popup and if success message comes hide all the form fields and show success message. 如何在弹出窗口中显示错误消息,如果成功消息隐藏所有表单字段并显示成功消息。

Presently i am doing like this: Placing a success message above the form and closing it after the form close. 现在我这样做:在表单上方放置一条成功消息,并在表单关闭后关闭它。 error message div placing above the form ie inside the success div. 错误消息div放在表单上方,即成功div内。 Please suggest me the best way. 请建议我最好的方法。 Give some sample code so that i can implement this. 给出一些示例代码,以便我可以实现这一点。

my html code: 我的HTML代码:

<div id="successMessage">
<form method="POST" id="update_form"  onsubmit = "return false;">
    <div id="errorMessage"></div>
    <table align="center">
        <tr>
            <td align="right">First Name:</td>
            <td align="left"><input type="text" name="firstName" value="<?php echo $firstName; ?>" /> 
            </td>
        </tr>
        <tr>
            <td align="right">Last Name:</td>
            <td align="left"><input type="text" name="lastName" value="<?php echo $lastName; ?>" /> 
            </td>
        </tr>
        <tr>
            <td align="right" colspan="2">
                    <input type="hidden" name="userId" value="<?php echo $userId; ?>" />
                    <input type="button" name="updateUserDetails" value="Update" onclick="updateUserDetails();">
            </td>
        </tr>   
    </table>
</form>
</div>

jquery Code: jquery代码:

function editUserDetails(userId, operation){
    currentOperation = operation; 
    $('#editUserDetails').dialog('open');
    $('#editUserDetails').html("Loading...");
    $.ajax({
           type: "POST",
           url: "editUserDetails.php?userId="+userId,                  
           data: "",
           success: function(msg){
               $('#editUserDetails').html(msg);
           },
           error:function(msg){
                 //alert(msg);
                $('#editUserDetails').dialog("close"); 
           }
    });
}

function updateUserDetails(){
        $.ajax({
               type: "POST",
               url: "updateUserDetails.php",                   
               data: $("#update_form").serialize(),
               success: function(msg){
                   if(msg=="Updated Successfully")
                   {
                       $('#successMessage').html(msg);
                   }
                   else
                   {
                       $('#errorMessage').html(msg);
                   }
               },
               error:function(msg){
                    $('#editUserDetails').dialog("close"); 
               }               
         });
    });
});

First, don't wrap your form inside the success div, keep it separate like this: 首先,不要将您的表单包装在成功div中,保持它分开如下:

<div id="successMessage"></div>

Next, in the ajax success function just do this to hide the form and display message: 接下来,在ajax成功函数中,只需执行此操作即可隐藏表单并显示消息:

success: function(msg){
         if(msg=="Updated Successfully")
         {
             $("#successMessage").html(msg);
             $("#update_form").hide();
         }
         else
         {
             $("#errorMessage").html(msg);
             $("#errorMessageDialog").dialog();
         }
}

For the error message it is a little bit trickier. 对于错误消息,它有点棘手。 First change the error message div to something like this: 首先将错误消息div更改为以下内容:

<div style="display: none;" id="errorMessageDialog">
<p id="errorMessage"></p>
</div>

Then in the ajax error function open set the error message and open the dialog like this: 然后在ajax错误函数中打开设置错误信息并打开如下对话框:

error:function(msg){
      $("#errorMessage").html(msg);
      $("#errorMessageDialog").dialog();
}

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

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