0

因此,因为我对 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>

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

4

2 回答 2

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.
    });
});
于 2013-08-23T18:35:30.567 回答
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。

于 2013-08-23T18:32:04.293 回答