简体   繁体   English

如何通过AJAX调用将表单数据传递给PHP(CodeIgniter Framework)

[英]How to pass form data to PHP via AJAX call (CodeIgniter Framework)

I have the following HTML structure: 我有以下HTML结构:

<form method="POST">
    Name : <input id="asgname" type="text"> <br>
    Description : <input id="asgdescription" type="text"> <br>
    <a href="#" id="asgsave" class="assignment-action">Save</a>
</form>

I want that on clicking the save button, the form values are sent to the server via AJAX call. 我希望在单击“保存”按钮时,将表单值通过AJAX调用发送到服务器。 For that, I've attached the click event via following command 为此,我通过以下命令附加了click事件

$("#asgsave").click(save_assignment);

save_assignment is also a javascript function defined as follow: save_assignment也是一个javascript函数,定义如下:

function save_assignment() {
     return $.ajax({
           type: "POST",
           url:  "<?php echo base_url();?>index.php/user/save_assignment",
           data: $('form').serialize(),
                success: function(response) {
                    alert('Form was submitted');
                },
                error: function(error) {
                    alert("Error");
                }
     });
}

The above is not working. 上面的方法不起作用。 So I tried the following approach as well: 因此,我也尝试了以下方法:

function save_assignment() {
     var formvalues = {
            name       : $('#asgname').text(),
            descripion : $('#asgdescription').text(),
     };
     return $.ajax({
           type: "POST",
           url:  "<?php echo base_url();?>index.php/user/save_assignment",
           data: {values : formvalues},
                success: function(response) {
                    alert('Form was submitted');
                },
                error: function(error) {
                    alert("Error");
                }
     });
   }

But this is also not working. 但这也不起作用。

Can anyone please guide me as to why are the above methods not working and what is the correct way to do so ? 谁能指导我上述方法为何不起作用,正确的方法是什么?

EDIT-1 : 编辑1:

By not working, I mean: in the first case ($('form').serialize() approach) the data is not being sent to the server. 通过不工作,我的意思是:在第一种情况($('form')。serialize()方法)中,数据未发送到服务器。 I checked it via chrome and firefox debugger that there was no post data sent corresponding to the form. 我通过chrome和firefox调试器检查了是否没有发送与表单相对应的帖子数据。

And in the second case, empty values are being sent. 在第二种情况下,将发送空值。 ie the post data sent to server is like following: 即发送到服务器的帖子数据如下:

values[name]
values[description]

ie the above values are empty. 即上述值是空的。

EDIT-2: 编辑2:

By logging on firephp, I have confirmed that the save_assignment PHP script is BEING EXECUTED. 通过登录firephp,我已经确认save_assignment PHP脚本已被执行。 Thus ajax call is working fine but it is NOT passing the data correctly. 因此,ajax调用工作正常,但未正确传递数据。

Try to use event.preventDefault() like, 尝试使用event.preventDefault()之类的方法,

$("#asgsave").on('click',function(e){
   e.preventDefault();
   save_assignment();
});

or use return false after ajax call like, 在ajax调用之后使用return false

function save_assignment() {
    //ajax code
    return false;
}

you have to use callbacks in the success function, cause ajax is asynchronously 您必须在成功函数中使用回调,因为ajax是异步的

edit 编辑

have you already tried console.log('foo'); 您是否已经尝试过console.log('foo'); in your function? 在你的职能? so you are able to test if the function is called ;) 这样您就可以测试该函数是否被调用;)

edit 2 编辑2

add the following line to your ajax options 将以下行添加到您的ajax选项

dataType: "json"

You could just serialize your form values: 您可以serialize表单值:
http://api.jquery.com/serialize/ http://api.jquery.com/serialize/

However, looking over your code, I'll take a stab and guess that you are not getting and sending your values properly. 但是,查看您的代码,我会遇到一点麻烦,并猜测您没有正确获取和发送值。 Using jQuery, you grab a value from input like so: 使用jQuery,您可以像这样从输入中获取值:

$('#idofInput').val();

http://api.jquery.com/val/ http://api.jquery.com/val/

You are doing: $('#asgname').text() 您正在执行: $('#asgname').text()

Format your data properly: data : { foo : 'bar', bar : 'foo' } 正确格式化数据: data : { foo : 'bar', bar : 'foo' }

One last guess, make sure your CodeIgniter config has CSRF protection disabled, otherwise you would need to pass: $this->security->get_csrf_token_name() & $this->security->get_csrf_hash(); 最后一个猜测,请确保您的CodeIgniter配置禁用了CSRF保护,否则您需要通过: $this->security->get_csrf_token_name()$this->security->get_csrf_hash();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM