dongtan6336 2016-08-17 10:36
浏览 43
已采纳

从输入通过ajax获取变量到外部php

I have this in index.php I would like to get my input data (range) as variable into my nova.php. When I am trying to use $range = $_GET['range']; in nova.php it shows : Undefined index: range. I think, I should put something like data:{range: range} into myAjax function. But of course it's not working

<input id="range" type="text" name="range" class="enter" value="">
<input type="button" onclick="myAjax()" value="Show nearby" />

my ajax

function myAjax() {
  $.ajax({
       type: "POST",
       url: 'nova.php',
       data:{action:'call_this'},
       success:function(html) {
         alert(html);
       }
  });
}

and my nova.php:

<?php
if($_POST['action'] == 'call_this') {
$con=mysqli_connect("localhost","tech","151987","portnew");
// Check connection 
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$range = $_GET['range']; 
// Perform queries
$query = $con->query("SELECT ( 6371 * acos( cos( radians(53.216908) )*
cos( radians( lat ) ) * cos( radians( lng ) - radians(7.453240) ) + 
sin( radians(53.216908) ) * sin( radians( lat ) ) ) ) AS distance,     
port_name FROM ports HAVING distance < '".$range."' ORDER BY distance LIMIT 0 , 20; ");

while ($row = $query->fetch_assoc()) {
    $port_list[] = $row['port_name'];
}
echo json_encode($port_list);
}
?> 

So basically this script is looking for nearby locations based on latitude, logitude. It's working when I put ...HAVING distance < 150 ORDER BY..., but I would like to set my range in input, and not using static range

  • 写回答

3条回答 默认 最新

  • douchengjue9892 2016-08-17 10:48
    关注

    Check this jsFiddle

    and you ajax function should be

    function myAjax() {
    var range = document.getElementById('range').value;
      $.ajax({
           type: "POST",
           url: 'nova.php',
           data:{action:'call_this',range:range},
           success:function(html) {
             alert(html);
           }
      });
    }
    

    at php code change this line

    $range = $_GET['range'];
    

    to

     $range = $_POST['range'];
    

    also the input of type range should have a value defined. ex value="100". don't leave it blank

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)