0

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.

3
  • no at the same file within script tag @DaniyalNasir Commented Mar 7, 2018 at 9:27
  • 2
    Why do you want to use ajax, if you are redirecting anways? Submitting the form on the target page as action seems more fitting for this problem. Commented Mar 7, 2018 at 9:31
  • Coz I'm sending data array on button click. This data array produced from element tag and some processing have been done there. Data generated on function postData(). Tell me other workaround to do this. Commented Mar 7, 2018 at 9:33

3 Answers 3

0

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

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.