我试图通过jQuery的阿贾克斯()在codeigniter提交表单...但没有发生... :(表单、发生、jQuery、阿贾克斯

2023-09-10 19:50:07 作者:小伙硬的有内涵i

抱歉,此重复和放大器;重复的问题,但我真的不知道该如何解决。

有一个按键,两个TEXT_INPUT,当我点击这个按钮,如果它的工作,我会看到一个警告,并显示成功给我,但不幸的是,我就是什么都看不见。

什么!!

我不知道为什么:(

请帮助我,请......我看了很多教程计算器或约codeigniter其他网站,但我不觉得什么都可以解决我的问题,请教我的家伙。

下面是低于

一个非常简单的形式

文件名:test.php的

 <!DOCTYPE HTML>
< HTML>
< HEAD>
    <元字符集=utf-8>
    <冠军>测试< /标题>
    <链接相对=样式表的href =< = BASE_URL(/ CSS / bootstrap.css)>>
    <链接相对=样式表的href =< = BASE_URL(/ CSS / basic.css)>>
    <脚本SRC =< = BASE_URL(/ JS / jQuery的-1.10.2.min.js)>>< / SCRIPT>
    <脚本SRC =< = BASE_URL(/ JS / bootstrap.js)>>< / SCRIPT>
    <脚本SRC =< = BASE_URL(/ JS / practice.js)>>< / SCRIPT>
< /头>
<身体GT;

< D​​IV的风格=保证金:19px>
    <形式ID =test_form方法=邮报>
    用户:其中,输入ID =NUMNAME =NUM类型=文本>< BR>
    名称:其中,输入ID =名的名称=名称类型=文本>< BR>
    <输入ID =提交名称=提交级=BTN类型=提交值=保存>
    < /形式GT;
< / DIV>
< /身体GT;
< / HTML>
 
欧冠小球队的 大腿 他们谁会日后登陆豪门

我想这种形式的jQuery阿贾克斯()提交数据,JS文件小于

文件名:practice.js

  $(文件)。就绪(函数(){
    $(#test_form)。递交(函数(五){
        即preventDefault();

        变种TDATA = $(#test_form)serializeArray()。

        $阿贾克斯({
            键入:POST,
            网址:HTTP://localhost/index.php/static_data/test_add
            数据类型:JSON,
            数据:TDATA,

            成功:函数(TDATA)
            {
                警报('成功!');
            }
        });
    });
});
 

和下面是我的控制器

文件名:static_data

 < PHP的,如果(定义(BASEPATH')!)出口('没有直接的脚本允许访问);

类Static_data扩展是CI_Controller {

  公共功能测试()
  {
    $这个 - >负载>查看(测试);
  }

  公共职能test_add()
  {
    $这个 - >负载>模型(纸);
    $这个 - >造纸> test_add();
  }
}
 

和该文件下面是我的模型设定

文件名:paper.php

 < PHP的,如果(定义(BASEPATH')!)出口('没有直接的脚本允许访问);
类纸扩展CI_Model {

  功能__construct()
  {
    父:: __结构();
  }

  功能test_add()
  {
    $这个 - >负载>帮手(形式);
    $这个 - >负载>帮手(HTML);
    $这 - >负载>数据库();
    $ TDATA =阵列(
      NUM'=> $这个 - >输入 - >后期(民),
      '名'=> $这个 - >输入 - >后期(名称),
    );
    $这个 - > DB->插入('TEST_TABLE',$ TDATA);
  }
}
 

解决方案

试试这个 - >在的test.php 文件,给action属性为

 行动=< PHP的回声SITE_URL();?> / static_data / test_add
 

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

  $('#test_form)。递交(函数(){
变种TDATA = $(#test_form)serializeArray()。
VAR的= $(本),
URL = that.attr('行动),
TYPE = that.attr(方法),
数据= TDATA;
 

现在,进行测试,在你的成功函数 practice.js ,只写了以下内容:

 成功:函数(响应){
         的console.log(响应);
}
 

在这里,你得到的回应是你的控制器返回给你,现在就来测试,只需键入回声你好; 或什么,只是呼应了一些东西。 (这是因为你说的教我:))

现在,看看这是工作,开deveoper工具(如果你在谷歌浏览器),转到控制台选项卡,然后从底部,选择日志,如果一切顺利,它会回报你回应的消息写在你的控制器。

另外,看是否表单正确提交后,选择网络选项卡,当你点击提交按钮,网络标签会告诉你,如果该数据已提交正确与否。

试试这个,如果还是不行,发表评论。

sorry for this repeated & repeated question , but I really don't know how to solve.

there was one button and two text_input , when I click this button,if it's work,I will see a alert and show "success" to me , but unfortunately , I just can see nothing.

nothing !!

I don't know why :(

please help me , please ...... I read many tutorials in stackoverflow or other website about codeigniter , but I don't find anything can solve my problem , please teach me guys.

here is a very simple form below

file name: 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>

I want to submit data in this form by jQuery Ajax() , the JS file is below

file name: 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!!');
            }
        });
    });
});

and below was my controller

file name: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();
  }
}

and this file below was my model set

file name: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);  
  }      
}  

解决方案

Try this -> In your test.php file, give the action attribute as

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

Then, in your practice.js file:

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

Now, for testing, in your success function in practice.js, just write the following:

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

Here, the response you get is what your controller returns to you, now to test that, just type echo "hello"; or anything, just echo out something. (This is because you said teach me :) )

Now to see if this is working, open deveoper tools (if you are in google chrome), go to the console tab and then from bottom, select log, if everything is fine, it will return your echoed message written in your controller.

Also, to see if the form is submitted correctly, select the network tab, when you hit the submit button, the network tab will show you if the data has been submitted correctly or not.

Try this, if doesn't work, leave a comment.