php – jQuery Mobile:如何正确提交表单数据

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – jQuery Mobile:如何正确提交表单数据脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是一个jquery mobile的问题,但它也涉及纯粹的jQuery.

如何将表单数据发布到没有页面转换的页面设置为表单操作属性.我正在建立phonegap应用程序,我不想直接访问服务器页面.

我已经尝试了几个例子,但每次形式转发我到目标PHP文件.

介绍

此示例使用jQuery Mobile 1.2创建.如果你想看到最近的例子,那么看看这个article或这个更复杂的one.你会发现2个工作实例解释了很好的细节.如果您有更多的问题请在文章评论部分.

表单提交是一个不断的jQuery Mobile问题.

几种方法可以实现.我会列出他们中的几个.

示例1:

这是最好的解决方案,以您使用phonegap应用程序,您不想直接访问服务器端PHP.如果您要创建一个phonegap iOS应用程序,这是一个正确的解决方案.

的index.htML

<!DOCTYPE html>
<html>
<head>
    <tITle>jQM Complex Demo</title>
    <Meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0"/>
    <link rel="stylesheet" href="http://code.jquery.COM/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <style>
        #login-button {
            margin-top: 30px;
        }        
    </style>
    <script src="http://www.Dragan-gaic.info/js/jquery-1.8.2.min.js"></script>    
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
    <script src="js/index.js"></script>
</head>
<body>
    <div data-role="page" id="LOGin" data-theme="b">
        <div data-role="header" data-theme="a">
            <h3>Login Page</h3>
        </div>

        <div data-role="content">
            <form id="check-user" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
                <fieldset>
                    <div data-role="fieldcontain">
                        <label for="username">Enter your username:</label>
                        <input type="text" value="" name="username" id="username"/>
                    </div>                                  
                    <div data-role="fieldcontain">                                      
                        <label for="password">Enter your password:</label>
                        <input type="password" value="" name="password" id="password"/> 
                    </div>
                    <input type="button" data-theme="b" name="submit" id="submit" value="Submit">
                </fieldset>
            </form>                              
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>
    <div data-role="page" id="second">
        <div data-theme="a" data-role="header">
            <h3></h3>
        </div>

        <div data-role="content">

        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">
            <h3>Page footer</h3>
        </div>
    </div>
</body>
</html>

check.PHP

<?PHP
    //$action = $_REQUEST['action']; // We dont need action for this tutorial,but in a complex code you need a way to determine ajax action nature
    //$formData = json_decode($_REQUEST['formData']); // Decode JSON object into readable PHP object

    //$username = $formData->{'username'}; // Get username From object
    //$password = $formData->{'password'}; // Get password from object

    // Lets say everything is in order
    echo "Username = ";
?>

index.js:

$(document).on('pagebeforeshow','#login',function(){  
        $(document).on('click','#submit',function() { // catch the form's submit event
        if($('#username').val().length > 0 && $('#password').val().length > 0){
            // Send data to server through ajax call
            // action is functionality we want to call and outputJSON is our data
                $.ajax({url: 'check.PHP',data: {action : 'login',formData : $('#check-user').serialize()},// Convert a form to a JSON string rePResentation
                        type: 'post',async: true,beforeSend: function() {
                        // This callback function will trigger before data is sent
                        $.mobile.showPageLoadingMsg(true); // This will show ajax spinner
                    },complete: function() {
                        // This callback function will trigger on data sent/received complete
                        $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
                    },success: function (result) {
                            resultObject.formSubmitionResult = result;
                                        $.mobile.changePage("#second");
                    },error: function (request,error) {
                        // This callback function will trigger on unsuccessful action                
                        alert('Network error has occurred please try again!');
                    }
                });                   
        } else {
            alert('Please fill all nececery fields');
        }           
            return false; // cancel original event to prevent form submitting
        });    
});

$(document).on('pagebeforeshow','#second',function(){     
    $('#second [data-role="content"]').append('This is a result of form submition: ' + resultObject.formSubmitionResult);  
});

VAR resultObject = {
    formSubmitionResult : null  
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – jQuery Mobile:如何正确提交表单数据全部内容,希望文章能够帮你解决php – jQuery Mobile:如何正确提交表单数据所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。