【问题标题】:I try to submit form by jQuery ajax() in Codeigniter ... but nothing happen ...:(我尝试在 Codeigniter 中通过 jQuery ajax() 提交表单……但什么也没发生……:(
【发布时间】:2013-11-10 11:02:14
【问题描述】:

抱歉这个重复和重复的问题,但我真的不知道如何解决。

有一个按钮和两个 text_input ,当我点击这个按钮时,如果它工作,我会看到一个警报并向我显示“成功”,但不幸的是,我什么也看不到。

什么都没有!!

我不知道为什么:(

请帮助我,请......我在stackoverflow或其他网站上阅读了很多关于codeigniter的教程,但我没有找到任何可以解决我的问题的方法,请教我。

下面是一个非常简单的表格

文件名:test.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test </title>
    <link rel="stylesheet" href="<?=base_url("/css/bootstrap.css")?>">
    <link rel="stylesheet" href="<?=base_url("/css/basic.css")?>">
    <script src="<?=base_url("/js/jquery-1.10.2.min.js")?>"></script>
    <script src="<?=base_url("/js/bootstrap.js")?>"></script>
    <script src="<?=base_url("/js/practice.js")?>"></script>
</head>
<body>

<div style="margin:19px">
    <form id="test_form" method="post">
    USER:<input id="num" name="num" type="text" ><br> 
    NAME:<input id="name" name="name" type="text" ><br>
    <input id="submit" name="submit" class="btn" type="submit" value="save"> 
    </form>
</div>
</body>
</html>

我想通过 jQuery Ajax() 以这种形式提交数据,JS文件在下面

文件名:practice.js

$(document).ready(function(){
    $("#test_form").submit(function(e){     
        e.preventDefault();

        var tdata= $("#test_form").serializeArray();

        $.ajax({
            type: "POST",
            url: "http://localhost/index.php/static_data/test_add",
            dataType: json, 
            data: tdata, 

            success:function(tdata)
            {
                alert('SUCCESS!!');
            }
        });
    });
});

下面是我的控制器

文件名:static_data

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Static_data extends CI_Controller {

  public function test()
  {
    $this->load->view('test');      
  }

  public function test_add()
  {
    $this->load->model("paper");
    $this->paper->test_add();
  }
}

下面这个文件是我的模型集

文件名:paper.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
class Paper extends CI_Model {  

  function __construct()  
  {  
    parent::__construct();  
  }  

  function test_add()
  {  
    $this->load->helper('form');
    $this->load->helper('html');
    $this->load->database();
    $tdata = array(
      'num' =>  $this->input->post('num'),
      'name' =>  $this->input->post('name'),
    );
    $this->db->insert('test_table',$tdata);  
  }      
}  

【问题讨论】:

  • 尝试删除 URL 的 http://localhost 部分,只留下 /index.php/static_data/test_add。还可以考虑添加一个“错误”处理程序。
  • 您看到实际发送的请求了吗? (您可以查看 chrome 的开发者控制台 - “网络”窗格)。想了解它是在客户端还是服务器上停止。

标签: php jquery ajax codeigniter


【解决方案1】:

试试这个 -> 在您的 test.php 文件中,将 action 属性设为

action="<?php echo site_url();?>/static_data/test_add"

然后,在你的 practice.js 文件中:

$('#test_form').submit(function() {
var tdata= $("#test_form").serializeArray();
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = tdata;

现在,为了测试,在 practice.js 的 success 函数中,只需编写以下内容:

success: function(response) {
         console.log(response);
}

在这里,您得到的响应就是您的控制器返回给您的内容,现在来测试一下,只需输入 echo "hello"; 或任何内容,然后回显一些内容。 (这是因为你说教我:))

现在看看这是否有效,打开开发者工具(如果你在谷歌浏览器中),转到控制台选项卡,然后从底部选择日志,如果一切正常,它将返回你写在你的回显消息控制器。

另外,要查看表单是否提交正确,请选择网络选项卡,当您点击提交按钮时,网络选项卡会显示数据是否已正确提交。

试试这个,如果不起作用,请发表评论。

【讨论】:

  • 非常感谢!!!虽然工作简洁....我用你发的方法,ajax也能用,真的谢谢,谢谢!!!但是....数据库出现问题,我使用的代码是utf-8,类型是“char”,但是无论我提交“aaaaa”还是“12345”,它们在数据库中都变成了“0” :(什么是继续?我还需要怎么做?
【解决方案2】:

我认为您可以通过下面给出的这个简单的 ajax 请求轻松解决这个问题:

$(document).ready(function(){

    $("#test_form").on("submit", function(event){
         event.preventDefault();
         $.ajax({
              url : base_url+"static_data/test_add",
              type : "post",
              data : $("#test_form").serialize(),
              dataType : "json",
              success(data)
              {
                  alert('SUCCESS!!');
              } 
         });
    });

});

在这样做之前,请在头部设置的视图文件(test.php)中写下这个

<script>
    var base_url = "<?php echo base_url(); ?>";
</script>

【讨论】:

    【解决方案3】:

    试试这个:

    实践.js

    $(document).ready(function(){
    
        $("#test_form").submit(function(e){     
            e.preventDefault();
    
            var tdata= $("#test_form").serializeArray();
    
            $.ajax({
                type: "POST",
                url: 'http://localhost/[CodeIgniterDirectory]/index.php/static_data/test_add',
                data: tdata, 
    
                success:function(tdata)
                {
                    alert('SUCCESS!!');
                },
                error: function (XHR, status, response) {
                   alert('fail');
                }
    
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 2012-03-22
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多