Search code examples
phpjavascriptvariablesquotestransfer

Variable transfer from php to javascript trouble


I want to transfer a variable from php to javascript but keep getting extra quatation marks in the beginning and end echoing it. What have i done wrong and is there a better way to do this?

$textForm = '<form method="post" action="upload.php" name="truth">
<br />
Tittle:<br /><input type="text" name="title" size="40" /> <br /><br />
Author:<br /><input type="text" name="author" size="40" /> <br /><br /><br />
Text:<br /><textarea name="story" cols="90" rows="24" wrap="physical">Input text here.</textarea>


<br /><input type="submit" value="upload" />
<form>';

<script type = "text/javascript">
function showTextForm() {

var form1 = '<?php print json_encode($textForm1); ?>'
</script>

Solution

  • Think you can simply do:

    <script type = "text/javascript">
    function showTextForm() {
    
    var form1 = <?=json_encode($textForm1); ?>;
    </script>