I would like to pass on a $_POST['foo'] variable to a JavaScript variable while I am using a php if- statement. But its not working. Any suggestions on how to make it run?
<?php
if(isset($_POST['foo']))
{
?>
<script>
var jFoo= <?php echo json_encode($_POST['foo']); ?>;
</script>
<?
}
else {
...
}
?>
EDIT:
This is weird: I was actually trying to make the code run like this (this wasnt working):
if(isset($_POST['foo']))
{
?>
<script>
var jFoo= <?php echo json_encode($foo); ?>;
</script>
<?
}
else {
...
}
However, when I just did this, it worked:
<?php
if(isset($_POST['foo']))
{
?>
<script>
var jFoo= <?php echo json_encode($_POST['foo']); ?>;
</script>
<?
}
else {
...
}
Any ideas why?
$_POSTvariables can also be arrays