0

I'm sending a html email from php. The body is wrapped in double quotes ("). How do I add a button onclick event and a link within the double quotes?

$body = "<BODY>
    <button type='button' onclick='window.location.href='www.example.com/page.html?id=".$id."''>
</BODY>";

How do I do onclick='window.location.href='''

Becky
  • 5,467
  • 9
  • 40
  • 73
  • 2
    It sounds like you're asking how to [escape quotes in PHP](http://stackoverflow.com/a/7999163/622391). – Simon MᶜKenzie Dec 07 '15 at 04:15
  • 3
    Many email service providers remove javascript code from the email source. I would recommend you to use links instead of button to archive this. To give a look of button, create an image and link you page on that image. Like – feco Dec 07 '15 at 04:16
  • 2
    Possible duplicate of [Escaping quotation marks in PHP](http://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php) – chris85 Dec 07 '15 at 04:19

5 Answers5

5

Use slash

$body = "<BODY>
     <button type='button' onclick=\"window.location.href='www.example.com/page.html?id=".$id."'\">
</BODY>";
Alexey Kurilov
  • 438
  • 4
  • 10
2

You can use heredoc syntax which means you don't have to worry about escaping double quotes or single quotes.

$body = <<<EOT
<body>
  <button type="button"
          onclick="window.location.href='www.example.com/page.html?id={$id}'">
    click me
  </button>
</body>";
EOT;

Or you can skip writing obtrusive javascript altogether and your issue goes away.

Mulan
  • 129,518
  • 31
  • 228
  • 259
1

As an alternative to escaping double quotes with \ in the string you can just use a separate string to store your substring. ie.

$href = '"www.example.com/page.html?id='.$id.'"';
$body = "<BODY>
    <button type='button' onclick='window.location.href=".$href."'>
</BODY>";
Memor-X
  • 2,870
  • 6
  • 33
  • 57
0

write a onclick function an move your location using that function.

<html>
<script>
function locations(){
    //var id=20; get the value of id and save in id(getElementById or jquery) 
window.location.href='www.example.com/page.html?id='+id;
}
</script>
<body>
<input type="button" onclick="locations()">
</body>
</html>
Vigneswaran S
  • 2,039
  • 1
  • 20
  • 32
0

another way to do this is storing quotes in variables

<?php
$quotes-dbl = '"';$quotes-sgl "'";
echo $quotes-dbl."text".$quotes-dbl;
Azure
  • 127
  • 11