1

Im trying to do something really simple I know I should probably be using ajax but I'm just doing some quick tests.

so I have a display.php file with some variable and I want to display the PHP variable in the input text by using document.getElementbyID.value = myVariable

//PHP
<?php
$name = 'Patrick';
?>

//HTML
First Name: <input type="text" id = "fname" name="fname" value=""   >

//JS
<script type="text/javascript">
     var f = <?php echo($name); ?>;
    document.getElementById("fname").value = f;`
</script>

I keep getting the error Uncaught ReferenceError: Patrick is not defined Not really sure whats wrong with my code it looks pretty simple but it don't want to put the value "Patrick" in the input box.

Tried different ways of writing it with '' or using json_encode but didnt change anything still getting same error.

3
  • You just need to put it in quotes. var f= patrick; won't work, var f = 'patrick'; will work. Commented Jan 16, 2023 at 3:31
  • well I made it simple in the question but actually im getting my $name by doing $name=$person->name so im getting the variable from an array of person Commented Jan 16, 2023 at 3:33
  • it still needs to var f = '<?echo ... (either ' or " before the <echo). try replacing your echo code with just the word patrick to see what we mean, echo() does not add quotes for you. PHP runs on the server, and outputs text BEFORE the page runs in the browser. You can view the page source in the browser to see this. Commented Jan 16, 2023 at 3:34

1 Answer 1

1

Uncaught ReferenceError: Patrick is not defined

That's because the resulting javascript is:

var f = Patrick;

Which means set f to the contents of the variable Patrick.

Since there is no variable defined that is named Patrick you'll get the uncaught reference error.

You need to put Patrick in quotes like so:

var f = "Patrick"; // <-- Note the "

Note, since you want to pass data from PHP to JavaScript, a better way would be to use JSON like this:

var f = <?php echo json_encode((string)$data); ?>;

This allows you to pass more complex types of data¹ AND you'll get proper escaped strings.

Proper escaped strings? If the user input is Test " (note the double quote) a primitive approach will break because the resulting javascript will be:

var f = "Test "";

This is not only a bug, but a security issue since user input could contain arbitrary javascript that would get executed.

¹ just remove the (string) cast.

Sign up to request clarification or add additional context in comments.

6 Comments

this seems to work but im getting SyntaxError: missing ) after argument list var f = JSON.parse("<?php echo json_encode($fname); ?>"); I simply copied your thing so not sure why where its missing
a few corrections, first off, don't add the quotes yourself, let json_encode do it, and second, if you want to use JSON.parse() you actually need to double-encode it, you can do var f = JSON.parse(<?php echo json_encode(json_encode($data)); ?>); - the first json_encode makes valid json, but not a valid json string, and the 2nd encode() converts it from a json to a json-encoded-string - but given that json is valid javascript anyway, there's no need to use JSON.parse here at all! you can also just do var f = <?php echo json_encode(...);?>;
@hanshenrik Oh my god, you're right 😂 I wasn't thinking it through...
@Anasde Unfortunately my first version of my answer contained an error which hanshenrik correctly pointed out. It should work now.
that's better! i don't think that cast-to-string is a good idea tho, will break if/when he tries anything other than a string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.