将URL传递给联系表单

时间:2011-06-23 19:49:31

标签: php javascript html url

情况: 用户正在使用提供的联系选项查看网页。点击“联系”后,我想将当前查看过的网页的网址作为文字传递到该联系表单的字段中。

起点是任何HTML页面(page-xy.html)。 现有的联系表单用PHP,HTML和CSS(contact.php)编写,运行正常。 我假设Javascript可能有助于完成此过程。

单击“联系人”按钮后,此Javascript命令将读取当前URL: var strReferrer=window.location.href;

问题是如何将此变量'strReferrer'传递给正在打开的'contact.php'?
在下文中,应该将变量插入到消息文本字段中。 我宁愿不使用cookie。

希望有人有解决方案的建议,谢谢。

My goal is just about the reverse of the problem described in this thread

4 个答案:

答案 0 :(得分:2)

只需使用隐藏的输入字段。

<form ...>
    <!-- Your contact form goes here -->
    <input type="hidden" id="strReferrer" name="strReferrer" value="whatever" />
</form>

如果需要,您可以让javascript设置值。

答案 1 :(得分:1)

这是我实施的解决方案。

在任何带有联系人选项的HTML页面中,如下所述调用简单脚本。 如果JS不可用,联系表格将通过'href'打开,当然没有原始网址。

<a href="/contact/contact-form.php" title="send an eMail" onclick="return(contact())">Contact the author</a>

...将当前网址的字符串放入'strReferrer'并用contact-form.php打开一个新窗口

<script>
var strReferrer = location.href;
function contact() {
window.open("/contact/contact-form.php?sourceURL="+strReferrer);
return false;}
</script>

PHP使用'strReferrer'查询字符串并将其保存在'$ params'中。 在contact-form.php中:

//get the URL from the originating page (where this form has been called from)
$params  = $_SERVER['QUERY_STRING'];
...
...
// Insert '$params' into the text as part of the text to be eMailed.
// the eMail Content Header for the recipient
$_POST["text"] = $_POST["send"]["author"]." with e-mail address: ".$_POST["send"]["mail"]." sent the msg below: \n <$params> \n \n $texto";

现在您知道访问者已决定与您联系的网页网址。

答案 2 :(得分:0)

在表单中插入此行,并在表单数据预处理中为referer提供。

<input type="hidden" id="referer" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" /> 

答案 3 :(得分:0)

我在这里解决了类似的问题

我使用此代码捕获网页网址

<script type="text/javascript">
var segments = window.location.pathname.split('/');
var toDelete = [];
for (var i = 0; i < segments.length; i++) {
    if (segments[i].length < 1) {
        toDelete.push(i);
    }
}
for (var i = 0; i < toDelete.length; i++) {
    segments.splice(i, 1);
}
var filename = segments[segments.length - 1];
console.log(filename);
document.write(filename);
</script>

但是如何将它放在联系表单文本框中? (我使用wordpress联系表单插件)

见这里:https://nationalland.co.uk/tw31sp