【问题标题】:Submitting a form through AJAX using CodeIgniter使用 CodeIgniter 通过 AJAX 提交表单
【发布时间】:2010-07-30 00:53:54
【问题描述】:

我正在构建一个 Web 应用程序,它可以让用户以问答形式关注讨论线程。为此,在显示问题时,我在每个问题旁边都有一个“关注”按钮。我希望用户能够在不重新加载页面的情况下关注这些线程,从而使用 AJAX。所以,我希望 AJAX 调用:

1) 提交更新数据库记录以下关系的表格;和 2)用“关注”替换“关注”提交按钮

这是我的 JavaScript 代码:

$("#submitFollow").click(function() {
    var type = $("input#type").val();
    var u_id = $("input#u_id").val();
    var e_id = $("input#e_id").val();
    var dataString = 'type='+ type + '&u_id=' + u_id + '&e_id=' + e_id;

    $.ajax({
        type: "POST",
        url: "<?=site_url()?>/follow/ajaxFollow",
        data: dataString,
        success: function() {
            $('#following').html("Following");
            .hide()
            .fadeIn(1500)
        }
    });
    return false;
});
});

这是我的表单代码的样子。我去掉了阻止表单定期提交的操作和方法。我试图使用 JS 来防止默认,但这没有用。 (数值由模型填写):

<div id="following">
<form id="followForm" action="" method="">
<input type="hidden" name="type" id="type" value="question">
<input type="hidden" name="u_id" id="u_id" value="1">
<input type="hidden" value="12" name="e_id" id="e_id">
<input type="submit" id="submitFollow" value="Follow Question" class="submitFollow">
</form>
</div>

现在控制器非常简单——它只是获取后变量数组并将它们直接提交到数据库。

我不太擅长 JavaScript,所以如果我在一个简单的问题上占用您的时间,我深表歉意。

【问题讨论】:

    标签: javascript ajax forms codeigniter


    【解决方案1】:

    我在这里只是笼统地说,但希望它会有所帮助。如果是我,我会这样做:

    这是一些从 codeigniter 生成的 html:

    <div>
    questions 1
    </div>
    <div>
    <a class="follow" href="http://example.com/follow/question/1">Follow Question 1</a>
    </div>
    
    <div>
    questions 2
    </div>
    <div>
    <a class="follow" href="http://example.com/follow/question/2">Follow Question 2</a>
    </div>
    

    然后让一切在没有 javascript 的情况下运行(稍后添加)。所以这里是带有一些通用代码的 codeigniter 控制器:

     <?php
    
    class Follow extends Controller {
    
        function Follow()
        {
            parent::Controller();
            $this->auth->restrict_to_admin();
        }
    
        function question($id = NULL)
        {
            if($id)
            {
                //get question from database using the id
                //i assume somehow the user is logged in? use their id where applicable
                echo '<span>You are now following this question</span>';
            }
        }
    
    }
    
    /* End of file follow.php */
    

    现在,无论他们是否使用 javascript,它都能正常工作。这是您添加 javascript 的地方(我假设您使用的是 jquery?)。如果没有,它会足够接近。

    $(document).ready(function(){
        $('follow').click(function(event){
            event.preventDefault();
    
            var followUrl = $(this).attr('href');
            //do ajax call here.
            $.post(
            followUrl,
            {
                //any paramaters you need to add can
                //go here in this format:
                //param_key: param_value
            },
            function(responseText){
                //here you can update the clicked link to say something
                //like "you are now following this question"
                //the responseText var will have whatever text the server responds with.
                //just responsd with html
            },
            'html'
            );
    
        });
    });
    

    【讨论】:

    • 我非常喜欢这个解决方案,但我无法让 event.preventDefault() 为我的网站做任何事情。记录被添加到数据库中就好了,但是链接有效,没有 Ajax。我已经安装了 jquery 并将它用于其他事情就好了,但我无法停止任何表单或链接的默认活动。输入一个 return false;也不做任何事情。有什么建议吗?
    • 好吧,我不确定它到底做了什么,但我终于让它工作了。我将 event.preventDefault() 替换为 return false 并将其移至函数的末尾。到目前为止,一切顺利。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多