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

时间:2022-11-24 14:32:00

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

我正在尝试创建一个表单来注册电子邮件订阅,这些订阅会发送到确切的目标(我们的邮件服务)。最初希望表单使用ajax jquery并在提交时将表单更改为成功消息或错误(取决于结果)。据悉,ajax表单提交不能在原始域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

尝试使用此表单提交并将数据附加到主页URL以在加载时显示不同的内容。有三个州

  1. Load form if GET is empty
  2. 如果GET为空,则加载表单

  3. Load error message and form if GET signup=error
  4. 如果GET注册=错误,则加载错误消息和表单

  5. Load success message if GET signup=success
  6. 如果GET注册=成功,则加载成功消息

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

目前这不起作用。我从这里得到了PHP代码

1 个解决方案

#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作为您和客户端之间的一种代理(您从客户端收集表单数据,代表他们将其提交给第三方服务器,并检索结果)。

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


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作为您和客户端之间的一种代理(您从客户端收集表单数据,代表他们将其提交给第三方服务器,并检索结果)。

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