Link to home
Start Free TrialLog in
Avatar of nish darsh
nish darsh

asked on

ajax call to redirect to a different page with data from controller

I am trying to redirect the page when I click on "goback" button. Since I already have an ajax call when I click on "submit" button, I went ahead to add ajax call to "goback" button. I want to redirect the page when I click on "goback" with the data from controller. I am able to see the next page with all the data I need to display in the console, but unable to actually navigate the page. I know that ajax is expecting a response for the request it has sent but I am stuck with the navigation part. Also I am using codeigniter.
I have posted my code below.


view:


 
 <form name="uploadFile" method="post" enctype="multipart/form-data" id="upload-form">
 <p>
<div class="input-files">
  <?php
  foreach ($results as $row) {
   ?>
            <label for="methodname">Method Name:</label>
  <input type="radio" name="taskid" checked="true" id="taskid" value="<?php echo $row->taskid; ?>"/><?php echo $row->taskname; ?><br>
              <input type="radio" name="methodfields" checked="true" id="methodfields" value="<?php echo $row->methodid; ?>"/><?php echo $row->methodname; ?><br>
            <?php } ?>
 </p>
  <b>Step</b><input type="text" name="step" id="step"/><br/>
  <b>Image</b>
  <input type="file" accept="image/png, image/jpeg, image/gif" name="message" id="message" />
</div>
 <input type="button" value="Send Comment" id="submit" name="submit">
   <input class="form-control" type="button" value="Go Back" id="goback" name="goback">
  <ul id="comment"></ul>
</form>

<script type="text/javascript">
$(document).ready(function(){
       $("#goback").click(function(){
         var taskid=$("#taskid").val();
         var methodfields = $('#methodfields').val();
         var formData = new FormData();
         formData.append('taskid',taskid);
         formData.append('methodfields',methodfields);
          formData.append('action','goback');

          $.ajax({
              type:"post",
              url:"<?= base_url() ?>index.php/task/gotomethodlist/",
              data:formData,
              processData: false,
         contentType: false,
         success:function(data){
   //window.location.href = 'goback_view.php';
       }

          });

        });
});

</script>

[b]Controller:[/b]

public function gotomethodlist()
{
$taskid=$this->input->post('taskid');

$data['records']=  $this->db->select('method.methodid,method.methodname,task.taskid,task.taskname')
					  	->from('method')
						->join('task_method', 'method.methodid = task_method.methodid')
						 ->join('task', 'task.taskid = task_method.taskid')
						->where('task_method.taskid',$taskid)
					  	->get()
						->result();

$this->load->view('goback_view',$data);
}

Open in new window



Could someone help me with this.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Can you explain a bit more what you are trying to do?

You click the go back button and you want to send some data to the controller as part of the request - but then you are redirecting to goback_view.php - which means unless you are saving state on the server from the go back AJAX call the data you sent back would be lost.

I suspect you are wanting to go to the goback_view.php page with the data for the controller so that the page you land on has the data you need for that view.

To do that you can't use an AJAX call - AJAX is a separate conversation that happens between browser and server and does not affect the page currently in the browser (unless you write script to do the changes - but nothing is done by default).

To fix your problem you need to either send the data as get parameters in the window.location call OR you need to use the AJAX call and then preserve state on the server through a session and then navigate to the new page and restore state from the session.

Based on the code posted I would use a GET request. Simply make your go back an <a href> element with the url pointing to the destination page and the state variables appended as GET parameters.
If you're using CodeIgniter, you have to redirect to a controller not to a view. But I don't understand the logic: what the ajax call for go back button is epected to do? What type of data processing is done by index.php/task/gotomethodlist/? What is the expected result? Why you don't just redirect the user in the click event?
Avatar of nish darsh
nish darsh

ASKER

Hi Julian Hansen,

Thank you for adding code tags. Will keep this in mind.

"I suspect you are wanting to go to the goback_view.php page with the data for the controller so that the page you land on has the data you need for that view"... This is what I want to do

For this suggestion of yours,
"Based on the code posted I would use a GET request. Simply make your go back an <a href> element with the url pointing to the destination page and the state variables appended as GET parameters"

<?php
  foreach ($results as $row) {
   ?>
            <label for="methodname">Method Name:</label>
  <input type="radio" name="taskid" checked="true" id="taskid" value="<?php echo $row->taskid; ?>"/><?php echo $row->taskname; ?><br>
              <input type="radio" name="methodfields" checked="true" id="methodfields" value="<?php echo $row->methodid; ?>"/><?php echo $row->methodname; ?><br>
            <?php } ?>
<a class="btn btn-primary btn-xl" href="<?php echo site_url('task/gotomethodlist/') ?>" role="button">Go back</a>

Open in new window


Could you please suggest as to how I can pass the taskid value into the controller?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for the help Julian Hansen.. it worked.. i had posted the controller in the beginning and I changed the code in controller to GET parameters instead of POST.
You are welcome.