Jump to content

Pass JS Variable to PHP variable. Please save me!


stevengreen22

Recommended Posts

Hi all,

 

This problem is killing me.

 

I hope / pray someone can help.

 

Essentially I need to pass a JS variable to a PHP variable without refresh/form submission or redirecting.  

I'm new to Ajax so please be patient :)

 

 

  1.  
  2.  
  3. The owner of this site generally lives in 3 countries lucky ******.  When he logs in he wants the contact page updated with his current number.
  4.  
  5. I have a form with hidden fields that return the address location using an ajax call to ipinfo.io - this works fine.
  6.  
  7. <script>
  8.         $(function() {
  9.             var admin = <?php echo json_encode($admin->isAdmin($user['username'])); ?>;
  10.             var phone="";
  11.  
  12.             if(admin == true){
  13.  
  14.             $.ajax({
  15.                 url:"http://ipinfo.io",
  16.                 dataType:'jsonp'
  17.             })
  18.                 .success(function(response){
  19.  
  20.                     $("#address").val(response.country);
  21.                     $("input[id=address]").val(response.country);
  22.  
  23.                     var testLoc = $('#address').val();
  24.                     if(testLoc == "GB"){
  25.                         phone = "<?php echo $gb?>";
  26.                         $("#phone").append(phone);
  27.                     }else if(testLoc == "GABON"){
  28.                         phone = "<?php echo $gabon?>";
  29.                         $("#phone").append(phone);
  30.                     }else if(testLoc == "THAILAND"){
  31.                         phone = "<?php echo $thai?>";
  32.                         $("#phone").append(phone);
  33.                 }
  34.  
  35.                 });
  36.  
  37.             var time = new Date();
  38.             $("#localTime").append(time);
  39.             }else{
  40.                 //alert("Not Admin");
  41.                 $("#phone").append(phone);
  42.             }
  43.         });
  44.     </script>
  45.  
  46. The above essentuially checks to see that he is THE admin, if it is it then sets the hidden field value to be the country, that is then placed in the testLoc variable and depending on what it is, it spits out the number.  This all works well unless of course you visit the site and you're not the admin.  All you see is an empty string.
  47.  
  48. I need to get that phone variable or location variable, either of them really from the js into a php variable.
  49.  
  50. That way when a normal joey visits the page I can just echo out that variable instead. 
  51.  
  52. phone or location var in js script to php variable?
Link to comment
Share on other sites

Rather than complicate this process with JS and AJAX, just do the lookup to ipinfo.io using PHP before the page loads. If fopen wrappers are on, a simple file_get_contents call will suffice. Otherwise you can use CURL

<?php

$phones = array(
   'GB' => 'blah
   , 'GABON' => 'foo'
   , 'THAILAND' => 'bar'
);

$url = 'http://ipinfo.io/'.$_SERVER['REMOTE_ADDR'].'/country';
$country = file_get_contents($url);

var_dump($country, $phones[$country]);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.