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

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)."\")'>";
?>

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

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

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.