Link to home
Start Free TrialLog in
Avatar of peter Ojeda
peter Ojeda

asked on

Getting script result to php variable

Hi experts I am using a small script (posted below) to get the location for my web form. I am able to display the location. I want to turn that location into a php variable to use in a text input somewhere else on my web form. Any suggestion on how I should do this? I  tried to use $_GET but I may have done it wrong.
Script:
<script>

$.get("https://ipinfo.io/json", function (response) {
   
   $location=$("#address").html("Location: " + response.city + ", " + response.region);
    //$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
</script>

Open in new window

Area in html I am trying to put the script result into
Address:<input type="text" name="Address"   style="width: 150PX;" value="(location here)">

Open in new window


Thankyou for any feedback
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

One option is to use jquery/js to fill in the form field. A better option is to leave this out of  your form and capture the data on the back end when the form is  processed. The docs show you can use curl https://ipinfo.io/developers
http://jsbin.com/yolukenisi/edit?html,output
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <input type="text" name="Address"   style="width: 150PX;" value="(location here)">
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script>
  $(function(){
    $location="123456";
    $('input[name="Address"]').val($location);
  });
  </script>
</body>
</html>

Open in new window

greetings peter Ojeda, , You ask about -
    "I want to turn that location into a php variable to use in a text input somewhere else on my web form"
The javascript and PHP operations are on TWO separate computers, the user browser computer and the server Host computer, so to have any javascript variables exchanged from the javascript to the server, you can use a page <form> submit (not good for you here) , , an AJAX exchange of info-data (that could be used here, and or sometimes by a cookie, also not good for you here?

This may be a way to handle this? ? depending on what you need? ? -
<script>

$.get("https://ipinfo.io/json", function (serverObj) {
// get the text you want for location into a variable like stringLoc
   var stringLoc = serverObj.city + ", " + serverObj.region;
   $("#address").html("Location: " + stringLoc); // change the html
   $("input[name='Address']").val(stringLoc);// change the value

// now send the string for location to the PHP page ajax-var.php to save it
// using a GET Ajax, this does NOT use JSON return, but you can do that here
  $.get( "ajax-var.php", { location: stringLoc },  function (serverText) {
      alert( "This is what the PHP server sent\n"+serverText);
       });
}, "jsonp");
</script>

Open in new window

the code above is Untested, but may give you an outline for something to try.
you will need a PHP page to process the $_GET['location']; sent to it by the ajax, and use or save that value

ask questions if you need other information.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial