1

I'm trying to send data by post using ajax (with codeigniter) and I don't know why but I don't receive anything...

This is how I send it:

var sendData = $('#formContact').serialize();
$.ajax({
    type: 'POST',
    url: '<?php echo base_url()?>/intranet/update/updateProfile',
    data: sendData,
    dataType: 'json',
    success: function (data) 
    {
        console.log(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

and this is an example of my form:

<form id="formContact" action="update" method="POST">
    <input class="headInput" type="text" name="userName" value="Tito"/>
    <input class="headInput" type="text" name="userLastName" value="Lancreo"/>
    <input class="headInput" type="text" name="phone[]" value="666666"/>
    <input class="headInput" type="text" name="phone[]" value="111111"/>
    <input class="headInput" type="text" name="phone[]" value="222222"/>
</form>

And when I debug it, I always get 0...

[false, false, Array[0], false, null]

My controller:

$this->load->helper('form');
$this->load->library('form_validation');

//1 way
$ret=$this->input->post();

//2 way        
$return=$this->input->post(NULL, TRUE);

//3 way
$all=$_POST;
json_encode($all);

//4 way
$contact=$this->input->post("userName");

//return everything...
$var[0]=$return;
$var[1]=$contact;
$var[2]=$all;
$var[3]=$ret;
$var[4]=$data;
echo json_encode($var);

How can I fix it??

6
  • 1
    what is inside sendData ? Commented Jan 5, 2015 at 9:20
  • 1
    Hope $.ajax in document ready Commented Jan 5, 2015 at 9:22
  • may be this can help you stackoverflow.com/questions/19395354/… Commented Jan 5, 2015 at 9:34
  • in sendData I have my form serialize, I update it. And yes, is in document.ready. Commented Jan 5, 2015 at 9:35
  • the same, it doesn't work Commented Jan 5, 2015 at 10:03

2 Answers 2

0

SOLVED!

The problem was not to replace with:

serialize().replace(/%5B%5D/g, '[]');

But I think it's usefull...

My problem was that I'm using a library for internationalization (https://github.com/bcit-ci/CodeIgniter/wiki/CodeIgniter-2.1-internationalization-i18n) and I must add language to my url, even if I change my routes.php

url: '<?php echo base_url()?>en/intranet/update/updateProfile'

Thanks a lot!

Sign up to request clarification or add additional context in comments.

Comments

-1

The issue, as it seems, Is the serialize itself.

As can be seen here :

How to send serialize form data using JQuery if the input element is an array

Serialize has an issue with an array in the input fields, It replaces the square barckets :

The fiddle :
http://jsfiddle.net/3vr0dtgn/

from my fiddle:

data = $('form').serialize();
$('div').append(data);

Using the stackoverflow I supplied above gives the solution(regex replacing certain elements)

4 Comments

As I have tested it. It works without any issue it's just an encoding done by serialize.
@IndrasinhBihola Well I did not invent the wheel, For me strictly fixing the encoding with a regex solved the issue.
It doesn't work... It is true that it replace it, but in my controller, I still have no data in _POST. But I've just found the solution. Thanks!
Great to hear, Answer your own question for future generations :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.