0

In a php file, there is a textbox with name "User".

I've used cookie to save the user.

However, how can I do something like this?

$user = $_COOKIE['user'];
document.getElementById('User').value = '$user';

Please help.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Jamie
  • 1,096
  • 2
  • 19
  • 38
  • Are you sure it is duplicate? It is __OUTSIDE__ the js code, you know? – Jamie Apr 11 '14 at 23:55
  • Hey, is it `exact duplicate`? – Jamie Apr 11 '14 at 23:58
  • Have you actually read the answers? If you did, did you fully understand the concept? – HamZa Apr 11 '14 at 23:58
  • I am asking about how to use document.getelementbyid in this question and the result is `no` in php. The answer is related to the may-be-duplicate question. What's the problem? – Jamie Apr 12 '14 at 00:00
  • I'm trying to teach you a fundamental concept. Read the whole thing and you will get wiser. PS: in the answer there's something like this mentioned `var baz = ;` which is almost the same what John Conde did. The only difference: the duplicate I linked will give you an in-depth explanation – HamZa Apr 12 '14 at 00:04

1 Answers1

2

You can access cookie values directly in JavaScript. But to make your code workable, you just need to use PHP tags and an echo statement:

document.getElementById('User').value = '<?php echo $user; ?>';

Or if you're using PHP 5.4+ or have short echo tags enabled:

document.getElementById('User').value = '<?= $user; ?>';
John Conde
  • 217,595
  • 99
  • 455
  • 496