简体   繁体   English

使用Php,mySql,ajax,(xmlhttp)和JS,如何创建php多维将其发送到JS并使用它?

[英]Using Php, mySql, ajax, (xmlhttp) and JS,how can i create a php multidimensional send it to JS and use it?

I looked a lot on the internet and wasn't able to find the answer i need, so here i come to you. 我在互联网上看了很多东西,却找不到我需要的答案,所以我来找你。 What i have : A database which look like this : 我所拥有的:一个像这样的数据库:

name  latitude  longitude
---- --------- ----------
foo    13.323   -51.356
foo    54.698   2.487

What i want to do : I need to retrieve the latitude and longitude from a mysqli request done with php and use it in a function that i defined. 我想做什么:我需要从用php完成的mysqli请求中检索经度和纬度,并将其用于我定义的函数中。

My problem : I'm trying to use xmlrequest but it apparently doesn't work. 我的问题:我正在尝试使用xmlrequest,但是它显然不起作用。

The code : JS : 代码: JS:

var selI = document.getElementById("nameIti");    
selI.onchange = function(){
                var val = this[this.selectedIndex].getAttribute("value");
                showMark(val);
            }
function showMark(str){
            var xhr;
            if(str==""){
                return;
            }
            if(window.XMLHttpRequest){
                xhr=new XMLHttpRequest();
            }
            else{
                xhr=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhr.onreadystatechange=function(){
                if(xhr.readyState==4 && xhr.status ==200){
                    var object = JSON.parse(xhr.responseText);
                    for(var a in object){
                    newMark(v['lat'], v['lng']);
                    document.getElementById("pi").innerHTML=JSON.parse(xrh.responseText); // This is a test to display any kind of result.
                    }                       
                }
            }
            xhr.open("GET", "getpos.php?q="+str, true);
            xhr.send();
        }   

PHP : PHP的:

<?php
$nom = $_GET['q'];

include("connexion.php");
$con = connect_LIF4();
$req1= "SELECT Latitude, Longitude FROM etape LEFT JOIN itineraires ON NomLieu=nomEtape WHERE nomIti LIKE '%$nom%'";
$result1 = mysqli_query($con, $req1);

$data = array();
while($row = mysqli_fetch_array($result1){
    $data['lat'] = $row['Latitude'];
    $data['lng'] = $row['Longitude'];
    $resp[] = $data;
}
echo json_encode($resp);
mysqli_close($con);
?>

I tried to use newMark(lat, lng)(Which i coded and works fine) with random values, in showMark outside the onreadystatechange and it works, but i need to use it with the values retrieved from the php. 我试图将newMark(lat,lng)(我编码并可以正常工作)与随机值一起使用,在onreadystatechange之外的showMark中,它可以工作,但是我需要将其与从php检索的值一起使用。

One problem with your PHP is that PHP的一个问题是

while($row = mysqli_fetch_array($result1){

is missing the second brace. 缺少第二个括号。 It should be: 它应该是:

while($row = mysqli_fetch_array($result1)){

Also the URL in the ajax request should be the full URL, not just getpos.php 另外,ajax请求中的URL应该是完整的URL,而不仅仅是getpos.php

Thirdly you have written xrh.responseText (should be xhr). 第三,您已编写了xrh.responseText(应为xhr)。

Basically there's loads of syntax errors in your code - you should use the javascript console to debug the front end ones, and PHP logging or error display for the back end ones. 基本上,您的代码中存在大量语法错误-您应该使用javascript控制台调试前端代码,并使用PHP日志记录或显示后端代码的错误。 You should only need help here once you've debugged all obvious syntax errors. 调试完所有明显的语法错误后,您只需在这里需要帮助。

EDIT - below is a working example (although I haven't done the MySQL part) 编辑-以下是一个有效的示例(尽管我还没有完成MySQL部分)

JS + HTML: JS + HTML:

<span id='pi'></span>
<select id='nameIti'>
    <option value='foo'>foo</option>
    <option value='bar'>bar</option>
</select>

<script>

function newMark(lat,lng) {
    console.log(lat);
    console.log(lng);
}

var selI = document.getElementById("nameIti");
selI.onchange = function(){
                var val = this[this.selectedIndex].getAttribute("value");
                showMark(val);
            }

function showMark(val){

    var str=val;
    var xhr;
    // if(str==""){
    //     return;
    // }
    if(window.XMLHttpRequest){
        xhr=new XMLHttpRequest();
    }
    else{
        xhr=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange=function(){
        if(xhr.readyState==4 && xhr.status ==200){
            var result = JSON.parse(xhr.responseText);
            console.log(result);
            for(var a in result){
                newMark(result[a]['lat'], result[a]['lng']);
                document.getElementById("pi").innerHTML = result[a]['lat'] + ', ' + result[a]['lng'];
            }
        }
    }
    // xhr.open("GET", "getpos.php?q="+str, true);
    xhr.open("GET", "getpos.php?q="+str, true);
    xhr.send();
}
</script>

PHP: PHP:

<?php
$nom = $_GET['q'];

$data = array();
if($nom == 'foo') {
  $data['lat'] = '5.12';
  $data['lng'] = '0.34';
  $resp[] = $data;
}
else if($nom == 'bar') {
  $data['lat'] = '2.34';
  $data['lng'] = '1.34';
  $resp[] = $data;
}
echo json_encode($resp);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM