【问题标题】:Passing a URL to PHP contact form将 URL 传递给 PHP 联系表单
【发布时间】:2014-12-18 18:01:01
【问题描述】:

我有点 PHP 菜鸟,所以这可能是基本的。我在 PHP/Bootstrap 中有一个使用此示例博客的联系表
https://jonbake.com/blog/bootstrap-3-contact-form-with-captcha/

我正在尝试修改 sendmail.php 文件以包含联系表单所在的 URL。

即我们有 3 种左右不同的联系表格,并希望确定用户从何处发送联系电子邮件。

我可以将文件复制三遍并更改主题以作为示例,这很好。但理想情况下,我只想要一个 sendmail.php 文件并传入发送表单/电子邮件的 URL。

问题
如何获取 URL 并将其传递到此 PHP 文件并修改到电子邮件?

编辑
AJAX 用于将 POST 发送到 sendmail.php,如果这会影响链接的传递方式。

   //send the feedback e-mail
    $.ajax({
      type: "POST",
      url: "../assets/library/sendmail.php",
      data: $("#feedbackForm").serialize(),
      success: function(data)
      {
        contactForm.addAjaxMessage(data.message, false);
        //get new Captcha on success
        $('#captcha').attr('src', '../assets/library/vender/securimage/securimage_show.php?' + Math.random());
      },
      error: function(response)
      {
        contactForm.addAjaxMessage(response.responseJSON.message, true);
      }
   });
    return false;
  }); 

谢谢

【问题讨论】:

  • $link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";借自本问答stackoverflow.com/q/6768793
  • @Fred-ii- 这不会将完整的 URL 返回到实际的 sendmail.php 文件,而不是我正在寻找的联系表单 URL?如果我将此添加到联系表单页面,如何将其传递给 sendmail.php ?
  • @Fred-ii- 只是为了确认,是的,这将返回 sendmail php 文件的链接。那么如何将其包含在联系表单中并将其传递给 sendmail 以在电子邮件中使用?
  • 如果传递给超过 2 个后调用,您可以使用会话。只需为其分配一个会话变量。
  • 您也可以在表单本身中使用隐藏属性。

标签: php sendmail forms


【解决方案1】:

首先确保您的表单包含 .php 扩展名,以便以下内容起作用:(请参阅 Nota)。

<input type="hidden" name="the_link" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>">

在表单的隐藏属性中传递当前 URL(应该是 POST 方法)。

然后使用类似的东西:

$link = $_POST['the_link'];

在您的 sendmail.php 文件中。

如果您使用的是 GET 方法,只需在我的回答中将 POST 更改为 GET。


注意:如果您当前使用的是.html 文件,您可以指示Apache 将.html 文件视为PHP。

示例:(从http://www.besthostratings.com/articles/php-in-html-files.html 提取/借用)。

对于使用 PHP 作为 apache 模块的 Web 服务器:

AddType application/x-httpd-php .html .htm

对于将 PHP 作为 CGI 运行的 Web 服务器:

AddHandler application/x-httpd-php .html .htm 

如果你想模仿 ASP:

对于 PHP 作为模块:

AddType application/x-httpd-php .asp

对于 PHP 作为 CGI:

AddHandler application/x-httpd-php .asp

另一个关于 Stack 的问答:

【讨论】:

    【解决方案2】:

    您可以在每个联系表单中设置一个标志并相应地处理电子邮件的正文:

    contact_form_1.html

    <form>
        <!-- code -->
        <input name="referer" type="hidden" value="contact_form_1">
        <!-- code -->
    </form>
    

    sendmail.php

    <?php
        // code
        $mailbody.="Referer: ".$_POST["referer"];
        // code
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多