dsnw2651 2015-11-04 22:54
浏览 40
已采纳

PHP表单提交然后检查url以显示相应的内容

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

</div>
  • 写回答

1条回答 默认 最新

  • drllqg2903 2015-11-04 23:13
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?