简体   繁体   English

JavaScript 值到 PHP 变量

[英]JavaScript value to PHP variable

JavaScript code: 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 code 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>

The JavaScript code should set the cookie, and then php get the values saved in the cookie. JavaScript 代码应该设置 cookie,然后 php 获取保存在 cookie 中的值。 I basically want to send value from JavaScript to php on the same page.我基本上想在同一页面上将值从 JavaScript 发送到 php。 but it seems to be an error "Notice: Undefined index: blog in C:\wamp64\www\test\ajax.php on line 40".但它似乎是一个错误“注意:未定义索引:第 40 行 C:\wamp64\www\test\ajax.php 中的博客”。 I have searched a lot but didn't find the proper solution.我搜索了很多但没有找到合适的解决方案。 How can I correct my code?如何更正我的代码?

Jquery ajax:查询阿贾克斯:

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

HTML code: 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>

Ajax code:阿贾克斯代码:

echo $_COOKIES[“blog”];

So first create HTML structure then call ajax in javascript by jquery and last create ajax file.所以首先创建 HTML 结构,然后通过 jquery 在 javascript 中调用 ajax,最后创建 ajax 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM