blogfisher Posted January 20, 2009 Share Posted January 20, 2009 Hi, In my PHP program i am accepting input from users. I am passing it to javascript function for further process. My function didn work if input string contains quote character. For example sample Code where $myvar will actually coming from from user input which needs to pass to js function: <? php $myvar = "Hello's"; echo '<FORM NAME="myform" ACTION="" METHOD="GET"> Enter something in the box: <BR> <INPUT TYPE="text" NAME="inputbox" VALUE=""><P>'; echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick=\"javascript:readText(this.form, \"$myvar\")\">"; //<INPUT TYPE="button" NAME="button2" Value="Write" onClick="javascript:writeText(this.form)"> echo '</FORM>'; ?> and javascript is as below: function readText (form, data) { TestVar =form.inputbox.value; alert ("You typed: " + TestVar + "... and ..." + data); } However this is not working because of quote character in $myvar. Any idea to overcome this ??? Quick solution will be appreciated... Thx in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/ Share on other sites More sharing options...
RussellReal Posted January 20, 2009 Share Posted January 20, 2009 addslashes Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741429 Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 What ^ he ^ said echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick=\"javascript:readText(this.form, \"".addslashes($myvar)."\")\">"; Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741432 Share on other sites More sharing options...
blogfisher Posted January 20, 2009 Author Share Posted January 20, 2009 wow... thats working .. i was not aware of that php function... highly appreciate you quick responses !! Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741446 Share on other sites More sharing options...
blogfisher Posted January 20, 2009 Author Share Posted January 20, 2009 Well, thats work fine... however what to do in case input string has " as a character e.g. $myvar = abcd"ef ? addslashes() wont work ? Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741457 Share on other sites More sharing options...
DeanWhitehouse Posted January 20, 2009 Share Posted January 20, 2009 do you want to remove it? If so use str_replace, or use htmlentities on it to turn the quote into a html special char Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741461 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick='javascript:readText(this.form, \"".addslashes($myvar)."\")'>"; Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741464 Share on other sites More sharing options...
blogfisher Posted January 20, 2009 Author Share Posted January 20, 2009 i dont want to remove any characters from input string, just want to display as it is by passing through js function... Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741465 Share on other sites More sharing options...
blogfisher Posted January 20, 2009 Author Share Posted January 20, 2009 echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick='javascript:readText(this.form, \"".addslashes($myvar)."\")'>"; Thats not working.. consider the input string as $myvar = 'Hello"s'; Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741472 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 not sure about your readtext() function, but this worked for me: <?php $myvar = 'Hello"s'; echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick='javascript:alert(\"".addslashes($myvar)."\")'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741475 Share on other sites More sharing options...
DeanWhitehouse Posted January 20, 2009 Share Posted January 20, 2009 Works for me $str = 'tes"t'; echo addslashes($str);//prints tes\"t Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741476 Share on other sites More sharing options...
blogfisher Posted January 20, 2009 Author Share Posted January 20, 2009 Still having problem... consider code as below : $myvar = 'Hello"s'; echo "<FORM NAME=\"myform\" ACTION=\"\" METHOD=\"GET\"> Enter something in the box: <BR> <INPUT TYPE=\"text\" NAME=\"inputbox\" VALUE=\"\"><P>"; echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick='javascript:readText(this.form, \"".addslashes($myvar)."\")';>"; echo "</FORM>"; This works fine... however if we changed input as $myvar = "hello's" it wont work ???? Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741487 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 what "doesn't work" about it. the only error i get is that the function isn't defined. can you please provide the readText() function? Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741495 Share on other sites More sharing options...
blogfisher Posted January 20, 2009 Author Share Posted January 20, 2009 what "doesn't work" about it. the only error i get is that the function isn't defined. can you please provide the readText() function? Code is as below : $myvar = 'Hello"s'; echo "<FORM NAME=\"myform\" ACTION=\"\" METHOD=\"GET\"> Enter something in the box: <BR> <INPUT TYPE=\"text\" NAME=\"inputbox\" VALUE=\"\"><P>"; echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick='javascript:readText(this.form, \"".addslashes($myvar)."\")';>"; echo "</FORM>"; <SCRIPT LANGUAGE="JavaScript"> function readText (form, data) { TestVar =form.inputbox.value; alert ("You typed: " + TestVar + "... and ..." + data); } </SCRIPT> The above code works fine. However if i change my input to $myvar = "Hello's" .. code doesn't work ??? Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741503 Share on other sites More sharing options...
rhodesa Posted January 20, 2009 Share Posted January 20, 2009 yeah...tricky...can you do it this way instead? <?php $myvar = "Hello's"; echo "<FORM NAME=\"myform\" ACTION=\"\" METHOD=\"GET\"> Enter something in the box: <BR> <INPUT TYPE=\"text\" NAME=\"inputbox\" VALUE=\"\"><P>"; echo "<INPUT TYPE=\"button\" NAME=\"button1\" Value=\"Read\" onClick=\"javascript:readText(this.form)\";>"; echo "</FORM>"; ?> <SCRIPT LANGUAGE="JavaScript"> var data = <?php echo json_encode($myvar); ?>; function readText (form) { TestVar =form.inputbox.value; alert ("You typed: " + TestVar + "... and ..." + data); } </SCRIPT> Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741531 Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 Try this. I used addslashes() and then replaced \" with " <?php $single = "sing'le"; $double = 'dou"ble'; $slash = "sla\sh"; ?> <html> <head> <SCRIPT LANGUAGE="JavaScript"> function readText (form, data) { TestVar = form.inputbox.value; alert ("You typed: " + TestVar + "... and ..." + data); } </SCRIPT> </head> <body> <form name=""> Input Box:<br /> <input type="text" name="inputbox"> <br /> <button onclick="readText (this.form, '<?php echo str_replace ('\"', '"', addslashes($single)); ?>');">Single Quote test</button><br /> <button onclick="readText (this.form, '<?php echo str_replace ('\"', '"', addslashes($double)); ?>');">Double Quote test</button><br /> <button onclick="readText (this.form, '<?php echo str_replace ('\"', '"', addslashes($slash)); ?>');">Slash test</button><br /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-741592 Share on other sites More sharing options...
blogfisher Posted January 21, 2009 Author Share Posted January 21, 2009 @mjdamato Thx.. it works amazingly.... Quote Link to comment https://forums.phpfreaks.com/topic/141646-how-to-pass-php-variable-containing-quote-to-js-function/#findComment-742465 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.