当 jquery 对话框关闭刷新该页面时,如何在 jquery 对话框中显示成功消息

发布于 2024-12-21 10:46:05 字数 3036 浏览 0 评论 0原文

成功和失败消息出现在上面的对话框中形成 我需要的是在对话框中显示成功消息而不是表单详细信息,当用户关闭对话框刷新 test.php 页面时我该怎么做?

test.php

<script type="text/javascript">
    <!--
    var currentOperation = "";
    $(function(){
        $('#Add').dialog({
            autoOpen: false,
            width: 600,
            title : 'Add New'
        });
    });
    function openDialog(operation){
        currentOperation = operation; 
        $('#Add').dialog('open');
        $('#Add').html("Loading...");
        $.ajax({
               type: "POST",
               url: "addNewForm.php",                  
               data: "",
               success: function(msg){
                   $('#Add').html(msg);
               },
               error:function(msg){
                     $('#Add').dialog("close"); 
               }               
             });
    }
    function authenticate(){
        $.ajax({
               type: "POST",
               url: "addNew.php",                  
               data: $("#form").serialize(),
               success: function(msg){
                   if(msg=="success")
                   {
                       alert(msg);
                       $('#successful').html(msg);
                   }
                   else
                   {
                       $('#errorMessage').html(msg);
                   }
               },
               error:function(msg){
                    $('#Add').dialog("close"); 
               }               
             });
    }

    //-->
    </script>  

<div style='display: none;' id='Add'></div>
<p><a onclick=openDialog("AddNew") href="#" id="dialog_link"> Add New Details </a></p>

AddNewForm.php

    <div style="color:red" id="errorMessage"></div>
    <div  id="suceesful">
    <form action="addNew.php" method="POST" id="login_form"  onsubmit = "return false;">
    <table>
        <tr>
            <td align="right">user Name:</td>
            <td align="left"><input type="text" name="userName" /></td>
        </tr>
        <tr>

            <td align="right" colspan="2">
                <input name="addDetails" type="button" id="submit" value="submit" onclick ="javascript:authenticate();" />
            </td>
        </tr>
    </table>
    </form>
    </div>  

addNew.php

try{
require_once ('connect.php');
$username = $_POST ['userName'];
if ($username == NULL) {
        throw new Exception ( "A user name is required" );
}
$db->beginTransaction ();
$sql = $db->query(
            "INSERT INTO users (id,userName)
            VALUES( :p_id, :p_userName)", 
            array( 'p_id' => '', 'p_userName' => $userName)  
            );
$db->commit ();
if($sql)
{
    echo "Sucessfully Added";
} else {
    echo "failed";
}
}
catch (Exception $e)
{
    echo $e->getMessage();
}

Success and failure message are appearing in dialog above to form
What i need is showing success message in dialog box instead of form details and when user closes dialog refreshing test.php page how can i do this?

test.php

<script type="text/javascript">
    <!--
    var currentOperation = "";
    $(function(){
        $('#Add').dialog({
            autoOpen: false,
            width: 600,
            title : 'Add New'
        });
    });
    function openDialog(operation){
        currentOperation = operation; 
        $('#Add').dialog('open');
        $('#Add').html("Loading...");
        $.ajax({
               type: "POST",
               url: "addNewForm.php",                  
               data: "",
               success: function(msg){
                   $('#Add').html(msg);
               },
               error:function(msg){
                     $('#Add').dialog("close"); 
               }               
             });
    }
    function authenticate(){
        $.ajax({
               type: "POST",
               url: "addNew.php",                  
               data: $("#form").serialize(),
               success: function(msg){
                   if(msg=="success")
                   {
                       alert(msg);
                       $('#successful').html(msg);
                   }
                   else
                   {
                       $('#errorMessage').html(msg);
                   }
               },
               error:function(msg){
                    $('#Add').dialog("close"); 
               }               
             });
    }

    //-->
    </script>  

<div style='display: none;' id='Add'></div>
<p><a onclick=openDialog("AddNew") href="#" id="dialog_link"> Add New Details </a></p>

AddNewForm.php

    <div style="color:red" id="errorMessage"></div>
    <div  id="suceesful">
    <form action="addNew.php" method="POST" id="login_form"  onsubmit = "return false;">
    <table>
        <tr>
            <td align="right">user Name:</td>
            <td align="left"><input type="text" name="userName" /></td>
        </tr>
        <tr>

            <td align="right" colspan="2">
                <input name="addDetails" type="button" id="submit" value="submit" onclick ="javascript:authenticate();" />
            </td>
        </tr>
    </table>
    </form>
    </div>  

addNew.php

try{
require_once ('connect.php');
$username = $_POST ['userName'];
if ($username == NULL) {
        throw new Exception ( "A user name is required" );
}
$db->beginTransaction ();
$sql = $db->query(
            "INSERT INTO users (id,userName)
            VALUES( :p_id, :p_userName)", 
            array( 'p_id' => '', 'p_userName' => $userName)  
            );
$db->commit ();
if($sql)
{
    echo "Sucessfully Added";
} else {
    echo "failed";
}
}
catch (Exception $e)
{
    echo $e->getMessage();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2024-12-28 10:46:05

嗯..我不确定我是否理解正确,但是..成功后,将消息写入会话,并在页面刷新后检查该值。如果存在,请将其显示在您想要的任何位置并将其从会话中删除。

umm..im not sure if I undestood right but.. after success, write the message to session, and after page refresh check that value. if it's there, show it where ever you want and remove it from session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文