PHP:使用PHP中的参数调用javascript函数
我试图用PHP变量的参数调用JavaScript函数。 我尝试了两种方法PHP:使用PHP中的参数调用javascript函数,php,javascript,function,google-maps,google-maps-api-3,Php,Javascript,Function,Google Maps,Google Maps Api 3,我试图用PHP变量的参数调用JavaScript函数。 我尝试了两种方法 在PHP中使用echo中的脚本标记调用JavaScript函数 i、 e 将PHP变量值分配给JavaScript变量 <script> var lat="<?php echo $lat;?>"; var lang="<?php echo $lang; ?>"; var zoom="<?php echo $zoom; ?>"; alert(lat+lang+zoom); initial
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>
<?php
if( isset($_POST['lat']) && isset($_POST['lang']) && isset($_POST['zoom']) && isset($_POST['city'])):
$lat=$_POST['lat'];
$lang=$_POST['lang'];
$zoom=$_POST['zoom'];
$city=$_POST['city'];
$zom=(int)$zoom;
var_dump($lang);
var_dump($lat);
//var_dump($zoom);
var_dump($zom);
//echo '<script>initialize('.$lat.','.$lang.','.$zom.');</script>';
endif;
?>
<script>
var lat="<?php echo $lat; ?>";
var lang="<?php echo $lang; ?>";
var zoom="<?php echo $zoom; ?>";
alert(lat+lang+zoom);
initialize(lat,lang,zoom);
</script>
<script>
function initialize(a,b,zom){
if (!a || !b ||!zom){
alert('came on not' +a+b +zom);
// var centerLoc=new google.maps.LatLng( 33.61701054652337,73.37824736488983);
zoom=16;
}
else
{
alert('came');
var zoom =parseInt(zom);
var centerLoc=new google.maps.LatLng(a,b);
}
var mapProp = {
center:centerLoc,
zoom:zoom,
//mapTypeId:google.maps.MapTypeId.ROADMAP
mapTypeId:google.maps.MapTypeId.SATELLITE
};
var map=new google.maps.Map(document.getElementById("googleMap") ,mapProp);
marker=new google.maps.Marker({
position:centerLoc,
title:'Click to zoom'
});
google.maps.event.addListener(marker,'click',function() {
map.setZoom(map.getZoom()+1);
map.setCenter(marker.getPosition());
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body style= "background-color:gainsboro;">
<form method="POST" action="myPage.php" >
Enter latitude: <input type ="text" name="lat" id="lat" / ><br/>
Enter longitude: <input type ="text" name="lang" id="lang"/ ><br/>
Enter City Name: <input type="text" name="city" id="city"/><br/>
Enter Zoom level: <input type ="text" name="zoom" id="zoom"/ ><br/>
<input type="button" value ="Perview" onclick=" initialize(
document.getElementById('lat').value,
document.getElementById('lang').value,
document.getElementById('zoom').value)"/>
<input type="Submit" value="Save" />
</form>
<center><div id="googleMap" style="width:1000px;height:500px;"></div></center>
</body>
</html>
var lat=“”;
var lang=“”;
var zoom=“”;
警报(横向+横向+缩放);
初始化(横向、横向、缩放);
函数初始化(a,b,zom){
如果(!a | | |!b | |!zom){
警报(‘未启动’+a+b+zom);
//var centerLoc=new google.maps.LatLng(33.61701054652337,73.37824736488983);
缩放=16;
}
其他的
{
警惕(“来了”);
var zoom=parseInt(zom);
var centerLoc=新的google.maps.LatLng(a,b);
}
var mapProp={
中心:centerLoc,
缩放:缩放,
//mapTypeId:google.maps.mapTypeId.ROADMAP
mapTypeId:google.maps.mapTypeId.SATELLITE
};
var map=new google.maps.map(document.getElementById(“googleMap”),mapProp);
marker=新的google.maps.marker({
位置:centerLoc,
标题:“单击以缩放”
});
google.maps.event.addListener(标记,'click',函数(){
map.setZoom(map.getZoom()+1);
map.setCenter(marker.getPosition());
});
marker.setMap(map);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
输入纬度:
输入经度:
输入城市名称:
输入缩放级别:
在浏览器加载完javascript后调用该函数
<script>
window.onload = function() {
var lat="<?php echo $lat; ?>";
var lang="<?php echo $lang; ?>";
var zoom="<?php echo $zoom; ?>";
alert(lat+lang+zoom);
initialize(lat,lang,zoom);
};
</script>
window.onload=函数(){
var lat=“”;
var lang=“”;
var zoom=“”;
警报(横向+横向+缩放);
初始化(横向、横向、缩放);
};
使用。如果不这样做,那么在数据从PHP传递到HTML/JS层时,总是有可能错误地转义数据
$vars = array($lat, $lang, $zoom);
// JSON_HEX_TAG and JSON_HEX_AMP are to remove all possible surprises that could be
// caused by vars that contain '</script>' or '&' in them. The rules for
// escaping/encoding inside script elements are complex and vary depending
// on how the document is parsed.
$jsvars = json_encode($vars, JSON_HEX_TAG | JSON_HEX_AMP);
echo "<script>initialize.apply(null, $jsvars)</script>";
$vars=array($lat、$lang、$zoom);
//JSON_HEX_标记和JSON_HEX_AMP将消除所有可能发生的意外
//由其中包含“”或“&”的变量引起。规则
//脚本元素内部的转义/编码非常复杂,并且会根据需要而变化
//关于如何解析文档。
$jsvars=json_encode($vars,json_HEX_TAG | json_HEX_AMP);
echo“initialize.apply(null,$jsvars)”;
一般来说,为了您的理智,您需要使当前页面上运行的js可用的PHP中的所有数据都应该收集到单个PHP数组中,然后放入单个js对象中。例如:
<?php
$jsdata = array(
'formvars' => array(
'lat' => $lat,
'lang' => $lang,
'zoom' => $zoom
),
'username' => $username,
'some_other_data' => $more stuff
);
?>
<script>
var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>;
initialize(JSDATA.formvars.lat, JSDATA.formvars.lang, JSDATA.formvars.zoom);
</script>
var JSDATA=;
初始化(JSDATA.formvars.lat、JSDATA.formvars.lang、JSDATA.formvars.zoom);
现在,JS和PHP/HTML层之间只有一个接触点,因此您可以轻松地跟踪您在JS名称空间中输入的内容。只需调用预定义的java脚本代码,如jsFunction();在您的php代码中我在上找到了一些非常好的示例,您也可以在 为了防止链接中断,以下是示例: 示例1:无参数调用
<?php
echo "<a href='http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/'>Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/</a>";
echo "<p>Add whatever PHP you want here...</p>";
?>
<!--This JS function can be defined here or a separate file since so long as it gets created in JS space'-->
<script>
function callAlert(){
alert('A alert without a parameter');
}
</script>
<script>
callAlert();
</script>
<?php
?>
<?php
echo "<a href='http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/'>Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/</a>";
echo "<p>Add whatever PHP you want here...</p>";
$myname = 'MyName';
$yourname = 'YourName';
//Data that is going to be passed into the JavaScript function. Try to keep all vars together so
//that it's easier to track down the php/javascript interaction
$jsdata = array(
'input' => $myname,
'array_input' => array(
'name' => $yourname
),
);
?>
<!--This JS can be here or a separate file since all it's doing is defining a function in the JS space'-->
<script>
function callAlert(text){
alert(text);
}
</script>
<!--This JS must be defined with the php since it's using previously defined php variables -->
<script>
var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>;
//Prompt using a single var in the array
callAlert(JSDATA.input);
//Prompt using a var from a nested array
callAlert(JSDATA.array_input.name);
</script>
<?php
?>
函数callAlert(){
警报(“没有参数的警报”);
}
callAlert();
示例2:使用单个参数调用
<?php
echo "<a href='http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/'>Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/</a>";
echo "<p>Add whatever PHP you want here...</p>";
//Data that is going to be passed into the JavaScript function. Try to keep all vars together so
//that it's easier to track down the php/javascript interaction
$jsdata = 'MyName';
?>
<!--This JS can be here or a separate file since all it's doing is defining a function in the JS space'-->
<script>
function callAlert(text){
alert(text);
}
</script>
<!--This JS must be defined with the php since it's using previously defined php variables -->
<script>
var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>;
//Prompt using a single var
callAlert(JSDATA);
</script>
<?php
?>
;
//使用单个变量进行提示
callAlert(JSDATA);
示例3:使用参数数组调用
<?php
echo "<a href='http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/'>Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/</a>";
echo "<p>Add whatever PHP you want here...</p>";
?>
<!--This JS function can be defined here or a separate file since so long as it gets created in JS space'-->
<script>
function callAlert(){
alert('A alert without a parameter');
}
</script>
<script>
callAlert();
</script>
<?php
?>
<?php
echo "<a href='http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/'>Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/</a>";
echo "<p>Add whatever PHP you want here...</p>";
$myname = 'MyName';
$yourname = 'YourName';
//Data that is going to be passed into the JavaScript function. Try to keep all vars together so
//that it's easier to track down the php/javascript interaction
$jsdata = array(
'input' => $myname,
'array_input' => array(
'name' => $yourname
),
);
?>
<!--This JS can be here or a separate file since all it's doing is defining a function in the JS space'-->
<script>
function callAlert(text){
alert(text);
}
</script>
<!--This JS must be defined with the php since it's using previously defined php variables -->
<script>
var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>;
//Prompt using a single var in the array
callAlert(JSDATA.input);
//Prompt using a var from a nested array
callAlert(JSDATA.array_input.name);
</script>
<?php
?>
;
//在数组中使用单个变量进行提示
callAlert(JSDATA.input);
//使用嵌套数组中的变量进行提示
callAlert(JSDATA.array\u input.name);
检查JavaScript控制台是否有错误。当执行echo'initialize('.$lat.','.$lang.','.$zom.')
,你在生成的页面的源代码中得到了什么?试着调试你的js-例如,使用Chrome的控制台调用这个函数,或者它只是“存在”?您可能需要在body onload
,something onclick
等中调用它";代码>似乎有点奇怪。PHP处理后是什么样子的?谢谢你的回复,但我不需要在窗口加载时调用它。谁可能会下载这个答案?我提供了三个例子来回答这个日益复杂的问题,同时还提供了视频,为那些喜欢音频/视频解决方案的用户解释了这一点。所以,请向落选的选民解释,而不是打了就跑!!!