1

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!

3 Answers 3

1

urlencode * is the correct syntax

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

I tried <?php echo "<a href='javascript:myFunction(" . urlencode("text'text\"text") . ");'>test1</a><br> <a href='javascript:myFunction(" . urlencode('text\'text"text') . ");'>test2</a><br>"; ?> That doesn't work either.
1

Try this code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.