1

我有一个表格,可以使用 ajax 插入到表“标签”中。我可以手动添加,但必须重新加载页面。

这是我的控制器控制器(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

4 回答 4

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!

  });
});
于 2013-05-28T22:56:40.630 回答
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'); ?>",
于 2013-05-29T04:04:14.703 回答
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');


}
于 2017-06-12T12:29:21.380 回答
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);
}
于 2017-06-12T11:27:24.887 回答