我看过一些与此有关的帖子,但在那里得不到多少帮助。
我试图将数组从php文件传递到js文件 这是php文件中的代码
<!DOCTYPE html >
<html >
<head> ------ </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>
&#13;
和js文件
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);
});
}
&#13;
我面临的问题是,在js文件中,错误来自&#34;数据未被声明&#34;并且js文件中的第二个警报也不起作用。在php文件中可以看到php echos但不是警报。
提前致谢
答案 0 :(得分:0)
在身体之前添加JS就像这样。
<?php
session_start();
$_SESSION['street']= $this->street;
$_SESSION['city']= $this->city;
$_SESSION['state']= $this->state;
$_SESSION['zip']= $this->zip;
?>
<!DOCTYPE html >
<html >
<head> ------ </head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
var data = {
var1:'<?php echo $this->variable;?>',
path:'<?php echo $this->path;?>'
}
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);
});
}
</script>
<body onload="gotofunction()">
<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);
?>