-2

In test.php:

<?php
$result = "test";
echo '<script type="text/javascript">parent.showThanksDiv(\"<?php echo $result;?>\");</script>';
?>

and in test.html

  <script type="text/javascript">
 function showThanksDiv(text){
      document.getElementById("myThanksDivtext").value = text;
    }
</script>

That's not working. It seams Im not passing php variables correctly. Any ideas?

2
  • why did I get downvoted?! My question is legit, clear and helpful for newbie to php. Care to explain? Commented Feb 9, 2013 at 22:26
  • Well, for starters, neither the failure(s) nor the generated HTML were provide .. also, there are many similar duplicates. Also, take time to ensure there are no trivial spelling mistakes, especially in the title. Commented Feb 9, 2013 at 22:48

2 Answers 2

1

Change the PHP to:

<?php
$result = "test";
echo '<script type="text/javascript">parent.showThanksDiv("' . $result . '");</script>';
?>

You don't need to wrap the PHP variable in <?php and ?> since you're still in PHP.

3
  • mm That didn't work. When I do parent.showThanksDiv("test") that work Commented Feb 9, 2013 at 22:21
  • @Kam - I removed a pair of `\` from my original post. Did you try the revised version? Commented Feb 9, 2013 at 22:22
  • Do spaces make a difference!? because I remembered trying "'.$result.'" before and it didn't work Commented Feb 9, 2013 at 22:23
1

I guess you wanted to do something like that:

<?php
$result = "test";
echo '<script>parent.showThanksDiv("' . $result . '");</script>';
?>

Or to be more secure, I'd suggest to use json_encode():

echo '<script>parent.showThanksDiv(' . json_encode($result) . ');</script>';

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.