使用帖子

时间:2019-03-10 21:11:13

标签: php forms

我有带有html编码的php文件。我正在使用include语句将同一表单导入许多不同的页面,但是我需要知道表单是从哪个页面提交的。文件本身是.php,但是大多数编码都使用html。如何添加提交表单的网站的当前URL?我使用发布方法。

<form action="post.php" method="post">
   <input type="hidden" name="url" value="(Current URL here)" />
   <input type="text" id="email" name="email">
</form>

和php部分:

<?php
  $addressto = "mail@mail.com";
  $subject = "Message";
  $content = "Email: ".$_POST['email']."\n"
            ."URL: ".$_POST['url']."\n";

    $email = $_POST['email'];
    if (mail($addressto, $subject, $content, 'From: <' . $email . '>')) {
        header("Location: message-sent.html");
    }
?>

我相信我需要某种获取URL的代码。我在这里没有找到类似的问题,但是没有一个问题清楚地说明了如何做。谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

看看下面的答案和代码

Get the full URL in PHP

<form action="post.php" method="post">
  <input type="hidden" name="url" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
  <input type="text" id="email" name="email">
</form>

答案 1 :(得分:1)

<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

?>

<form action="post.php" method="post">
   <input type="hidden" name="url" value="<?=$actual_link?>" />
   <input type="text" id="email" name="email">
</form>

相关问题