Jquery提交单选按钮而不重新加载

时间:2013-08-23 18:25:18

标签: php javascript jquery html mysql

因为我的jquery经验为零,我不知道如何在不重新加载页面的情况下提交表单时运行我的mysql脚本? 简而言之 表单单选按钮提交 - >形式发送 - >没有重新加载的查询

我的剧本:

<form id="myForm" method="get">
    <div class="row">
        <div class="span4 info1">
            <section class="widget">
                <img src="../bootstrap/img/thumb/0.jpg" width="auto" height="auto">
                <?php if ($selectedBg == '0') {
                    echo '<h2>Current one</h2>';
                } ?>

                <input type="radio" name="options[]" value="0" onclick="document.getElementById('myForm').submit();"/>
                Choose Background
            </section>
        </div>
    </div>

    <?php
        $checked = $_GET['options'];
        for ($i = 0; $i < count($checked); $i++) {
            $sql = 'UPDATE blog_users SET background = '.$checked[$i].' WHERE username=?';
            $bg = $db->prepare($sql);
            $bg->execute(array($session->username));
        } 
    ?>      
</form>

所以我的问题是如何在不重新加载页面+运行查询的情况下提交表单?

2 个答案:

答案 0 :(得分:1)

您需要创建一个接受表单数据的单独页面,并使用jQuery的ajax函数异步提交数据。

这是一些伪代码,用以说明如何实现这一目标:

$('form').on('click', 'input:radio', function() {
    $.get('NEW_PAGE_URL' + $(this).serialize(), function(data) {
        // this function is the success function. 
        // your page should return some kind of information to let this function 
        // know that the submission was successful on the server side as well. 
        // This was you can manipulate the DOM to let the user know the submission 
        // was successful.
    });
});

答案 1 :(得分:0)

您所描述的是一种称为AJAX的技术。这是一种并非特定于jQuery,php或mysql的技术。话虽如此,JQuery的常见任务是帮助。

您可以查看此帖子:https://stackoverflow.com/a/5004276/1397590

或者,如果您正在寻找更多教程,请在此处取得高峰:http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

无论哪种方式,都有很多资源可以开始学习AJAX。

相关问题