Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140705

JavaScript value to PHP variable

$
0
0

JavaScript code:

<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

<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. I basically want to send value from JavaScript to php on the same page. but it seems to be an error "Notice: Undefined index: blog in C:\wamp64\www\test\ajax.php on line 40". I have searched a lot but didn't find the proper solution. How can I correct my code?


Viewing all articles
Browse latest Browse all 140705

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>