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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看