Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP echos编写JavaScript代码(用于传单映射点)_Javascript_Php_Mysql_Leaflet_Leaflet.draw - Fatal编程技术网

使用PHP echos编写JavaScript代码(用于传单映射点)

使用PHP echos编写JavaScript代码(用于传单映射点),javascript,php,mysql,leaflet,leaflet.draw,Javascript,Php,Mysql,Leaflet,Leaflet.draw,我试图在传单中创建地图点,它使用javaScript编写点,例如: L.marker([39.0865039, -9.2636987], {icon: tree3}).addTo(map) .bindPopup('<a class="speciallink">Olive Tree</a>') .openPopup(); L.marker([39.0865039,-9.2636987],{icon:tree3}).addTo(map) .bindPopup(“橄榄树”

我试图在传单中创建地图点,它使用javaScript编写点,例如:

L.marker([39.0865039, -9.2636987], {icon: tree3}).addTo(map)
 .bindPopup('<a class="speciallink">Olive Tree</a>')
 .openPopup();
L.marker([39.0865039,-9.2636987],{icon:tree3}).addTo(map)
.bindPopup(“橄榄树”)
.openPopup();
在MySQL中,我存储了coords,然后我尝试生成以下代码:

    <?php
     $res=mysql_query("SELECT * FROM arv_especie");
     $rowArv=mysql_fetch_array($res);
     $idPointVarzea=mysql_query("SELECT * FROM map_point_varzea");
     while ($row=mysql_fetch_array($idPointVarzea)) {
       echo "L.marker([" . $row["coordHori"] . ", " . $row["coordVerti"] . "], {icon: tree" . $row["ARV_id"] . "}).addTo(map)
       .bindPopup($('<a class='speciallink'>" . $rowArv["ARV_Especie"] . "</a>').click(function() {
       document.getElementById('DivShow" . $row["ARV_id"] . "').click();
       })[0])
       .openPopup();";
     }
?>

有没有更好的方法使用PHP编写javaScript代码

嗯。你没有

虽然从PHP动态编写JS很酷,但它也很容易发生错误和/SQL注入攻击

一种更简洁的方法是使用PHP在JS中编写变量的值,然后让JS完成所有工作。您的代码将更好地分割,更容易调试,并且您将有一个必须转义变量的单点

比如:

<script>

<?php
$res=mysql_query("SELECT * FROM arv_especie");
$rowArv=mysql_fetch_array($res);
$idPointVarzea=mysql_query("SELECT * FROM map_point_varzea");

// This will hold a data structure mimicking a GeoJSON FeatureCollection
// See http://geojson.org/
$featureCollection = [ "type"=>"FeatureCollection", "features"=>[] ];

while ($row=mysql_fetch_array($idPointVarzea)) {

    $feature = [
        "geometry"=>[
            "type"=> "Point",
            "coordinates"=> [ $row["coordVerti"] , $row["coordHori"] ]
        ],
        "properties"=> [
            "species"=>$rowArv["ARV_Especie"],
            "id"=>$row["ARV_id"]
        ]
    ];

    $featureCollection["features"][] = $feature;
}
?>

// PHP shall echo() $featureCollection as JSON, which JS will parse nicely.
// From here on, the JS variable "dataInGeoJsonFormat" holds all the needed data
var dataInGeoJsonFormat = <?php echo json_encode($featureCollection); ?>

var markers = L.geoJSON(dataInGeoJsonFormat, {
    onEachFeature: function(feat, layer) {
        layer.bindPopup('ID: ' + feat.properties.id + '<br>Species:' + feat.properties.species)
    }
}).addTo(map);

</script>

//PHP将echo()$featureCollection作为JSON,JS将很好地解析它。
//从这里开始,JS变量“dataInGeoJsonFormat”保存所有需要的数据
var dataInGeoJsonFormat=
var markers=L.geoJSON(dataInGeoJsonFormat{
onEachFeature:功能(专长,图层){
layer.bindPopup('ID:'+feat.properties.ID+'
物种:'+feat.properties.Species) } }).addTo(地图);
我的PHP很生疏,但你应该明白。如果使用此模式,您将能够调试javascript并查看数据。使用GeoJSON等知名格式意味着您可以欣赏其他地方的功能和教程

通过在代码中有一个将数据编码为JSON的点(以及使用PHP内置的
JSON\u encode
),您将省去很多麻烦和潜在的错误。您可以在JS代码中设置断点,并在任何JS代码运行之前检查该变量的值

这不是唯一可用的模式。然而,这将防止你犯错误

有没有更好的方法使用PHP编写javaScript代码

嗯。你没有

虽然从PHP动态编写JS很酷,但它也很容易发生错误和/SQL注入攻击

一种更简洁的方法是使用PHP在JS中编写变量的值,然后让JS完成所有工作。您的代码将更好地分割,更容易调试,并且您将有一个必须转义变量的单点

比如:

<script>

<?php
$res=mysql_query("SELECT * FROM arv_especie");
$rowArv=mysql_fetch_array($res);
$idPointVarzea=mysql_query("SELECT * FROM map_point_varzea");

// This will hold a data structure mimicking a GeoJSON FeatureCollection
// See http://geojson.org/
$featureCollection = [ "type"=>"FeatureCollection", "features"=>[] ];

while ($row=mysql_fetch_array($idPointVarzea)) {

    $feature = [
        "geometry"=>[
            "type"=> "Point",
            "coordinates"=> [ $row["coordVerti"] , $row["coordHori"] ]
        ],
        "properties"=> [
            "species"=>$rowArv["ARV_Especie"],
            "id"=>$row["ARV_id"]
        ]
    ];

    $featureCollection["features"][] = $feature;
}
?>

// PHP shall echo() $featureCollection as JSON, which JS will parse nicely.
// From here on, the JS variable "dataInGeoJsonFormat" holds all the needed data
var dataInGeoJsonFormat = <?php echo json_encode($featureCollection); ?>

var markers = L.geoJSON(dataInGeoJsonFormat, {
    onEachFeature: function(feat, layer) {
        layer.bindPopup('ID: ' + feat.properties.id + '<br>Species:' + feat.properties.species)
    }
}).addTo(map);

</script>

//PHP将echo()$featureCollection作为JSON,JS将很好地解析它。
//从这里开始,JS变量“dataInGeoJsonFormat”保存所有需要的数据
var dataInGeoJsonFormat=
var markers=L.geoJSON(dataInGeoJsonFormat{
onEachFeature:功能(专长,图层){
layer.bindPopup('ID:'+feat.properties.ID+'
物种:'+feat.properties.Species) } }).addTo(地图);
我的PHP很生疏,但你应该明白。如果使用此模式,您将能够调试javascript并查看数据。使用GeoJSON等知名格式意味着您可以欣赏其他地方的功能和教程

通过在代码中有一个将数据编码为JSON的点(以及使用PHP内置的
JSON\u encode
),您将省去很多麻烦和潜在的错误。您可以在JS代码中设置断点,并在任何JS代码运行之前检查该变量的值


这不是唯一可用的模式。但是,这将防止您犯错误。

我建议编写一个javascript层,从php/crud服务器请求geojson,而不是尝试将geojson嵌入php构造的响应中。您将有更好的关注点分离,如果您感兴趣,有几个php框架和库能够生成geojson或促进crud系统

例如:支持mysql几何体


披露:我负责D2 Spatial和传单。Draw

我建议编写一个javascript层,从php/crud服务器请求geojson,而不是尝试将geojson嵌入php构造的响应中。您将有更好的关注点分离,如果您感兴趣,有几个php框架和库能够生成geojson或促进crud系统

例如:支持mysql几何体


披露:我正在制作D2 Spatial和传单。Draw

为什么不使用ajax调用您的
php
文件。让php返回坐标,以便您可以在使用ajax的过程中处理它们……我会尝试一下,感谢您的时间。请停止使用不推荐的mysql_*API。使用mysqli_*或PDO。然后,您可以避免与此库相关的安全问题,并使用参数化查询来正确保护自己免受SQL注入攻击。尽管此处显示的查询不易受攻击,但您编写的任何依赖变量来完成查询的内容都可能遭到黑客攻击。另外,mysql在PHP7中也被完全删除,所以每当你升级到这个版本时,你的代码都会停止工作。我知道mysql不如mysqli好,我正在上编程课程的第二年,第一年是C和ASP.net,今年是PHP,我的老师教我们mysqli,但我不知道为什么我更喜欢mysqli,但是我认为你是对的,是时候升级了,如果在做出更改后一切正常,我将警告你,感谢你的时间为什么不使用ajax调用你的
php
文件。让php返回坐标,以便您可以在使用ajax的过程中处理它们……我会尝试一下,感谢您的时间。请停止使用不推荐的mysql_*API。使用mysqli_*或PDO。然后,您可以避免与此库相关的安全问题,并使用参数化查询来正确保护自己免受SQL注入攻击。尽管此处显示的查询不易受攻击,但您编写的任何依赖于变量来完成查询的内容都将被广泛接受