将jQuery UI Dialog Modal Form数据插入mySQL

时间:2013-01-07 12:18:04

标签: php jquery html ajax user-interface

我正在使用jQuery UI Dialog在我的网站上注册新用户。对话框工作正常。但是,当我提交表单时,AJAX调用没有做任何事情。我在网上搜索了一段时间,但我找不到一个有效的解决方案。这是我目前的代码:

var username=$("#username").val();
var password=$("#password").val();
var checkpassword=$("#checkpassword").val();
var email=$("#email").val();
var user_level=$("#user_level").val();
var date_created=$("#date_created").val();

$( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 500,
    width: 600,
    modal: true,
    buttons: {
        "Registreren": function() {
            var bValid = true;
            allFields.removeClass( "ui-state-error" );

                bValid = bValid && checkLength( username, "username", 3, 16 );
                bValid = bValid && checkLength( email, "email", 6, 80 );
                bValid = bValid && checkLength( password, "password", 4, 16 );
                bValid = bValid && checkLength( checkpassword, "checkpassword", 4, 16 );
                bValid = bValid && checkLength( user_level, "user_level", 0, 2 );
                bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "error" );
                bValid = bValid && checkRegexp( email, "error");
                bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "error" );
                bValid = bValid && checkRegexp( checkpassword, /^([0-9a-zA-Z])+$/, "error" );

            if ( bValid ) {
                $.post({
                url: 'register.php',
                data: 
                        {  
                            username: username,
                            password: password,
                            checkpassword: checkpassword,
                            email: email,
                            user_level: user_level,
                            date_created: date_created
                        }//end data
                })//end AJAX post
           }//End if
        }//End function
    }//end buttons
});//end dialog

我的表单只是一个带有指定ID

的简单HTML输入表单

Ex输入用户名:

<form name="registeracc" id="registeracc" action="register.php" method="post">
<input required type="text" id="username" name="username" class="text ui-widget-content ui-corner-all" />
</form>     

我在网上发现了这个问题的几个“修复”,但它们似乎对我不起作用。

例如:

data: $('#registeracc :input').serialize(),
error: function(xml, status, error) {
$('#registeracc').html('<p>???</p>');

2 个答案:

答案 0 :(得分:2)

我不确定,但根据jQuery API Reference尝试

$.post(
    'register.php',
    {  
        username: username,
        password: password,
        checkpassword: checkpassword,
        email: email,
        user_level: user_level,
        date_created: date_created
    }//end data
)//end AJAX post

答案 1 :(得分:2)

找到解决我问题的答案:

$.post({
url: 'register.php',
data: 
{  
   username: username,
   password: password,
   checkpassword: checkpassword,
   email: email,
   user_level: user_level,
   date_created: date_created
}

应该是:

$.post("register.php", $("#registeracc").serialize());