Haniver
Haniver

Reputation: 352

Sending quoted string to javascript function, then to php

I want to send a string containing single and double quotes to a javascript function which then sends it to a php page as a get variable. The code is this (I've tried escaping the quotes in two ways, but none of them work):

<html>
<head>
  <script type="text/javascript">
    function myFunction (foo) {
      window.location = "bar.php?var=" + foo;
    }
  </script>
</head>
<body>
  <a href='javascript:myFunction("text&#39;text&#34;text");'>text'text"text</a><br>
  <a href='javascript:myFunction("text&apos;text&quot;text");'>text'text"text</a>
</body>
</html>

The links just don't have any effect. Thanks!

Upvotes: 1

Views: 108

Answers (3)

ops
ops

Reputation: 2049

Try this code:

echo '<a href="bar.php?var='.urlencode($value).'">text\'text"text</a><br>';

Upvotes: 1

castis
castis

Reputation: 8223

try urlencode() the value before echoing it onto the page

<a href='javascript:myFunction(<?php echo urlencode($value); ?>);'><?php echo $value; ?></a>

Upvotes: 1

VortexMath396
VortexMath396

Reputation: 66

urlencode * is the correct syntax

Upvotes: 1

Related Questions