簡體   English   中英

JavaScript 值到 PHP 變量

[英]JavaScript value to PHP variable

JavaScript 代碼:

<script type="text/javascript">
$("#test").click(function() {
  getQuerystring(this);
  return false;
});

function getQuerystring(el) {
  console.log(el.href);
  var getUrlParameter = function(sParam) {
    var sPageURL = el.href.split('?')[1],
      sURLVariables = sPageURL.split('&'),
      sParameterName;

    for (var i = 0; i < sURLVariables.length; i++) {
      sParameterName = sURLVariables[i].split('=');

      if (sParameterName[0] === sParam) {
        return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
      }
    }
  };

  var blog = getUrlParameter('c');
  //document.getElementById('detail').innerHTML = blog;
  document.cookie = "blog = " + blog;
}
</script>

html代碼

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="border: dashed; size: auto;">
  <a name="divtest" href="#detail?c=active" id="test">testing</a>
</div>
<div id="detail" style="border: 2px; size: auto;">
    <?php
    $chk = $_COOKIE['blog'];
    echo $chk;
    ?>
</div>

JavaScript 代碼應該設置 cookie,然后 php 獲取保存在 cookie 中的值。 我基本上想在同一頁面上將值從 JavaScript 發送到 php。 但它似乎是一個錯誤“注意:未定義索引:第 40 行 C:\wamp64\www\test\ajax.php 中的博客”。 我搜索了很多但沒有找到合適的解決方案。 如何更正我的代碼?

查詢阿賈克斯:

$.ajax({
    url: "path_to_ajax_file",
    type: "post",
    success: function (response) {
            document.getElementById(“result”);
    }
 });

HTML 代碼:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="border: dashed; size: auto;">
<a name="divtest" href="#detail?c=active"  id="test">testing</a>
</div>
<div id="detail" style="border: 2px; size: auto;">
  <span id=“result”></span>
</div>

阿賈克斯代碼:

echo $_COOKIES[“blog”];

所以首先創建 HTML 結構,然后通過 jquery 在 javascript 中調用 ajax,最后創建 ajax 文件。

暫無
暫無

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

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