1

How can i pass an array with ajax? My code in view:

$(document).ready(function(){
 $('#btn-send').click(function(){
 var value = {
 'name': $('#name').val(),
 'phone': $('#phone').val(),
 'mail': $('#mail').val(),
 };
 $.ajax({
 type: 'POST',
 url: '<?php echo site_url('customer_add/test'); ?>',
 data: value,
 success: function(resp){
 $('#error').html(resp);
 }
 });
 });
});

My test method:

public function test5(){
 $data = $this->input->post["value"];
 $name = $data['name'];
 echo $name; 
}

In params - in the inspect element - there are data but in response, there's nothing to return. What's wrong? Thank's in advance.

Amaan Iqbal
7612 gold badges9 silver badges25 bronze badges
asked May 14, 2016 at 7:30
2
  • try this success : function(data) { //alert("ok"); }, error: function(er){ alert("There is some error"); console.log(er.responseText); } Commented May 14, 2016 at 7:35
  • You are requesting the function test in ajax and in the code you are showing us function test5 in your server script ??? Commented May 14, 2016 at 8:44

2 Answers 2

1

First, you can handle your error at the error section of $.ajax by error: function(xhr) {},

$.ajax({
 type: 'POST',
 url: '<?php echo site_url('customer_add/test'); ?>',
 data: {
 name: $('#name').val(),
 phone: $('#phone').val(),
 mail: $('#mail').val()
 },
 error: function(xhr) {
 alert('error');
 },
 success: function(resp){
 $('#error').html(resp);
 }
});

Next, type: 'POST' means the form would post the fields out by the method POST.
So that we can catch them by $_REQUEST or $_POST.

In Codeigniter, the way to catch the posted fields is $this->input->post('name');.
There's an error in your syntax.

public function test5(){
 $data['name'] = $this->input->post('name');
 $data['phone'] = $this->input->post('phone');
 $data['mail'] = $this->input->post('mail');
 echo $data;
}

Hope it would be help.

answered May 14, 2016 at 18:57

Comments

0

You send data through ajax and you create a array of value with key:value pair when data send to server using post so you can get this whole array in one single variable using.

public function test5(){
 $data = $this->input->post();
 $name = $data['name']; echo $name;
 $phone = $data['phone']; echo $phone;
 $mail = $data['mail']; echo $mail;
}

you dont have to mention value key in $data = $this->input->post["value"];

and the second thing

you are writing wrong syntax $this->input->post["value"];

the write syntax is $this->input->post("value");

but in $this->input->post("value"); case you cant get value because there is no key with value name.

answered May 14, 2016 at 11:07

2 Comments

Thanks quarksera :) You're right. I changed it to data: {value: value}, and now i gat response but the problem is in this way, form validation doesn't work. At first it returns the validation errors but if i write data, the error are still the same and doesn't change!
do you use codeignitor form validation or custom give me some hint otherwise i cant do anything

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.