Ajax插入代码点火器

时间:2013-05-28 22:32:08

标签: php ajax codeigniter

我有一个表单,它将使用ajax插入到表'tags'中。我能够手动添加但不是没有重新加载页面。

这是我的控制器控制器(tags.php)     

    class Tags extends CI_Controller{

        function  __construct(){
            parent:: __construct();
            $this->load->model('tags_model');
            $this->load->helper('form');
            $this->load->helper('url');
        }

        function index(){
            $data['tags']=$this->tags_model->get();
            $this->load->view('tags/index',$data);

        }

        function add()
        {
            $this->tags_model->save();
             return true;
        }
    }

?>

这是我的观点('index.php')

<script src="<?php echo base_url('assets/js/jquery.js');?>"></script>

<?php
    foreach ($tags as $t){
        echo '<span>';
        echo $t['id'].':';
        echo $t['title'];
        echo '-';
        echo '</span>';
    }
?>


<form id="comment" method="post">
    <?php echo form_input('title','text is here....');?>
    <label>&nbsp;</label><input type="submit" value="Submit" />
</form>

<!-- here is the script that will do the ajax. It is triggered when the form is submitted -->
<script>
   $(function(){
       $("#comment").submit(function(){
         dataString = $("#comment").serialize();

         $.ajax({
           type: "POST",
           url: "<?php echo base_url(); ?>tags/add",
           data: dataString,
           return false;  //stop the actual form post !important!

           success: function(data)
           {
               alert('Successful!');
           }

         });

         return false;  //stop the actual form post !important!

      });
   });
</script>

模型

<?php


    class Tags_model extends CI_Model{
        function __construct()
        {
            parent:: __construct();
            $this->load->database();
        }

        function save()
        {
            $title=$this->input->post('title');
            $data=array(
                'title'=>$title
                );
            $this->db->insert('tags',$data);
        }

        function get(){
            $query=$this->db->get('tags');
            return $query->result_array();
        }
    }
?>

代码对我来说似乎没问题。我可以正常插入但不能在ajax中插入。欢迎任何帮助。

4 个答案:

答案 0 :(得分:2)

删除jquery ajax调用中的return false。

$(function(){
   $("#comment").submit(function(){
     dataString = $("#comment").serialize();

     $.ajax({
       type: "POST",
       url: "<?php echo base_url(); ?>tags/add",
       data: dataString

       success: function(data)
       {
           alert('Successful!');
       }

     });

     return false;  //stop the actual form post !important!

  });
});

答案 1 :(得分:0)

我喜欢这个教程和现在的工作 http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-8-ajax/

现在我的观点就是那样

<script src="<?php echo base_url('assets/js/jquery.js');?>"></script>

<?php
    foreach ($tags as $t){
        echo '<span>';
        echo $t['id'].':';
        echo $t['title'];
        echo '-';
        echo '</span>';
    }
?>


<?php echo form_open('tags/add');?>
    <?php echo form_input('title','text is here....','id="title"');?>
    <?php echo form_submit('submit', 'Submit', 'id="submit"'); ?>
<?php echo form_close();?>

<!-- here is the script that will do the ajax. It is triggered when the form is submitted -->
<script type="text/javascript">

$('#submit').click(function() {

  //var title = $('#title').val();

  var form_data = {
    title: $('#title').val(),
    ajax: '1'   
  };

  $.ajax({
    url: "<?php echo site_url('tags/add'); ?>",
    type: 'POST',
    data: form_data,
    success: function() {
      alert('added Successfully');
    }
  });

  return false;
});

</script>

而不是

 url: "<?php echo base_url('tags/add'); ?>",

我还必须切换到

url: "<?php echo site_url('tags/add'); ?>",

答案 2 :(得分:0)

JS代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function (e) {
            $("#form1").on('submit',(function(e) {
                var name = $("#name").val();
                var address = $("#address").val();
        e.preventDefault();
                // document.getElementById("submit").value="Sending.....";
                $.ajax({
                       url: "<?php echo base_url(); ?>Myconn/added",
                    type: "POST",
                    data:  new FormData(this),
                    contentType: false,
                    cache: false,
                    processData:false,
                    success: function(html)
                    {
                        $("#feed_m").after(html);
                            //document.getElementById("submit").value="submit.";
                            document.getElementById('name').value='';
                            document.getElementById('address').value='';
                        }  
                    });
        }));
        });
    </script>

HTML代码

<div id="feed_m"></div> 
        <form method="post" id="form1" action="">
            <table >
                <tr>
                    <td>name</td>
                    <td><input type="text" id="name" name="name"></td>
                </tr>

                <tr>
                    <td>address</td>
                    <td><input type="text" id="address" name="address"></td>
                </tr>

                <tr>
                    <td></td>
                    <td><input type="submit" id="submit" name="submit" value="submit"></td>
                </tr>

            </table>
        </form>

<强>控制器

public function added()
{

        $data['name']=$this->input->post('name');
        $data['address']=$this->input->post('address');


        $re=$this->model->add('aa',$data);

        if($re)
        {
            ?>
            <script> alert("inserted");</script>
            <?php

        }
        else
        {
            ?>
            <script> alert("not insert");</script>
            <?php
        }
}

<强>模型

public function add($table,$data)
{
    return $this->db->insert($table,$data);
}

答案 3 :(得分:0)

<强>控制器 公共功能添加()     {

        $data['name']=$this->input->post('name');
        $data['address']=$this->input->post('address');


        $re=$this->model->add('aa',$data);

        if($re)
        {
            ?>
            <script> alert("inserted");</script>
            <?php

        }
        else
        {
            ?>
            <script> alert("not insert");</script>
            <?php
        }
        //redirect('Myconn/insert');


}