【问题标题】:send javascript variable value to sql query in PHP on different page将javascript变量值发送到不同页面上的PHP中的sql查询
【发布时间】:2022-03-04 18:14:27
【问题描述】:

我有 2 个元素。自动搜索栏 和地图(mapbox-gl)

  • 我想加载其 id 被搜索的用户的位置。
  • 我正在为每个用户获取相同的标记(位置)
  1. 我正在通过 xmlHttpRequest 从主页(“monitor.php”)加载“get_pin.php”。
  2. “get_pin.php”有查询 $results=$myPDO->query("SELECT * FROM clients where id =".$user_id); 使用此查询,我获取搜索其姓名的人的 id,然后获取该 id 的数据,创建 geojson,这是 get_pin.php 的 json 编码结果。 "geometry":{"type":"Point","coordinates":[143.3601,42.6500000000001]},"type":"Feature","properties":[]}

现在,我得到 , SELECT * FROM clients where id = 1,通过硬编码值。所以即使我搜索不同的人的 id。它将标记加载到同一位置。

我试图解决这个问题 - “监视器.php” HTML

<tr>//table appears after the id is searched
<th>id</th>
<td><label id="lbl_id" name="lbl_id">'.$row["id"].'</label></td>
</tr>//further I also get other info of user in the same table

在同一页上-

function load_data(query, typehead_search = 'yes') {
            $.ajax({
            url: "dbconn.php",
            method: "POST",
            data: { query: query, typehead_search: typehead_search },
            success: function (data) {
                //show the table inside employee_data div
                $('#employee_data').html(data);
                //get the id and store it in js variable 
                var l_id = document.getElementById('lbl_id').textContent;
                    alert(l_id);//I am getting the correct id
                    $.ajax({
                        type : "POST",
                        url  : "get_pin.php",
                        data : {l_id : l_id},
                        success: function(data){  
                                console.log(data);
                                },
                        error: function(err){
                            console.log(err);
                        }
                    });
            }
        });
    }

在 get_pin.php 上我正在获取数据

    if (isset($_POST["l_id"])) {
         require ("db_conn.php");  
         $user_id =$_POST["l_id"];
            try {             
            $results=$myPDO->query("SELECT * FROM clients where id =".$user_id); 
            /*get data convert it to a geojson*/

错误- ajax 中的成功函数为我提供了特定 id 的正确 geojson 输出。 但是在我调用 get_pin.php.There 的 xmlhttprequest 中,响应是空白的。 我得到-

Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.request.onload

如何将表中的 id 值传递给 get_pin.php 并在地图上获取该标记?

【问题讨论】:

    标签: javascript php jquery ajax xmlhttprequest


    【解决方案1】:

    我得到了解决方案。 我使用 XMLHttpRequest 来传递变量,而不是 jQuery ajax。

    //this is to get and show the initial position of the user
    var url = 'get_marker.php?id=';
    
    map.on('load', function() {
      var request = new XMLHttpRequest();
      //I have added setInterval here to get updated value 
      var clearIntervalValue = window.setInterval(function() {
    
        var id = document.getElementById('l_id').textContent;
    
        url = 'get_marker.php?id=';
        url = url + id;
        request.open('GET', url, true);
    
        request.onload = function() {
    
          if (this.status >= 200 && this.status < 400) {
            console.log(this.response);
    
            var json = JSON.parse(this.response);
    
            map.getSource('drone').setData(json);
            map.flyTo({
              center: json.geometry.coordinates,
              speed: 0.5
            });
    
          } else if (this.status == 404) {
            clearInterval(clearIntervalValue);
          }
    
        };
        request.send();
    
      }, 2000);
      //rest of the code...
    });
    <tr>
      <th>id</th>
      <td><label id="l_id" name="l_id">'.$row["id"].'</label></td>
    </tr>

    在 get_marker.php 中

    <?php
      //check if the id is set
      if (isset($_GET["id"])) 
      {
       //include database connection file
       require ("db_conn.php");
       $results=$myPDO->query("SELECT * FROM clients where id =".$_GET["id"]); 
      }
    ?> 
    

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多