【问题标题】:PHP form submit then check url to display corresponding contentPHP表单提交然后检查url显示相应内容
【发布时间】:2016-02-05 15:25:41
【问题描述】:

我正在尝试创建一个表单来注册发送到确切目标的电子邮件订阅(我们的邮件服务)。最初希望表单使用 ajax jquery 并在提交时将表单更改为成功消息或错误(取决于结果)。了解到无法在原始域SOURCE之外完成 ajax 表单提交

问题:

试图让这个表单提交数据并将数据附加到主页 url 以在加载时显示不同的内容。三种状态

  1. 如果 GET 为空则加载表单
  2. 如果 GET signup=error 则加载错误消息和表单
  3. 如果 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>'
;}?>

隐藏的输入被发送到我们的电子邮件主机,主机根据错误或成功选择正确的值发送回。

目前这不起作用。我从here得到了php代码

【问题讨论】:

  • 这可能不会按照您想要的方式工作。将表格提交到另一个网站后,您将无法看到或对其他网站所说/所做的任何事情。唯一可行的方法是,如果其他网站在成功/失败时使用某种状态代码重定向回您的网站。
  • 为此,您可能必须将他们填写到表单中的任何内容都拿走,然后使用该数据向其他网站提交 curl 请求。访问者实际上从未访问过其他网站,您的服务器会代表他们访问(通常使用 API)。您会收到 curl 响应,并根据该响应确定成功/失败并显示您的消息。
  • 我删除了我之前的评论 - @JonathanKuhn 的回复更清楚了。是的,除非电子邮件提供商/服务可以发回状态,否则您将无法执行任何类型的状态检查。
  • 电子邮件提供商根据成功与否将隐藏名称“thx”和“err”输入值中的链接发送回链接。如果它有效,他们会重定向到我在“thx”中指定的链接,如果失败,则会出现“err”,因此分别为 example.com/?signup=success 和 example.com/?signup=error。我不能使用 php if 语句并阅读 ?signup= 来确定内容是否显示?

标签: javascript php jquery html ajax


【解决方案1】:

这样做的正确方法是使用 PHP 作为您和客户端之间的一种代理(您从客户端收集表单数据,代表他们将其提交给第 3 方服务器,然后检索结果)。

<?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
}

【讨论】:

  • 谢谢!这种方法确实有效,但由于某种原因显然不适用于我们的电子邮件主机。我们最终使用 javascript 来检测 url 中返回的代码并相应地显示内容。
猜你喜欢
  • 2013-05-24
  • 1970-01-01
  • 2012-03-20
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多