Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with form POST and redirect

    Hey all - new to the site and could really use some help.

    My situation: I have a form that needs to post results to a specific URL (which is on a different server and I have no control over). Essentially it is a lead generating form...

    But after I post to the URL, I want it then to redirect back to a page on my site, a thank you page.

    I believe I need some sort of form processing script, where the form results are processed, sent to the URL and then the script redirects or displays a Thank you message.

    I don't know how to write such a script though.

    I know that the form action will need to call the script, but I don't know how to pass the results to the processing script nor how to make the script then POST them to the URL.

    Any help would be greatly appreciated - Thanks!

  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Read the script comments and replace the variable values with your webpage urls:

    PHP Code:

    <?php
    //SCRIPT VARS
    //url of ewbpage to submit POST array to
    $curl_url 'http://www.externalDomain.com/thisPage.php';
    //thanks webpage
    $redirect_success 'success.php';
    //error webpage
    $redirect_failure 'failure.php';

    //submit POST array to external webpage
    if(isset($_POST['Submit'])) {

        
    $postVals '';
        foreach(
    $_POST as $k => $v) {
            
    $postVals .= "$k=".urlencode($v);
        }
        
        
    $ch curl_init();
        
    curl_setopt ($chCURLOPT_URL$curl_url);
        
    curl_setopt($curlCURLOPT_POST1);
        
    curl_setopt($chCURLOPT_POSTFIELDS$postVals);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    $result curl_exec($ch);
        
    curl_close($ch);

        
    //here you would normally check for success or failure
        //by searching $result for positive or negative response.
        //for now, just redirect to the thanks page if $result is not empty
        
    if(!empty($result)) $redirect $redirect_success;
        else 
    $redirect $redirect_failure;
        
    header("Location:$redirect");

    }
    ?>
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #3
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Hmm, thought I might add that the form on your webpage submits to its self (self submitting form) and the submit button should be named "Submit".
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • Users who have thanked meth for this post:

    Whoopage (02-27-2008)

  • #4
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your posts - I think this is getting me where I need to be.

    So what should the 'action' look like for a self submitting form? Or what should the whole <form...> stuff look like when self submitting? (for reference I will call the file with the code you gave me processing.php)

    Again - thank you very much for you help

  • #5
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    action="?" will do the trick
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #6
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks - where then do I refer to the file that contains the code you gave me?

    Or does that code go on the same page as the form?

    Also - for reference - my form:

    PHP Code:
    <form method="post" name="debtform" onSubmit="return isValid(document.debtform)" action="?">
                    
    /*form stuffs*/

    <input name="Submit" type="image" src="images/continue.jpg"/>
    </
    form
    Last edited by Whoopage; 02-28-2008 at 01:20 AM.

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    I would suggest using the CURL code posted above, but on a seperate page, and then redirecting back to your form page. Otherwise, if the user hits that page again, whether through a refresh, or the back/forward buttons, that POST data will be resent. Which is probably not what you want.
    If I'm postin here, I NEED YOUR HELP!!

  • #8
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Ultragames: If the user submits the form, they are redirected to a stateless success/error page; refresh will have no effect. I'll code in nocache headers eliminate the resend.

    Whoopage: the final product will look like this:

    PHP Code:

    <?php
    // no using the back button !!
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    // always modified
        
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    // HTTP/1.1
        
    header("Cache-Control: no-store, no-cache, must-revalidate");
        
    header("Cache-Control: post-check=0, pre-check=0"false);
    // HTTP/1.0
        
    header("Pragma: no-cache");

    //SCRIPT VARS
    //url of ewbpage to submit POST array to
    $curl_url 'http://www.externalDomain.com/thisPage.php';
    //thanks webpage
    $redirect_success 'success.php';
    //error webpage
    $redirect_failure 'failure.php';

    //submit POST array to external webpage
    if(isset($_POST['Submit'])) {

        
    $_SESSION['submit_form'] = true;
        
    $postVals '';
        foreach(
    $_POST as $k => $v) {
            
    $postVals .= "$k=".urlencode($v);
        }
        
        
    $ch curl_init();
        
    curl_setopt ($chCURLOPT_URL$curl_url);
        
    curl_setopt($curlCURLOPT_POST1);
        
    curl_setopt($chCURLOPT_POSTFIELDS$postVals);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    $result curl_exec($ch);
        
    curl_close($ch);

        
    //here you would normally check for success or failure
        //by searching $result for positive or negative response.
        //for now, just redirect to the thanks page if $result is not empty
        
    if(!empty($result)) $redirect $redirect_success;
        else 
    $redirect $redirect_failure;
        
    header("Location:$redirect");

    }
    ?>
    <form method="post" name="debtform" onSubmit="return isValid(document.debtform)" action="?">
                    
    <!-- form stuff -->

    <input name="Submit" type="image" src="images/continue.jpg"/>
    </form>
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #9
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I placed the php code on the same page as the form, above all the html code (all the way up above the opening <html>). I changed the variables to direct to the outside website, and the correct error and success page, made sure that the form referred to '?' - submitted and...

    The same page reloaded, and the only difference was a '?' on the end of the URL.

    I didn't mention earlier, and this may be very important - that the page I am supposed to post the results of the form to is (and this is obviously not the whole address, just the end) '...public/LeadImport.asmx/LeadReceiver'

    I don't know if that means PHP will have issues or won't work with it - but it did not seem to work when submit was hit. The page refreshed, but no thankyou page, and no lead ever made it through.

    I hate to keep coming back for help - I know I'm out of my league here, but I really do appreciate all the help from everyone.

    If I need to give more info, please let me know - if you need to see the site, tell me and I'll PM you.

    Thanks again everyone.
    Last edited by Whoopage; 02-28-2008 at 04:56 AM.

  • #10
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Would it matter that I'm sending to a .asmx page?

  • #11
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'm trying to post the form results to an asmx page - does that matter? Will that make curl not work?

    I tried the above code and I get nothing. Could desperately use some help here.

    I want to post to a third party lead collector, and they have given me the following URL to do so. It looks something like this:

    http://www.example.com/.../LeadImport.asmx/LeadReceiver

    Everything works fine when I use that URL in the action of the form, and everything is posted through - they get the lead. The problem is, doing it this way the user is redirected to the above URL and away from my site - and they see the receiving page and that will cause confusion.

    What I need to do is have a form processing script that will take the post results from the form, post to the above URL, but redirect the user to a ThankYou page on my site.

  • #12
    New Coder
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I seem to have figured it out with the following code:

    PHP Code:
    <?php

    //thanks webpage
    $redirect_success '/thankyou.php';
    //error webpage
    $redirect_failure '/failure.php';

    $ch curl_init();

    // Follow any Location headers
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);

    curl_setopt($chCURLOPT_URL'http://www.leadproweb.com/.../LeadImport.asmx/LeadReceiver');
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

    // Alert cURL to the fact that we're doing a POST
    curl_setopt($chCURLOPT_POST1);

    curl_setopt($chCURLOPT_POSTFIELDS"FirstName=$_POST[FirstName]&LastName=$_POST[LastName]&Email=$_POST[Email]&State=$_POST[State]&Zip=$_POST[Zip]&PrimaryPhone=$_POST[PrimaryPhone]&BestTime=$_POST[BestTime]&UnsecureDebt=$_POST[UnsecureDebt]&PmtStatus=$_POST[PmtStatus]&HomeOwner=$_POST[HomeOwner]&VID=##&AID=##&LID=##&IPAddress=$_POST[IPAddress]");

    $output curl_exec($ch);
    curl_close($ch);

    $redirect $redirect_success;

    header("Location:$redirect");

    ?>
    Thanks for all your help everyone!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •