如何使用JavaScript / jQuery编辑PHP变量? [英] How do I edit PHP variable with JavaScript/jQuery?

查看:57
本文介绍了如何使用JavaScript / jQuery编辑PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据复选框元素 .avflipswitch 的当前状态更新PHP变量 $ LinkOpen



根据 .avflipswitch 复选框状态,我想切换我的PHP变量的值 $ LinkOpen '_ blank''_ self'之间,所以我可以推我的 Google CSE 链接目标属性的价值。






到目前为止我尝试了什么:

  $('。avflipswitch')。on(change,function(e){
if(this。已检查){
functionOne(<?php $ LinkOpen ='_ blank';?>);
}
else {
functionTwo(<?php $ LinkOpen =' _self';?>);
}
});

< gcse:searchresults-only linktarget =<?php echo $ LinkOpen;?>>< / gcse:searchresults-only>


解决方案

您可以使用<将值推送到cookie strong> JavaScript 或 jQuery ,然后让 PHP 从该Cookie中检索此值:



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>  






JavaScript + PHP:



< pre class =snippet-code-js lang-js prettyprint-override> / * 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>


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>

解决方案

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:

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>


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>

这篇关于如何使用JavaScript / jQuery编辑PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