Pass variable from javascript to PHP

前端 未结 5 367
一向
一向 2020-12-16 06:22

I\'m using Google Maps to make a map that can load markers with lat/lng data stored in a database. I want there to be three different \'layers\' that the user can load by cl

相关标签:
5条回答
  • 2020-12-16 06:56

    If you do an ajax get request with the following url

    somePhpFile.php?varName=10
    

    Within your somePhpFile.php, you can do

    $v = $_GET['varName'];
    
    0 讨论(0)
  • 2020-12-16 06:57

    If your using AJAX requests it's pretty easy to pass variables to a php file. Here is a quick example.

     $('#your-button').on("click", function(){
           var somevar1 = "your variable1";
           var somevar2 = "your variable2";
        $.ajax({
            type:"POST",
            url: "your-phpfile.php",
            data: "variable1=" + somevar1 + "\u0026variable2="+ somevar2,
            success: function(){
            //do stuff after the AJAX calls successfully completes
        }
    
        });
    });
    

    Then in your php file you simple access the varables using

     $ajax_var1 = $_POST['variable1'];
     $ajax_var2 = $_POST['variable2'];
    
    0 讨论(0)
  • 2020-12-16 06:57

    Here is Mike Williams' (v2) tutorial on "The AJAX Philosophy", where he does exactly what you are asking about.

    (I should note that the map uses Google Maps API v2, but this concept is not API version specific)

    0 讨论(0)
  • 2020-12-16 06:57

    Quote From Vijay S :

    Please try this:

    We can pass a value from javascript to PHP.

    we can use as,

    $getValue = echo "<script>document.write(your script variable);</script>";

    Modified a little bit it worked good for me :

    $getValue = "<script>document.write(YourVarHere);</script>";
    echo $getValue;
    
    0 讨论(0)
  • 2020-12-16 07:05

    Please try this:

    We can pass a value from javascript to PHP.

    we can use as,

    $getValue = "<script>document.write(your script variable);</script>";
    
    0 讨论(0)
提交回复
热议问题