【问题标题】:How to Initialize php variable with javascript?如何用 javascript 初始化 php 变量?
【发布时间】:2015-05-14 13:18:41
【问题描述】:

您好,我向客户发送的电子邮件如下所示: $body = '<div>my email content</div>'; 我需要从 google api 将此脚本添加到 $body 变量:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "LodgingReservation",
  "reservationNumber": "abc456",
  "reservationStatus": "http://schema.org/Confirmed",
  "underName": {
    "@type": "Person",
    "name": "John Smith"
  },
  "reservationFor": {
    "@type": "LodgingBusiness",
    "name": "Hilton San Francisco Union Square",
    "address": {
      "@type": "PostalAddress",
      "streetAddress": "333 O'Farrell St",
      "addressLocality": "San Francisco",
      "addressRegion": "CA",
      "postalCode": "94102",
      "addressCountry": "US"
    },
    "telephone": "415-771-1400"
  },
  "checkinDate": "2017-04-11T16:00:00-08:00",
  "checkoutDate": "2017-04-13T11:00:00-08:00"
}
</script>

我该怎么做?有任何想法吗? 我无法将 " 更改为 '。

【问题讨论】:

    标签: javascript php variables google-schemas


    【解决方案1】:

    如果我理解了这个问题,这应该是你要找的:

    $script = <<<EOF
    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "LodgingReservation",
      "reservationNumber": "abc456",
      "reservationStatus": "http://schema.org/Confirmed",
      "underName": {
        "@type": "Person",
        "name": "John Smith"
      },
      "reservationFor": {
        "@type": "LodgingBusiness",
        "name": "Hilton San Francisco Union Square",
        "address": {
          "@type": "PostalAddress",
          "streetAddress": "333 O'Farrell St",
          "addressLocality": "San Francisco",
          "addressRegion": "CA",
          "postalCode": "94102",
          "addressCountry": "US"
        },
        "telephone": "415-771-1400"
      },
      "checkinDate": "2017-04-11T16:00:00-08:00",
      "checkoutDate": "2017-04-13T11:00:00-08:00"
    }
    </script>
    EOF;
    
    $body = '<div>my email content</div>' . $script;
    

    更多关于heredoc

    可能你需要使用 htmlentities() 方法来编码特殊字符

    【讨论】:

      【解决方案2】:

      您的问题不清楚,我认为这就是您要寻找的任何方式。 你可以使用 Ajax,使用 Jquery ajax $.ajax({}); 用于在您的网站中实现跨浏览器 ajax 支持。使用 ajax 将该数组发送到您实现邮件功能的 php 文件。只需使用$_GET$_POST 来获取数据。然后将数据附加到您的邮件正文中。

      请参阅底层链接以阅读 jquery ajax 文档。 http://api.jquery.com/jquery.ajax/

      【讨论】:

        猜你喜欢
        • 2019-05-17
        • 1970-01-01
        • 2012-01-16
        • 2013-04-05
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        • 2019-12-07
        • 2012-10-11
        相关资源
        最近更新 更多