PHP / HTML反馈表:如何获取当前页面的URL?

时间:2017-11-20 17:42:55

标签: php html forms webforms

我正在使用HTML和PHP创建一个简单的反馈表单。表单和脚本正在运行,我收到提交的电子邮件,但有一个问题: =>我希望反馈表单发送使用反馈表单的页面的URL,并在我的电子邮件中接收。我想知道用户正在谈论哪个页面。例如,如果用户点击了反馈按钮并在http://example.com/features页面上打开了反馈表单(模态),我希望在我的电子邮件中显示该页面的网址(http://example.com/features)以及反馈消息。

以下是我的HTML和PHP:

HTML表单:

<div id="myModal" class="modal">
    <div id="modal-box" class="modal-content">
         <div class="modal-header">
         	<span class="close">×</span>
            <h2>Some Heading?</h2>
        </div>
        <div class="container">
         <!--The submitt button in the form redirects action to the PHP script for message submission -->
        <form action="http://example.com/mailer.php" role="form" name="feedbackForm" id="feedback_docs"  method="post" >
           <textarea id="comments" type="text" name="comments" placeholder="Please write your comments about this topic here." style="height:200px"></textarea>
            <input id="submit" name="submit" type="submit" value="Send" />
        </form>
     </div>
</div>
</div>      
<div id="myBtn">
    <button id="popup" class="feedback-button">FEEDBACK</button>
    <p id="demo"></p>
</div>

PHP:

<?php
 $comments = $_POST['comments'];
 $to = 'feedback@example.com';
 $subject = 'Feedback received from website';
 $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PATH_INFO'];
 $headers[] = 'MIME-Version: 1.0';
 $headers[] = 'Content-type: text/html; charset=iso-8859-1';
 $body = "Message: \r\r $comments";
if ($_POST['submit']) {
    if (mail ($to, $subject, $url, $body)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

如果您查看PHP脚本,我已尝试添加以下内容:

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PATH_INFO'];

我也试过了:

$url = "http://".$_SERVER['HTTP_REFERER'].$_SERVER['REQUEST_URI'];

但都没有奏效。

我只是学习PHP和脚本,所以我可能犯了一些错误。谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

无论用户在哪个文件中,请求最终都会转到同一个PHP文件。所以,你需要找到推荐人。但它在PHP方面不可靠。相反,你要做的是使用隐藏的输入并传递它:

<input type="hidden" name="fromPage" value="feedbackForm" />
<input type="hidden" name="url" value="https://www.example.com/feedback.html" />

然后在POST数据中捕获它们并将其发送到您的电子邮箱。

答案 1 :(得分:0)

原因是您错误地使用了某些部件。 正确的两个是

 $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

答案 2 :(得分:0)

这是我的问题的解决方案。 Forbs提供的答案非常有用。

我将$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];更改为$url = "http://".$_SERVER['HTTP_REFERER'];

解决方案非常简单,我缺乏PHP经验,这对我来说很有挑战性!