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
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.
Uncaught ReferenceError: Patrick is not defined
var f = Patrick;
Which means set
f to the contents of the variable
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 "
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
var f = "Test "";
¹ just remove the