0

I am working on a login form. The form fields are validated via jQuery . After jQuery validation the control goes to the php page then it fetches the values from the table in the form of array and send it back to the jQuery.

Now the problem is I want to redirect the users to the new page with the values in the array. How can I do that?

Here is my jQuery code:

$.ajax({

    type:'POST',
    url:baseurl,

    data:data1,

    success: function (response){

            if (response != false)
            {

             window.location = index.php?u= "+response;

            }
   }

})

Can I send those values via post method and how do I get those values in index.php page?

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Abbasi
  • 588
  • 1
  • 7
  • 26

2 Answers2

1

EDIT- Perhaps, its better if you change the approach. what you want is a data array in the page in which you get redirect after ajax complete.

so- better use php session.

Your controller-

   class myclass extends CI_Controller {
    function myfunction()
    {
    $data=array(
    // set your data array here
    );
    $this->session->set_userdata($data);
    }
    }

Now when ajax runs this function gets executed and data array is stored in session.

$.ajax({

    type:'POST',
    url:'<?php echo site_url() ?>myclass/myfunction',

    data:data1,

    success: function (response){

            if (response != false)
            {

             window.location = '<?php echo site_url() ?>newcontroller/newfunction';

            }
   }

})

now, retrieve it in your target view.

print_r($this->session->all_userdata());

///////////////////////////////////////////////////////////////////// EDIT ENDS

For what you are asking, you cant acheive it directly, but that does'nt means it cant be done.

You can acheive it by this-

var url = 'http://example.com/vote/';
var form = $('<form action="' + url + '" method="post">' +
  '<input type="text" name="api_url" value="' + Response + '" />' +
  '</form>');
$('body').append(form);
$(form).submit();

Now , on index.php, use $_POST to retrive the value posted by this form.

sunny
  • 1,156
  • 8
  • 15
  • i am actually working on codeigniter and i want to redirect the users to the new page with that array value in post method. From this code how can i redirect the user with the values to the new controller?? – Abbasi Apr 01 '14 at 12:53
  • ok. You should have mentioned this in your question, wait. i will edit my question, perhaps, that can help you. – sunny Apr 01 '14 at 12:56
  • @Mudi i had edited my answer, I hope that will help you! – sunny Apr 01 '14 at 13:05
  • @ I want to session data value of custom data that i have entered into the session using array. how can i do this. $data=array( 'username'=> 'abc' ); $this->session->set_userdata($data); } I want to reterive the value of username in the controller how can i do this i have tried reteriving the value like this $username = $this->session->userdata('username'); echo $username; but it is giving nothing – Abbasi Apr 03 '14 at 07:58
  • @Mudi try- print_r($this->session->all_userdata()); see what it returns – sunny Apr 03 '14 at 10:06
0

You should use absolute URL and put in between quotes :

window.location = "http://domain.com/index.php?u=" + response[0];
ôkio
  • 1,772
  • 1
  • 15
  • 16
  • i have tried this option but its giving the null array on index page. – Abbasi Apr 01 '14 at 12:56
  • The redirection works but the URL is wrong ? The response you're sending back to your jQuery is an array ? Then you just have to pick the first element (or whatever you need) from the array – ôkio Apr 01 '14 at 13:04