0

好的,我已经看到很多关于提交表单而不刷新的方法,我觉得有几种方法可以通过提交或仅使用按钮(差异/最佳?)来做到这一点,尽管搜索了无数帖子,但我仍然遇到问题. 所以:

$(function() {
    $('form').on('submit', function(e) {
        e.preventDefault();
        var privacy = $('input:radio[name=privacy]:checked').val();
        var invites = $('input:radio[name=invites]:checked').val();
        var posts = $('input:radio[name=posts]:checked').val();
        var dataString = 'privacy=' + privacy + '&invites=' + invites + '&posts=' + posts;
        alert(dataString);
        $.ajax({
            type: "POST",
            url: "editOptions.php",
            data: dataString,
            success: function() {
                $('#admin').append("<div id='message'</div>");
                $('#message').html("Changes Saved");
            }
        });
        return false;
    });
});

和:

<form action="" method="post" name="editOptions">

Privacy
<input type="radio" name="privacy" id="privacy" value="public" 
<?php if($privacy == "public") { ?>checked="checked"<?php } ?>> 
Public

<input type="radio" name="privacy" id="privacy" value="private" 
<?php if($privacy == "private") { ?>checked="checked"<?php } ?>> 
Private

Invites
<input type="radio" name="invites" id="invites" value="adminonly" 
<?php if($invite == "adminonly") { ?>checked="checked"<?php } ?>> 
Admin Only

<input type="radio" name="invites" id="invites" value="everyone" 
<?php if($invite == "everyone") { ?>checked="checked"<?php } ?>> 
Everyone

Posts
<input type="radio" name="posts" id="posts" value="adminonly" 
<?php if($post == "adminonly") { ?>checked="checked"<?php } ?>> 
Admin Only

<input type="radio" name="posts" id="posts" value="everyone" 
<?php if($post == "everyone") { ?>checked="checked"<?php } ?>> 
Everyone

<button>Save Changes</button>
</form>

所以只是一个带有 3 组 2 个单选按钮的表单。因此,我认为问题出在许多教程和帖子中,当您“提交”或单击“按钮”时,我结合了要做什么,但也许其他人可以看到我错过的简单内容。

编辑 editOptions 以防万一

<?php 
include 'config.php';

$privacyX = $_POST['privacy'];
$invitesX = $_POST['invites'];
$postsX = $_POST['posts'];
$groupAd = $_POST['groupName'];

$sql = "UPDATE `options` SET privacy='$privacyX', invite='$invitesX', post='$postsX' WHERE adminGroup = '$groupAd'";

if(!mysql_query($sql, $link))
{
    die('Error: ' . mysql_error()); 
}

mysql_close($link);

?>
4

2 回答 2

1

完整的工作示例:

     <script type="text/javascript">
        $(function() {
            $('.button1').click(function(e) {
                e.preventDefault();
                var privacy = $('input:radio[name=privacy]:checked').val();
                var invites = $('input:radio[name=invites]:checked').val();
                var posts = $('input:radio[name=posts]:checked').val();

                console.log(privacy, invites, posts);
                //return;
                //alert(dataString);
                $.ajax({
                    type: "POST",
                    url: "editOptions.php",
                    data: {privacy: privacy, invites: invites, posts: posts},
                    success: function(result) {
                        $('#admin').append("<div id='message'>Changes Saved</div>");
                        console.log(result);
                        console.log("success");
                    }
                });
            });
        });
     </script>
</head>
<body>
    <div id="admin">
    </div>
    <form action="" method="post" name="editOptions">

        Privacy
        <input type="radio" name="privacy" id="privacy" value="public" > 
        Public

        <input type="radio" name="privacy" id="privacy" value="private" > 
        Private

        Invites
        <input type="radio" name="invites" id="invites" value="adminonly" > 
        Admin Only

        <input type="radio" name="invites" id="invites" value="everyone" > 
        Everyone

        Posts
        <input type="radio" name="posts" id="posts" value="adminonly" > 
        Admin Only

        <input type="radio" name="posts" id="posts" value="everyone" > 
        Everyone

        <button class="button1">Save Changes</button>
    </form>


</body>

于 2013-11-13T04:33:47.970 回答
0

都整理好了。这是 editOptions 的问题,没有传递正确的变量名。:Pty

于 2013-11-14T01:35:41.907 回答