$code = $_POST['code'];
echo "<script language='javascript'>
document.getElementById('code').value = $code </script>";
I tried '$code', "$code","..." + "%code + "...", and i cant change the textfield's value to the PHP value.
Mmm!!!
$code = $_POST['code'];
echo "<script language='javascript'>
document.getElementById('code').value = $code </script>";
I tried '$code', "$code","..." + "%code + "...", and i cant change the textfield's value to the PHP value.
Mmm!!!
Try this :
$code = $_POST['code'];
echo "<script language=\"javascript\">
document.getElementById(\"code\").value =". $code. "</script>";
Or better yet :
<?php
$code = str_replace("'", "\'", htmlspecialchars($_POST['code']));
?>
<script type="text/javascript">
document.getElementById("code").value = "<?php echo $code;?>";//OR <?= $code;?>;
</script>
Does $code contain a string? If so you'll need to quote it.
e.g. if $code equals "my test string", the Javascript will be output like:
$code = $_POST['code'];
echo "<script language='javascript'>
document.getElementById('code').value = my test string </script>";
which is invalid Javascript.
You need to:
$code = str_replace("'", "\'", $_POST['code']);
echo "<script language='javascript'>
document.getElementById('code').value = '$code' </script>";
I would also do:
$code = str_replace("'", "\'", htmlspecialchars($_POST['code']));
htmlspecialchars quotes HTML characters to prevent XSS attacks.
PHP doesn't see the $code as you've written it. Use this instead:
$code = $_POST['code'];
echo "<script language='javascript'>
document.getElementById('code').value = ".$code."; </script>";
or
$code = $_POST['code'];
echo "<script language='javascript'>
document.getElementById('code').value = {$code}; </script>";
Either more explicitly allows PHP to recognise and parse your string.