/    Sign up×
Community /Pin to ProfileBookmark

Redirecting after form is submitted

Say I have a form like so:

<form action=”ex.php” method=”post” name=”frm”>
<input type=”text” name=”fn” />
<input type=”text” name=”ln” />

<input type=”button” value=”submit” />
</form>

I need these input values to be posted to ex.php and once submission is done redirected to a different .php or .html page after. Basically, I would like it to look like the viewer never even went to ex.php. Does that make sense? Sorry if I am confusing!!!!

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@tirnaAug 12.2010 — You can use header() in ex.php to redirect to another url.
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 12.2010 — My example was kind of off...sorry. I need the form sent to another url that collects the data and then redirected back to my url. So basically, the viewer will fill out the form, data will be submitted to http://www.exampledatabasesite.com and then I need to redirect back to my page. Is that even possible if I don't have control over the database site's files?
Copy linkTweet thisAlerts:
@NogDogAug 12.2010 — Rather than redirecting, I'd probably use the [url=http://php.net/curl]cURL functions[/url] to submit the data from your form-handler page to the other URL, then parse the response and display whatever you want as a result of it.
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 12.2010 — How exactly would I apply this to my form??? Is the CURLOPT_URL my html form page? Where do I put the url where I want the form data to be posted?

[CODE]<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/path/to/form");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
'foo' => 'foo foo foo',
'bar' => 'bar bar bar',
'baz' => 'baz baz baz'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);[/CODE]
Copy linkTweet thisAlerts:
@NogDogAug 12.2010 — Actually, I think I read more into your question than was there. For some reason I was thinking you wanted to submit the data to another site, then have it redirect back to yours. I'm a bit tied up at the moment, but I'll get back here later with some ideas.
Copy linkTweet thisAlerts:
@NogDogAug 12.2010 — So anyway, all you need to do is have ex.php do all the nitty gritty stuff to process the form input without outputting anything (including anything not within the <?php tags), then do a header() to the desired "landing page".
[code=php]
<?php
/*
process the $_POST data...
then...
*/
header("Location: http://yoursite.com/success.html");
exit;
?>
[/code]

However, I often like to have the form page also be the processing page, so that it's easy to redisplay the form with any errors should the user screw up somewhere, so it might go something like:

[code=php]
<?php
if(!empty($_POST)) {
/*
/ Process the form data
/ write any errors to the $errors array
*/
if(empty($errors)) {
header("Location: http://yoursite.com/success.html");
exit; // we're done here and don't want to do anything else
}
}
<html><head><title>form</title></head><body>
<h1>My Form</h1>
<?php
if(!empty($errors)) {
echo "<div class='error'>n";
foreach($errors as $err) {
echo "<p>$err</p>n";
}
echo "</div>n";
}
?>
<form action="" method="post"> <!-- empty action submits to self -->
<div>
<input type="text" name="foo" value="<?php
if(isset($_POST['foo'])) { echo htmlentities($_POST['foo'], ENT_QUOTES); }
?>" /><br />
<input type="text" name="bar" value="<?php
if(isset($_POST['bar'])) { echo htmlentities($_POST['bar'], ENT_QUOTES); }
?>" /><br />
<input type='submit' value='Submit' />
</div>
</form>
</body></html>
[/code]
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 13.2010 — For some reason I was thinking you wanted to submit the data to another site, then have it redirect back to yours.[/QUOTE]

This is exactly what I want (what you said above)...

Sorry I made things so confusing. I need to submit data to another URL and redirect back to mine. I have been reading up on CURL, but haven't figured it out yet.
Copy linkTweet thisAlerts:
@NogDogAug 13.2010 — This is exactly what I want (what you said above)...

Sorry I made things so confusing. I need to submit data to another URL and redirect back to mine. I have been reading up on CURL, but haven't figured it out yet.[/QUOTE]


Heh...I guess I'm just not with it today...sort of a virtual Friday for me. ?

