0

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

Based on the .avflipswitch checkbox state, I want to toggle the value of my PHP variable $LinkOpen between '_blank' and '_self' so I can push that value to my Google CSE link target attribute.


What I have tried so far:

 $('.avflipswitch').on("change", function (e){ 
   if(this.checked){
     functionOne(<?php $LinkOpen = '_blank';?>);
   }
   else{
     functionTwo(<?php $LinkOpen = '_self';?>);
   }
});

<gcse:searchresults-only linktarget="<?php echo $LinkOpen;?>"></gcse:searchresults-only>
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
Leo
  • 314
  • 1
  • 6
  • 27

2 Answers2

6

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>
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • Yes thanks again, your code works perffect, can you give to me a suggestion, how I can add and safesearch button, off and active? – Leo Jan 11 '19 at 22:13
  • I'm not sure what you mean Leo. safesearch button? – AndrewL64 Jan 11 '19 at 22:14
  • same thing like this, but no _blank/_self, i want to do off/active example if checked SafeSearch="active" if unchecked SafeSearch="off" – Leo Jan 11 '19 at 22:16
  • Oh ok. So you want to add this toggle attribute to an HTML element or something? – AndrewL64 Jan 11 '19 at 22:19
  • with checkbox to allow user to change the search angine SafeSearch="active" SafeSearch=="off" so on change I want to change active to off, or off to active, but that is diferent, if I don't find solution I ask again new question, thanks again for your help. – Leo Jan 11 '19 at 22:23
  • if you have time please take a look at this question https://stackoverflow.com/questions/54078071/how-set-filter-manually-on-google-custom-search-engine-free-version – Leo Jan 11 '19 at 22:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186569/discussion-between-andrewl-and-leo). – AndrewL64 Jan 11 '19 at 22:29
  • Hello again! is possible, to do this function only with js or php? – Leo Jan 14 '19 at 13:49
  • on first page load do not have any function so linktarget is empty, I need to checkbox, after that start the fnction, and when i change the query example from web, to video still the linktarget is empty so i need to uncheck and check again, after that start working! can you help me to fix this please? – Leo Jan 14 '19 at 16:31
  • I'm not sure I understand what you mean Leo. So you want the checkbox to be checked every time a page load? Or? – AndrewL64 Jan 14 '19 at 18:50
  • if I set it` _blank` from this url example: `https://example.com/search/?q=blablabla` that works but when I change it `https://example.com/news/search/?q=blablabla` that is not working so I need to turn off on again to get `_blank` – Leo Jan 27 '19 at 20:54
-1

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")
    }
});
Hans Dash
  • 761
  • 4
  • 18