Guest Posted February 5, 2011 Share Posted February 5, 2011 I have a blog, and under each comment there is this link: <a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="quote(3, '<?php echo strip_tags($content); ?>');">Quote</a> This calls the following JavaScript function: function quote(thisField, text) { if (text == null) { var text = prompt('Enter quote:', ''); } if (text != null) { document.thisForm.elements[thisField].value += "<quote>" + text + "</quote>"; } } It works beautifully, as long as the PHP variable $content doesn't contain any quotes in it. I tried using htmlentities() and addslashes() on it with no luck. It doesn't insert anything at all into the form. Quote Link to comment https://forums.phpfreaks.com/topic/226766-inserting-php-variable-into-javascript-function/ Share on other sites More sharing options...
Guest Posted February 5, 2011 Share Posted February 5, 2011 Really close to getting this to work: <script type="text/javascript"> var content = <?php echo json_encode($content); ?>; </script> <a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="quote(3, content);">Quote</a> However, now the last comment on the page is the one that always gets inserted. Probably because the js var content is getting set each time a comment is displayed. How do I work around this? Quote Link to comment https://forums.phpfreaks.com/topic/226766-inserting-php-variable-into-javascript-function/#findComment-1170322 Share on other sites More sharing options...
Guest Posted February 5, 2011 Share Posted February 5, 2011 I thought this would work, but it doesn't: <script type="text/javascript"> content[i] = <?php echo json_encode($content); ?>; </script> <a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="quote(3, content[i]);">Quote</a> <script type="text/javascript"> i ++; </script> Before the PHP while loop, I initialize the js array content and the js var i: <script type="text/javascript"> var i = 0; var content = new Array(); </script> But, this results in the text not being passed at all. So now I'm being prompted for the text. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/226766-inserting-php-variable-into-javascript-function/#findComment-1170329 Share on other sites More sharing options...
Guest Posted February 5, 2011 Share Posted February 5, 2011 <script type="language/javascript"> function copyContent() { document.thisForm.content.value += document.thatForm.content.value; } </script> <form name="thatForm"> <textarea name="content"><?php echo stripSpaces($content); ?></textarea> </form> <a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="copyContent()">Quote</a> Doesn't work. Seriously, doesn't anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/226766-inserting-php-variable-into-javascript-function/#findComment-1170361 Share on other sites More sharing options...
Guest Posted February 5, 2011 Share Posted February 5, 2011 Well, hopefully everyone will be able to benefit from my labors. I finally got it to work. Here is the js function: function copyContent(field1, field2) { document.getElementById(field1).value = document.getElementById(field2).value; } Here is the html form: <form action="article/<?php echo $id; ?>#comments" method="post"> New comment:<br /> <textarea id="content0" name="content" rows="5" cols="69"></textarea> <p> <input type="submit" name="insert" value="Save" /> <input type="submit" name="cancel" value="Cancel" /> </p> </form> Then, I get all the comments from a mysql database. Before the while loop, I set the php var $i = 1. Then, this html form is inside the loop, followed by a link that executes the function: <form> <input id="content<?php echo $i; ?>" type="hidden" value="<quote><?php echo htmlentities(strip_tags($content)); ?></quote>" /> </form> <a href="javascript:void()" onClick="copyContent('content0', 'content<?php echo $i; ?>');">Quote</a> Finally, I increment the php var $i ++. Quote Link to comment https://forums.phpfreaks.com/topic/226766-inserting-php-variable-into-javascript-function/#findComment-1170404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.