0

这个问题也和我之前的帖子有关

jquery ajax表单提交不能与codeigniter一起使用,但可以正常工作

我想在不加载新的 php 页面的情况下提交表单。我正在使用代码点火器。当我使用预定义的值运行控制器时,它工作正常并将值插入数据库中。但是当我运行 ajax 时,我的控制器没有运行或没有被调用。这是我的代码:-

<script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script>
    $('#submit_article').click(function(){

        var article_title = document.getElementById("title").value;
        var article_body = document.getElementById("body").value;

        $.ajax({
            url: '<?php echo base_url()."/main/submit_article";?>',
            type: 'POST',
            dataType: 'json',
            data: 'title='+article_title + '&body='+article_body,
            success: function(output_string){
                $('#result_table').append(output_string);
            }
        });
    });
</script>

<form name="article_form" method="POST" action="">
<input type="text" name="title" id="title" placeholder="Title for your article" />
<br>
<textarea rows="12" name="body" id="body" placeholder="Tell your story"></textarea>
<br>
<input type="submit" name="submit_article" id="submit_article" value="Post" />
</form>
4

2 回答 2

1

尝试这个:

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
    $('#submit_article').click(function(e){
       e.prevenDefault();
       $.post(
         '<?php echo site_url("main/submit_article");?>',
         $('#article_form').serialize()
       ).success(function(resp) {
          $('#result_table').append(output_string);
       });
    });
</script>

<form id="article_form" method="post" action="<?php echo site_url('main/submit_article');?>">
    <input type="text" name="title" id="title" placeholder="Title for your article" />
    <br>
    <textarea rows="12" name="body" id="body" placeholder="Tell your story"></textarea>
    <br>
    <input type="submit" id="submit_article" value="Post" />
</form>
于 2013-02-25T06:51:01.570 回答
1

试试这样的代码。更改提交到按钮

<script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script>
    $('#submit_article').click(function(){
       $.ajax({
            url: $('#article_form').attr('url'),
            type: 'POST',
            dataType: 'json',
            data: $('#article_form').serialize(),
            success: function(output_string){
                $('#result_table').append(output_string);
            }
        });
    });
</script>

<form name="article_form" method="POST" action="<?php echo site_url('main/submit_article');?>">
    <input type="text" name="title" id="title" placeholder="Title for your article" />
    <br>
    <textarea rows="12" name="body" id="body" placeholder="Tell your story"></textarea>
    <br>
    <input type="button" id="submit_article" value="Post" />
</form>
于 2013-02-24T18:04:23.773 回答