Insanity Geek
Insanity Geek

Reputation: 157

Pass data array from view to Controller codeigniter

There are a lot of solution for this question.

But I dont know why my code doesnt work.

What I want :

Data Array -> ajax post -> codeigniter controller(save data in DB and redirect to another page)

    function postData(){
    // Generate final Array Code
    //then Post
    $.ajax({
      	 	type: "POST",
       		url: "<?php echo site_url('c/myController/')?>",
       		data: {values:finalArray},
       		success: function(msg){
                    	alert(msg);
          }
    });                  
    }
<button id="submitJson" onclick="postData()">Submit</button>

In my Controller :

function myController()
    {             
            //insert into db as per rule
            $data = "";  
            //then redirection
            $this->load->view('c/redirectionPage', $data);
    }

I dont know why its not working. I tried direct url it works and also ajax success function gives alert message.

Thanks in advance.

Upvotes: 0

Views: 491

Answers (3)

Insanity Geek
Insanity Geek

Reputation: 157

I did it without ajax post. This is how it is done.

on js function 1. generate my array 2. set value to input element 3. submit form

    <form id="formId" name="add-form" method="post" action="<?php echo site_url('c/myController'); ?>">
        <input id="dataArray" name="a" type="text" >
    </form>
function postData(){     
   $('#dataArray').val(JSON.stringify(finalArray));
       $( "#formId" ).submit();
}

Upvotes: 0

Saranya Rajendran
Saranya Rajendran

Reputation: 757

just redirect your page in ajax success function

success: function(msg){
         alert(msg); 
      window.location.href="<?php  echo site_url('c/redirectionPage');?>";
}

it will work.

Upvotes: 0

Hassaan
Hassaan

Reputation: 7672

Your redirect code is not fine. CodeIgniter has own method to redirect the url. Please the following example to redirect the url.

redirect('c/redirectionPage');

Upvotes: 0

Related Questions