我可以从PHP数组中移动数组从JavaScript数据库回收 [英] can i move array from php to javascript retrive from mysql database
问题描述
我的php代码get_marker_connect2.php
<?php
$ servername =localhost;
$ username =root;
$ passcode =;
$ dbname =complaint_system;
$ con = mysqli_connect($ servername,$ username,$ passcode,$ dbname);
//检查连接
if(mysqli_connect_errno()){
echo无法连接到MySQL:。 mysqli_connect_error();
}
//存储查询
$ result = mysqli_query($ con,SELECT l_longitude,l_latitude FROM register_complain);
echo< table border ='1'>
< tr>
th纬度< />
< /< / ;经度< / th>
< / tr>;
while($ row = mysqli_fetch_array($ result)){
echo< tr>;
回显< td> 。 $ row ['l_longitude']。 < / TD> 中;
回显< td>
。 $ row ['l_latitude']。 < / TD> 中;
回声< / tr>;
}
echo< / table>;
header('Location:http://localhost/cca/View_Map.html');
mysqli_close($ con);
?>
这是我的javascript,命名为javascript1.js
function initialize(){
var myLatLng = new google.maps.LatLng(24.86,67.01);
var mapOptions = {
center:myLatLng,
zoom:14,
mapTypeControl:true,
mapTypeId:google.maps.MapTypeId.ROADMAP} ;
alert('alert array here');
$ b $ * for(var i = 0; i< latlong1.length; i ++){
alert(Longitude =。latlong1 [i] .longitude);
alert(Latitude =。latlong1 [i] .latitude);
}
* /
map = new google.maps.Map(document.getElementById(view-map),
mapOptions);
var marker = new google.maps.Marker({
position:simple,
map:map,
title:'Hello Karachi!'});
}
google.maps.event.addDomListener(window,'load',initialize);
可以直接移动php数组$ row ['l_longitude']和$ row ['l_latitude']到我的JavaScript?我有谷歌它很多,但还没有找到解决方案。
i希望在我的javascript中提醒该数组,在alert array here中
如果您希望打印复杂结构插入到在浏览器中工作的页面中,您需要Javascript可以解析的语法,并且可以打印服务器端语言。
neareset的可能性为:
我现在推荐使用JSON。
$ b 使用PHP,您可以在某处写入:
echovar myTransportedArrayJson = \。json_encode($ my_array)。\;
然后让Javascript解析你的json表达式:
var myTransportedArray = JSON.parse(myTransportedArrayJson);
alert(myTransportedArray); //<<<在这里,你是。
当然,片段必须是响应的一部分,否则它不会到达服务器。另外,你应该用 try .. catch
来解析。如果继续工作并使用更复杂的结构,则必须考虑转义某些字符。所以你现在会发现一些文献:-)
my php code get_marker_connect2.php
<?php
$servername = "localhost";
$username = "root";
$passcode = "";
$dbname = "complaint_system";
$con=mysqli_connect($servername,$username,$passcode,$dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//store query
$result = mysqli_query($con,"SELECT l_longitude,l_latitude FROM register_complain");
echo "<table border='1'>
<tr>
<th>Latitude</th>
<th>Longitude</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['l_longitude'] . "</td>";
echo "<td>
" . $row['l_latitude'] . "</td>";
echo "</tr>";
}
echo "</table>";
header('Location:http://localhost/cca/View_Map.html');
mysqli_close($con);
?>
This is my javascript named as javascript1.js
function initialize() {
var myLatLng = new google.maps.LatLng(24.86, 67.01);
var mapOptions = {
center: myLatLng,
zoom: 14,
mapTypeControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP};
alert('alert array here');
/*for (var i = 0; i < latlong1.length; i++) {
alert(" Longitude = ". latlong1[i].longitude);
alert(" Latitude = ". latlong1[i].latitude);
}
*/
map = new google.maps.Map(document.getElementById("view-map"),
mapOptions);
var marker = new google.maps.Marker({
position: simple,
map: map,
title: 'Hello Karachi!'});
}
google.maps.event.addDomListener(window, 'load', initialize);
can i move php array $row['l_longitude'] and $row['l_latitude'] directly to my javascript? i have google it alot but have not found solution yet. i want to alert that array in my javascript at "alert array here"
If you wish to print a complex structure into your page that is worked in a browser, you need a syntax that Javascript can parse, and the serverside language can print.
The neareset possibilities are:
- XML
- JSON
I recommend JSON for now.
So with PHP, you write somewhere:
echo "var myTransportedArrayJson = \"" . json_encode($my_array) . "\"";
And then let the Javascript parse your json expression:
var myTransportedArray = JSON.parse(myTransportedArrayJson);
alert(myTransportedArray); // <<< and here you are.
Of course the snippet must be part of the response, otherwise it won't reach the server. Also, you should embrace the parsing with try .. catch
. If you go on and work with even more complex structures, you have to consider escaping certain chars. So you will find now some literature :-)
这篇关于我可以从PHP数组中移动数组从JavaScript数据库回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!