0

I have a form on page1.php which redirects to page2.php, I want to pass the data from the form to page3.php immediately on load of page2.php. (the user doesn't need to see what's going on in page3, only page2)

What should I use to pass the variables? Currently I'm using

page1.php

    <html>
            <form action=page2.php method=post>
                    <!-- form content here incl name attr for input elements -->
            </form>
    </html>

page2.php

    <body>
    <?php
            $var1 = $_POST['name1']; // int   
            $var2 = $_POST['name2']; // int   
            $var3 = $_POST['name3']; // int 
            $var4 = $_POST['name4']; // str
    ?>
    <!-- some code here -->
    <script>
            var var1 = <?php echo $var1; ?>;        
            var var2 = <?php echo $var2; ?>;        
            var var3 = <?php echo $var3; ?>;        
            var var4 = "<?php echo $var4; ?>";        
            $.post('page3.php',{var1: var1, var2: var2, var3: var3, var4: var4});         
    </script>
    </body>

page3.php

    <?php        
            $var1 = $_POST['var1'];
            $var2 = $_POST['var2'];
            $var3 = $_POST['var3'];
            $var4 = $_POST['var4'];
    ?>

a. This seems like way too much to me, are there any jquery shortcuts? How can I use serialize to help me? b. this isn't working entirely... I think there's some problem with the $.post, maybe I'm not triggering it well? I am not sure.

Help would be appreciated.

hakre
  • 193,403
  • 52
  • 435
  • 836
Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76

1 Answers1

0

By doing this way your completely defeating the first A of ajax, asynchronous. What you should do is add the ajax call to the submit event of the form on your first page.

<form action="action.php" method="GET" id="form">
    <input name="var1" />
    <input name="var2" />
    <input name="var3" />
    <input name="var4" />

    <input type="submit" />
</form>

<script>
$('#form').submit(function() {
    $.post('action.php', {
        var1: $('input[name="var1"]').val(),
        var2: $('input[name="var2"]').val(),
        var3: $('input[name="var3"]').val(),
        var4: $('input[name="var4"]').val()
    });        

    return false; //Prevent form from actually submitting
});
</script>

This way if the user has javascript, the form is submitted asynchronously (not requiring a new page to be loaded) by jQuery. But if the user doesn't have javascript, the form is submitted normally.


To ensure that your page3.php (what I called action.php) continues to execute even after the user navigates away you should look into ignore_user_abort() and set_time_limit().

//At the top of page3.php
ignore_user_abort(true);  //Allow user to navigate away
set_time_limit(0);        //Allow script to run indefinitely

And as I suggested above, you should still submit the form with AJAX like I did. The two PHP functions I mentioned above will eliminate the need for the intermediary script, page2.php. Like so:

page1.php

<form action="" method="get" id="form">
    <input name="var1" />
    <input name="var2" />
    <input name="var3" />
    <input name="var4" />

    <input type="submit" />
</form>

<script>
$('#form').submit(function() {
    $.post('page3.php', {
        var1: $('input[name="var1"]').val(),
        var2: $('input[name="var2"]').val(),
        var3: $('input[name="var3"]').val(),
        var4: $('input[name="var4"]').val()
    });        

    return false; //Prevent form from actually submitting
});
</script>

The user can now fill out page1.php and hit the submit button. jQuery will intercept the form submit and send the data via AJAX to page3.php.

page3.php

ignore_user_abort(true);  //Allow user to navigate away
set_time_limit(0);        //Allow script to run indefinitely

//Rest of processing code...

page3.php will receive the AJAX request and begin to do its work. When the user navigates away from page1.php the AJAX request will be canceled (and the connection to the request for page3.php will be lost), but page3.php will continue to run.

Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
  • no!! this is totally NOT what I need. I know what ajax IS. The ajax call will take about an hour and I want the user to go to page2.php right away and let page3.php run in the background. Read my question here too: http://stackoverflow.com/questions/8768470/submit-form-with-ajax-but-redirect-to-another-page/8768496#8768496 – Lucy Weatherford Jan 08 '12 at 07:55
  • @LucyWeatherford I'm sorry. That wasn't very clear when I first read your question. Allow me to update my post to include relevant information. – Bailey Parker Jan 08 '12 at 08:04
  • okay thanks!! this looks very promising :-) but how *do* I then redirect to page2.php? after all, the user submitted the form, now I want to redirect the user to somewhere else, upon submitting the form. – Lucy Weatherford Jan 08 '12 at 08:20
  • oh I know! I'll add href.location after .post in the form submit! – Lucy Weatherford Jan 08 '12 at 08:22
  • btw - why is there `false` twice in the code? one is not to redirect to the real form location, what is the other one? – Lucy Weatherford Jan 08 '12 at 08:23
  • @LucyWeatherford The false was a bit of careless coding by me. I started by using `addEventListener('submit', function() {}, false);` but noticed you were using jQuery and just forgot to remove the false. I'll update my post again to include a complete demo. – Bailey Parker Jan 08 '12 at 08:26
  • @LucyWeatherford I've added it to the end of my post. – Bailey Parker Jan 08 '12 at 08:49
  • @LucyWeatherford Ignore that. That's a copying error. It should work now. – Bailey Parker Jan 08 '12 at 08:55
  • also, how can I add to the `$.post` the following `window.location.href = 'http://www.page2.php';` - there is a `return false` in there, so should I insert this before or after the `return false` - if I do it after, the return will prevent it from running. If I do it before, then `return false` doesn't run, but if it is redirected to `page2.php`, and `page3.php` is running in the background, then the result is there, right? and there is actually no need for `return false`, is that correct? – Lucy Weatherford Jan 08 '12 at 08:56
  • @LucyWeatherford It depends on what you are trying to do. If it is important that your users see page 2 then just move your AJAX call back to page 2 (but leave the functions at the top of page 3 that allow it to run indefinitely). But if the only thing page 2 does it send an AJAX request to page3 then it is unnecessary. – Bailey Parker Jan 08 '12 at 08:59
  • yes, users *do* need to see page2. How to move the ajax to page2? (that's my origianl question!). I thought maybe to do the following, not sure it will work: `$('#form').submit(function() { $.post('page3.php', { var1: $('input[name="var1"]').val(), var2: $('input[name="var2"]').val(), var3: $('input[name="var3"]').val(), var4: $('input[name="var4"]').val() }); window.location.href = 'page2.php'; });` – Lucy Weatherford Jan 08 '12 at 09:22
  • @LucyWeatherford You can make the action of the form page2 and remove your `window.location.href`. This way on page1 AJAX will intercept and send a request to page3. Then the form will be submitted to page2 (which shouldn't perform any AJAX requests because the original is still processing) and the user will see page2. – Bailey Parker Jan 08 '12 at 09:28
  • oh why can't page2 send any ajax requests? this may explain a problem i've been having there which I thought was unrelated! – Lucy Weatherford Jan 08 '12 at 09:40
  • Yes remove the `return false`. Since the form is on page1 it makes sense to submit the request for page1. It is unnecessary to have page1 submit the form to page2 and then page2 submit the information to page3. If page1 requests page3 with the form information via ajax and then submits the actual form to page2, page2 doesn't have to bother with the data. This is merely a suggestion though. You don't have to. It's just my recommendation. – Bailey Parker Jan 08 '12 at 10:24
  • Hi, I tried it BOTH ways, and it isn't working! I don't know what to do - page3.php works stand alone, but when I tried to run it now it didn't work :( – Lucy Weatherford Jan 09 '12 at 00:33