0

I am doing an silly mistake in getting the location of the browser and storing using an external ip , ajax and php file.I am getting an error as

Uncaught SyntaxError: Unexpected token :

I think i am not sending the data properly in the php file, Here is my AJAX function:

$(document).ready(function() {
  $.get("https://ipinfo.io", function(response) {
      var city_data = val(response.city);
      var region_data = val(response.region);
        data: {city: city_data, region: region_data},
        type: "post",
        url: "test3.php",
        success: function(data){
             alert("Data Saved: " + data);
        }
}, "jsonp");

});

and my php file as :

<?php
 if(isset($_REQUEST))
 {
 mysql_connect("localhost","root","");
 mysql_select_db("practice");
 error_reporting(E_ALL && ~E_NOTICE); 
 print_r($_POST);
 return;
  $city=$_POST['city'];
  $region=$_POST['region'];
  $sql="INSERT INTO location(area,country) VALUES ('$city','$region')";
  $result=mysql_query($sql);
  if($result){
  echo "Work Done.";
   } 
   }
   ?>

I used mysql as this is for testing, Please have a look on my codes and let me know where i went wrong. Thanks in advance.

5
  • Please check your syntax. You have the body of your callback function mingled with AJAX properties. Commented Aug 1, 2017 at 16:28
  • 4
    since you are practicing, why dont you adopt PDO right now ? will save you tons if issues later, not to mention that mysql_* is deprecated right now. Read about little bobby and SQL injection. Commented Aug 1, 2017 at 16:35
  • @DarthJDG Can you write in the answer field where i did wrong as i am not getting it. Thanks Commented Aug 1, 2017 at 16:36
  • @YvesLeBorg Sure ,I will practice with PDO here after , Can you tell me where i went wrong Commented Aug 1, 2017 at 16:37
  • Well the return; saves you from SQL injections because the SQL will never get executed. If called from the global scope, then execution of the current script file is ended. Commented Aug 1, 2017 at 16:40

2 Answers 2

1

You have error in syntax, who say @DarthJDG Add $.ajax to your request

Example that will serve you for your problem

jQuery(document).ready(function($){
  $.getJSON('https://ipinfo.io', function(data){
      var city_data = data.city;
      var region_data = data.region;
      $.ajax({
        data: {city: city_data, region: region_data},
        type: "post",
        url: "test3.php",
        success: function(data){
             alert("Data Saved: " + data);
        }
      })
  })

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

2 Comments

It does solved my error but the data is not getting stored in the database
You remove print_r($_POST); return; ? and out print $sql execute in mysql client or command line mysql and see which output delivers the command line
1

You mixed up the data object and the success callback. According to the documentation of jQuery.get(), the first parameter is the url, the second the data-object, the third the callback if the request succeeded and the last one the data type (like json, html or xml).

$(document).ready(function() {
    $.get("https://ipinfo.io", function(response) {
        console.log(response);

        var city_data = response.city;
        var region_data = response.region;

        $.post("test3.php",
            {
                // data
                city: city_data,
                region: region_data
            },
            function (data) {
                // success
                console.log("data successfully saved.");
            }
        )
    }, "json")
        .fail(function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown)
        });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

5 Comments

Hello, I am not getting any error but the data is also not getting stored in the database
Take a look into the developer tools console. Do you get an error?
Add the error controller or die(mysqli_error()) after the mysqli_query() function to see if something's wrong with the query.
Is the script inside the if(isset($_REQUEST)) reachable? Otherwise try to replace it with if ($_SERVER["REQUEST_METHOD"] == "POST").
I got it what i am looking for . Thank you for the help

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.