JQuery UI对话框 - 在对话框中从另一页面加载表单,并在(提交时)在同一对话框中打开处理页面

时间:2011-10-20 16:09:31

标签: php jquery

我有一个非常好的表单(changeuserjudge.php),我已经从一个单独的页面加载到一个对话框中,现在提交表单时,提交页面将在浏览器窗口中打开(而不是在打开的对话框中)。我试图弄清楚如何在按下提交按钮后将处理表单(changeuserjudgeprocess.php)加载到同一个对话框中(并提交POST变量)。我试图避免在同一页面上进行处理。

对话框脚本:

<script type="text/javascript">
    $(document).ready(function(){
        $('#click').click(function(){
            $("#test").load('changeuserjudge.php?user=tim').dialog({modal:true}); 
            $( "#test" ).dialog( "option", "minWidth", 330 );
            $( "#test" ).dialog( "option", "position", 'top' );             
        });
    });
</script>

(测试只是空白页面上的div)

提交表格:

<body>
    <div id="judgesMenu">
    <form action="changeuserjudgeprocess.php" method="POST">

    <?php

        $selectedJudges = array();
        if(isset($_GET['user'])){
            echo '<center><b>User: '.ucfirst($_GET['user']).'</b></center><br />';
        }
        echo '<center><input type="checkbox" id="checkall">Select All &nbsp;&nbsp;&nbsp;<input type="checkbox" id="checknone">Select None</center><br />';
        $listUserJudges = getUserJudges(userNametoID($_GET['user']));
        $arr = array();
        $num = count($listUserJudges);
        if ($num>1){
            foreach ($listUserJudges as $x){
                array_push($arr, judgeIDtoName($x));
            }
        }
        else {
            array_push($arr, $listUserJudges);
        }

        echo '<ul>';
        $list = getActiveJudges();
        foreach ($list as $x){
            $found = 0;
                if (count($arr)>1){
                    foreach ($arr as $y){
                        if ($x == $y){
                            $found = 1;
                            break;
                        }
                    }
                }
                else { 
                    if (judgeNametoID($x) == $arr[0]){
                        $found = 1;
                    }                       
                }
                if ($found==1){
                    echo '<li>';
                    array_push($selectedJudges, judgeNametoID($x));
                    echo '<input type="checkbox" checked="checked" name="checkbox[]" value="'.$x.'" />'.$x.'<br/>';
                    echo '</li>';   
                }
                else {
                    echo '<li>';
                    echo '<input type="checkbox" name="checkbox[]" value="'.$x.'" />'.$x.'<br/>';
                    echo '</li>'; 
                }
        }
            echo '</ul>';
            $_SESSION['selectedJudges']=$selectedJudges;
    ?>

    <input type="hidden" name="user" value="<?php echo userNametoID($_GET['user']); ?>" />

<br/>
    <p><center><input type="Submit" value="Save Changes" id="sub"/></center></p></div>
    </form></body>

1 个答案:

答案 0 :(得分:1)

您是否看过jquery表单插件http://jquery.malsup.com/form/?这是一种非常简单的方法来劫持表单并通过ajax提交它,而不是直接发布到表单url,正如你所发现的那样(如果我理解正确的话)会丢失你的对话框。这是最小的代码。如何对无效表单提交做出反应是下一步要考虑的事情。如果我在正确的方面,请告诉我,我会详细说明。

<script type="text/javascript">
$(function(){
    $("#judgeForm").ajaxForm({
        success: function(){
           // add some code in here to react to the success of your form post
        }
    });
});
</script>
<body>
    <div id="judgesMenu">
    <form action="changeuserjudgeprocess.php" method="POST" id="judgeForm">

</form>
</div>