Jump to content

How to pass php variable containing quote to JS function


blogfisher

Recommended Posts

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!!

Link to comment
Share on other sites

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 ????

Link to comment
Share on other sites

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 ???

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.