0

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

1
  • if any of the answers were correct mark as correct answer please Commented Aug 18, 2016 at 10:01

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

And if I would like use variable values from db, so instead of 53.216908 and 7.453240. i will use $lat and $lng where port_name = $port. I will probably need two queries. first SELECT lat,lng FROM ports WHERE port_name = $port; while ($row = $query->fetch_assoc()) { $lat = $row['lat']; $lng = $row['lng']; } And then second one where I will use $lat and $lng instead of 53.216908 and 7.453240. Am I right?
2

You are using post method so use $_POST not $_GET like,

$range = $_POST['range']; 

And you need to pass it in data like,

data:{action:'call_this',range:$('#range').val()},

Comments

1

you are using $_GET and you should use $_POST or you can use $_REQUEST when you don't know how the datas are coming

$range = $_REQUEST['range']; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.