0

I'm trying to create a form to signup for email subscription that goes out to exact target (our mailing service). Originally wanted the form to use ajax jquery and upon submit change the form to success message or error (depending on results). Learned that ajax form submits cannot be done outside of originating domain SOURCE

Question:

Trying to get this form to submit and append data to homepage url to display different content on load. There are three states

  1. Load form if GET is empty
  2. Load error message and form if GET signup=error
  3. Load success message if GET signup=success

<?php
if (empty($_GET)) {
echo'
<form action="emailhost.net/subscribe.aspx" name="subscribeForm" method="post" class="forms" id="form">
<input type="hidden" name="thx" value="example.com/?signup=success" />
<input type="hidden" name="err" value="example.com/?signup=error" />
<input type="text" name="Full Name" placeholder="Name" />
<input type="text" name="Email Address" placeholder="Email Address" />
<input type="submit" />
</form>'
;}

if(isset($_GET["signup"]) && trim($_GET["signup"]) == "error"){
echo '
<p>There was an error. Please try again</p>
<form action="emailhost.net/subscribe.aspx" name="subscribeForm" method="post" class="forms" id="form">
<input type="hidden" name="thx" value="example.com/?signup=success" />
<input type="hidden" name="err" value="example.com/?signup=error" />
<input type="text" name="Full Name" placeholder="Name" />
<input type="text" name="Email Address" placeholder="Email Address" />
<input type="submit" />
</form>'
;}

if(isset($_GET["signup"]) && trim($_GET["signup"]) == 'success'){
echo'
<p>Thank you for signing up!</p>'
;}?>

The hidden inputs are sent to our email host and based on error or success the host chooses the correct value to send back to.

Currently this doesn't work. I got the php code from here

4
  • This likely isn't going to work the way you want it. Once the form is submitted to another website, you will not be able to see or do anything with what the other website says/does. The only way this would work is if the other website, on success/fail, would redirect back to your site with some sort of status code. Commented Nov 4, 2015 at 23:01
  • For this to work, you would likely have to take whatever they filled into the form and submit a curl request to the other website with that data. The visitor never actually goes to the other website, your server does on their behalf (usually with an API). You get the curl response and based on that determine success/fail and display your message. Commented Nov 4, 2015 at 23:03
  • I deleted my previous comment - @JonathanKuhn's response made it more clear. Yes, you will not be able to perform any sort of status check unless the email provider/service could send back a status.
    – Super Cat
    Commented Nov 4, 2015 at 23:04
  • the email provider sends back to the links in the values of the hidden names "thx" and "err" inputs based on whether it succeeded or failed. If it works, they redirect to the link I specify in "thx" and on failure to "err" so example.com/?signup=success and example.com/?signup=error respectively. Can't I use a php if statement and read the ?signup= to determine if the content displays? Commented Nov 4, 2015 at 23:16

1 Answer 1

0

The correct way to do this would be to use PHP as a sort of proxy between you and the client (you collect the form data from the client, submit it to the 3rd party server on their behalf, and retrieve the result).

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $postdata = http_build_query(
                    $_POST, /* or wherever else you collected the data from */
                );
    $opts = [
      'http'=> [
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata,
      ]
    ];

    $context = stream_context_create($opts);

    $result = file_get_contents('http://emailhost.net/subscribe.aspx', false, $context);
    if ($result) {
        echo "<h1>Success!</h1>";
    } else {
        echo "<h1>ohnoes...</h1>";
    }
} else {
?>
<form action="myscript.php" name="subscribeForm" method="post" class="forms" id="form">
<input type="hidden" name="thx" value="example.com/?signup=success" />
<input type="hidden" name="err" value="example.com/?signup=error" />
<input type="text" name="Full Name" placeholder="Name" />
<input type="text" name="Email Address" placeholder="Email Address" />
<input type="submit" />
</form>
<?php
}
1
  • Thanks! This method does work but apparently not with our email host for some reason. We ended up using javascript to detect what code was returned in the url and display content accordingly. Commented Nov 6, 2015 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.