How do I edit PHP variable with JavaScript/jQuery?

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>

Leave a Comment