簡體   English   中英

使用wordpress插件將變量從javascript傳遞到php

[英]Passing Variable from javascript to php using wordpress plugin

在我的代碼的頂部,我確實有

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"></script>

然后我實現谷歌地圖API的功能

function geocodeAddress(geocoder, resultsMap) {
    var lat = '';
    var lng = '';
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address}, function(results, status) {
      if (status === 'OK') {
        lat = results[0].geometry.location.lat();
        lng = results[0].geometry.location.lng();
        alert(lng + ' ' +lat);
        nearLocation();
        resultsMap.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: resultsMap,
          position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
}

位置附近通話上方的功能

function nearLocation()
{
    alert("hi");
    var userID = "Hello";
                //alert($(this).attr('id'));
                $.ajax({
                    type: "POST",
                    url: 'script.php',
                    data: "userID=" + userID,
                    success: function(data)
                    {
                        alert("success!");
                    }
    });
}

在我的script.php中

<?php
$uid = isset($_POST['userID']);
?>

但是成功警報不顯示任何原因?

當您向script.php發送請求時,沒有任何輸出。 您只聲明了一個變量,從未將任何輸出發送回已發送的AJAX請求。

將script.php更改為此。

<?php
$uid = isset($_POST['userID']);
echo $uid;

如果您的javascript成功,則添加console.log(data)以查看服務器的響應。 現在有更好的方法來執行此操作,例如返回json,但這應該可以解決您的問題。

旁注:僅包含php的文件中不需要關閉php標記'?>'。 最佳做法是忽略它以避免不必要的空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM