0

I'm trying to pass an array from php file to a js file
here is the code in php file

<!DOCTYPE html >
<html >
<head>
<style>------</style>
</head>

<body onload="gotofunction()">
<?php
session_start();
$_SESSION['street']= $this->street;
$_SESSION['city']= $this->city;
$_SESSION['state']= $this->state;
$_SESSION['zip']= $this->zip;
?>

<script type="text/javascript">
    function voodoo(){
          alert('alert in voodoo');  // 1st alert
          document.contents.submit();
       }

</script>
</body>
</html>

<?php echo "php text";   //1st echo
        $var= json_encode($this->variable);
?>

<script type="text/javascript">
        var data = {
        var1:'<?php echo $this->variable;?>',
        path:'<?php echo $this->path;?>'
        }

</script>

here is the js file content

function gotofunction(){

        alert('alert number1');  //1st alert
        alert(' data '+data.var1 );  2nd alert
        $.post(data.path+'res/data_controller/tablog1',{'var':data.var1},function(data){
            $('#pos1').html(data);
        });
    }

the problem I'm facing is that in the js file, the error comes as "data is not declared" and also the 2nd alert in js file doesnt work. In the php file php echos can be seen but not the alerts.

Thanks in advance

Edited by JamesCherrill: Moved to Programming forum

2
Contributors
1
Reply
22
Views
2 Months
Discussion Span
Last Post by michael.dewitt.716
0

have you tried performing a var_dump on the json_encode on line 27? Also how are you calling the function in the js file when its not linked to your page?

have you tried ajax?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.