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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!