How do I edit PHP variable with JavaScript/jQuery?

后端 未结 2 1276
野性不改
野性不改 2020-12-07 00:08

I want to update a PHP variable $LinkOpen based on the current state of a checkbox element .avflipswitch.

Based on the .avflipswitch<

相关标签:
2条回答
  • 2020-12-07 00:42

    What you are trying is not possible in php, but very easy with jquery:

    $('.avflipswitch').on("change", function (e){ 
        if(this.checked){
         $('a').attr("target","_self")
        }
        else{
         $('a').attr("target","_blank")
        }
    });
    
    0 讨论(0)
  • 2020-12-07 00:54

    You can just push the value to a cookie using JavaScript or jQuery and then let PHP retrieve the value from that cookie like this:

    JavaScript + PHP:

    /* JavaScript */
    const switch = document.querySelector(".avflipswitch")[0];
        
    switch.addEventListener("change", function(){
      let blankTar = "_blank";
      let selfTar = "_self";
          
      if(this.checked){
        document.cookie = "target =" + blankTar;
        window.location.reload();
      }
      else{
        document.cookie = "target =" + selfTar;
        window.location.reload();
      }
    }
    
    /* PHP */
    
    <gcse:searchresults-only linktarget="<?php echo $_COOKIE['target']; ?>"></gcse:searchresults-only>

    jQuery + PHP:

    /* jQuery */
    
    $('.avflipswitch').on("change", function (){
      let blankTar = "_blank";
      let selfTar = "_self";
    
      if(this.checked){
        document.cookie = "target =" + blankTar;
        window.location.reload();
      }
      else{
        document.cookie = "target =" + selfTar;
        window.location.reload();
      }
    });
    
    /* PHP */
    
    <gcse:searchresults-only linktarget="<?php echo $_COOKIE['target']; ?>"></gcse:searchresults-only>

    0 讨论(0)
提交回复
热议问题