0

I want to click a button on a page and go to another page with posted data but it's directing without posting ! $_POST is empty and $_SESSION also is empty

here's my ajax function :

function ajax4(Div_Submit,Div_Response,SuccessScript,Data_Submit) 
            {
                var Type = 1;
                var post_data = '';
                if(!Data_Submit) 
                    post_data = $('#'+Div_Submit+' *').serialize();
                else
                    post_data = Data_Submit;
                $.ajax({
                    data: post_data,
                    type: "POST",
                    url: document.URL,
                    dataType: 'text',
                    success:function(response){
                            $('#'+Div_Response).html(response);
                            window.location.href = "index.php";
                    }
                    ,
                    complete: function(){
                        $.unblockUI();
                    }
                });
                return false;
            }

what's wrong with my code ?

SafeY
  • 287
  • 2
  • 6
  • 18
  • It's redirecting because you redirecting it on line `window.location.href = "index.php";` – Muthu Kumaran Dec 04 '12 at 11:52
  • I want it to redirect and get data from the first page "login" to the next page "index " – SafeY Dec 04 '12 at 11:55
  • after redirect you can not pass form post data to other page u need to use session or need to write it in text file or other but after redirecting page form post data can not be received on redirected page. – Dipesh Parmar Dec 04 '12 at 12:04
  • I think you don't need AJAX in this page. Just submit the form like a traditional way and store the values in sessions and then redirect the page from your server side. – Muthu Kumaran Dec 04 '12 at 12:21
  • @DipeshParmar sorry , didn't understand your answer , there's no punctuations ! – SafeY Dec 04 '12 at 12:23
  • I can't , I must use ajax with div , can't use submit with form , I have to keep developing with the same way as my team do – SafeY Dec 04 '12 at 12:25
  • if you can not get posted data using $_POST then user either $_REQUEST or $_GET because in ajax request u have passed post_data and its serialized so u will get those using GET or REQUEST because actually data isnt posted but its passed in url. Check process in firfox addon called firbug u will see header information with data send during request.. – Dipesh Parmar Dec 05 '12 at 04:48

2 Answers2

1

When you call

window.location.href = "index.php";

The browser redirects to the index.php page, not showing the response in Div_Response. If you want to go to index.php with the information from the response and this information is small, you might need to do something like

window.location.href = "index.php?" + encodeURIComponent(response);

but if you are going to redirect all the time, you should let your server do it. You can have a look at this SO answer for how to redirect in php.

Community
  • 1
  • 1
jaudette
  • 2,305
  • 1
  • 20
  • 20
  • I got this error : Request-URI Too Large The requested URL's length exceeds the capacity limit for this server. – SafeY Dec 04 '12 at 12:20
  • it's a login page I want to get the username and password from it , cjeck them and if they are correct , just go the index page – SafeY Dec 04 '12 at 12:21
  • @Safey As i said, the information must be small. Why do you want to display the response and redirect? If you want to change the url but not redirect, see [this answer](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) – jaudette Dec 04 '12 at 12:24
  • @Safey then you should not use ajax. Post the data to your server, let the server reload the page if the credentials are not good or redirect you to index.php is your credentials are ok. – jaudette Dec 04 '12 at 12:29
  • The link in the answer is all about using header and it can't be used with ajax ... I'm forced to use this way , ajax with div – SafeY Dec 04 '12 at 12:34
  • I solved this problem with help of you guys , thank you all :) The answer is : success:function(response){ $('#'+Div_Response).html(response); window.location.href = "index.php"; } without echoing anything in the php page and without if condition . – SafeY Dec 04 '12 at 13:01
0

I solved this problem with help of you guys , thank you all :) The answer is :

success:function(response)
{ 
    //$('#'+Div_Response).html(response); 
     window.location.href = "index.php";
 }

without echoing anything in the php page and without if condition .

SafeY
  • 287
  • 2
  • 6
  • 18