I've found I've had better success setting the data as a string instead of an array (being sure to urlencode the data):
[code=php]
$data = array();
$data[] = 'foo='.urlencode($_POST['foo']);
$data[] = 'bar='.urlencode($_POST['bar']);
// etc....
// use implode() to create the data string:
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
[/code]
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 17.2010 — Thanks for all of your help, however, I am a little confused. Where do you store the URL where the data is being sent?
Copy linkTweet thisAlerts:
@NogDogAug 17.2010 — The rest of your cURL code looked OK, I was just suggesting a change to how you use curl_setopt() for the CURL_POSTFIELDS setting. If we assume $data is an array that has your form data to be transmitted, where the array keys are the field names, you can use a little looping to do it:
[code=php]
$post_data = array();
foreach($data as $key => $value) {
$post_data[] = urlencode($key)."=".urlencode($value);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/path/to/form");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $post_data));
$output = curl_exec($ch);
/* now parse $output to get the results of the transaction */
[/code]
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 17.2010 — [CODE]curl_setopt($ch, CURLOPT_URL, "http://www.example.com/path/to/form");[/CODE]

So this is the path to my html with the form on it?

Where do I put the URL that the form is being sent to?
Copy linkTweet thisAlerts:
@NogDogAug 17.2010 — [CODE]curl_setopt($ch, CURLOPT_URL, "http://www.example.com/path/to/form");[/CODE]

So this is the path to my html with the form on it?

Where do I put the URL that the form is being sent to?[/QUOTE]


No, that would be the URL to the external script you want to send the form data (or some subset of it) to for further processing. Your form's action would be the script where we're doing this cURL stuff, the cURL stuff would send the data via HTTP request (doing the same thing your form did to send its data to your script, but now sending it to the external site) and wait for the response from that external script, which will be returned by curl_exec().

PS: Just to clarify, the attached image is my understanding of what you want to do.

[upl-file uuid=48871f7c-3916-42dc-876e-fc64653ad40c size=17kB]flow.png[/upl-file]
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 17.2010 — So this is all that I would need?

[CODE]
<?php

echo "
<html>
<body onload='document.formAuto.submit();'>
<form method='post' action='www.thankyou.php' name='formAuto' >
<input type='hidden' name='firstname' value='$i1' />
<input type='hidden' name='lastname' value='$i2' />
<input type='hidden' name='email' value='$i3' />
<input type='hidden' name='address1' value='$i4' />
<input type='hidden' name='city' value='$i5' />
<input type='hidden' name='state' value='$i6' />
<input type='hidden' name='country' value='$i7' />
<input type='hidden' name='zip' value='$iz' />
<input type='hidden' name='birthyear' value='$i8' />
<input type='hidden' name='ph1' value='$i9$i10$i11' />

<input type='hidden' name='laprotocolkey' value='332' />
<input type='hidden' name='LAPKEY' value='332' />
<input type='hidden' name='listcode' value='http://www.mylocaljoblisting.com' />
<input type='hidden' name='listcodedate' value='$today' />
<input type='hidden' name='source' value='CoReg' />
<input type='hidden' name='channel' value='Web1' />
<input type='hidden' name='lpartner' value='DMI' />
<input type='hidden' name='debug' value='No' />

</form>
</body>
</html>
";

$data = array();
foreach($data as $key => $value) {
$data[] = urlencode($key)."=".urlencode($value);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://laprotocol.com/xml/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
$output = curl_exec($ch);

?>[/CODE]


This will send all my forms to http://laprotocol.com/xml/ and then goto my thankyou.php page?
Copy linkTweet thisAlerts:
@bradleybebadauthorAug 17.2010 — What am I doing with $output??? Or does this already send the form to the url given?
Copy linkTweet thisAlerts:
@NogDogAug 18.2010 — Okay, let's see if I [i]really[/i] understand what you want. My understanding is:

  • 1. User fills in form, and on submission it sends its data to your script at "http://your-site.com/some_file.php".

  • 2. In some_file.php, you want to take that form data and submit it to another URL somewhere else, e.g. "http://remote-site.com/api.php".

  • 3. If all that goes OK and you get a successful reponse from remote-site.com, then do a redirect to a "success" page back on your-site.com.


  • Does that jibe with what you are thinking, or have I gotten it confused?
    Copy linkTweet thisAlerts:
    @bradleybebadauthorAug 18.2010 — Correct! Sorry I have made things so hard. CURL confuses me a little.

    Here's what I have so far:

    my-site.com/form.html posts data to my-site.com/capture.php

    In capture.php, I have:

    [CODE]$data = array();
    foreach($data as $key => $value) {
    $data[] = urlencode($key)."=".urlencode($value);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.external-site.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
    $output = curl_exec($ch);
    curl_close($ch);


    echo "
    <html>
    <body onload='document.formAuto.submit();'>
    <form method='post' action='http://www.my-site.com/thankyou.php' name='formAuto'>
    <input type='hidden' name='firstname' value='$i1' />
    <input type='hidden' name='lastname' value='$i2' />
    <input type='hidden' name='email' value='$i3' />
    <input type='hidden' name='address1' value='$i4' />
    <input type='hidden' name='city' value='$i5' />
    <input type='hidden' name='state' value='$i6' />
    <input type='hidden' name='country' value='$i7' />
    <input type='hidden' name='zip' value='$iz' />
    <input type='hidden' name='birthyear' value='$i8' />
    <input type='hidden' name='ph1' value='$i9$i10$i11' />
    <input type='hidden' name='d_url' value='$d_url' />




    </form>
    </body>
    </html>
    ";

    [/CODE]


    Does this code curl data to www.external-site.com and then automatically send data to thankyou.php? I need to use the same data in thankyou.php as well.

    PS - when I echo $output it gives me a number that looks like an ip address
    Copy linkTweet thisAlerts:
    @NogDogAug 18.2010 — After doing the curl_exec() (and doing some error-checking to see if it worked ? ), I would do a header() redirect to the success page. If you want to carry over some or all of the form data to that page for displaying feedback to the user or whatever, I would make use of PHP sessions.
    [code=php]
    <?php
    session_start();
    /*
    Do the cURL processing
    */
    if($result_from_curl_is_ok) {
    $_SESSION = array_merge($_SESSION, $data); // replace $data with applicable array
    session_write_close(); // makes sure session data saved before redirect
    header('Location: http://your-site.com/success.php');
    exit; // our work is done here
    }
    /*
    Do whatever is needed if the cURL stuff failed, or the remote site complained
    about the data, or whatever....
    */
    [/code]
    Copy linkTweet thisAlerts:
    @bradleybebadauthorAug 18.2010 — Is this pseudo code or is it an actual curl expression?

    if($result_from_curl_is_ok)
    Copy linkTweet thisAlerts:
    @bradleybebadauthorAug 18.2010 — Why when i test $results by echoing the variable do I get a decimal number?
    Copy linkTweet thisAlerts:
    @NogDogAug 19.2010 — Is this pseudo code or is it an actual curl expression?

    if($result_from_curl_is_ok)[/QUOTE]


    It's a sort of place-holder variable I used there in place of whatever you want to really use to determine that the transaction with the other host was successful.
    Copy linkTweet thisAlerts:
    @bradleybebadauthorAug 19.2010 — I am a little confused about how you would go about checking if the $results went through to the other server successfully. However, everything you have given me has really helped. I really appreciate your time!

    Below is what I have created so far but I am not sure if it is put together correctly. Would you mind taking a look?

    Here is what I have:

    [CODE]
    $data = array();
    $data[] = 'firstname='.urlencode($i1);
    $data[] = 'lastname='.urlencode($i2);
    $data[] = 'keyCode='.urlencode('12343233');



    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "www.external-url.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
    $output = curl_exec($ch);
    curl_close($ch);



    echo "
    <html>
    <body onload='document.formAuto.submit();'>
    <form method='post' action='www.my-site.com/thankyou.php' name='formAuto'>
    <input type='hidden' name='firstname' value='$i1' />
    <input type='hidden' name='lastname' value='$i2' />
    </form>
    </body>
    </html>
    ";[/CODE]
    ×

    Success!

    Help @bradleybebad spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.15,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...