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.
var f= patrick;won't work,var f = 'patrick';will work.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.