首页 > 解决方案 > 如果通过按钮访问页面,请勾选复选框

问题描述

寻求一些帮助以了解如何添加以下功能:

我的 WP 网站上有一个联系页面,其中有一个联系 7 表格和一个用于注册时事通讯的复选框。

可以通过两种方式访问​​联系页面 - 显然是通过菜单,但主页上还有一个“为我注册时事通讯”按钮,当点击该按钮时,当前也会将用户带到联系页面。我想这样当用户点击主页上的按钮时,它不仅会将他/她带到联系页面,而且还会自动勾选那里的复选框。如果用户通过菜单或任何其他方式访问联系页面,我不希望自动勾选复选框。

我想我正在寻找一些 php 逻辑,但任何想法都值得赞赏。

标签: phpwordpresscheckbox

解决方案


Add PHP GET string to button link then read that on other side and make logic like:

if page is contact-page.php, then on button add link contact-page.php?button=true

on contact-page.php read that ?button=true with PHP:

if (isset($_GET["button"])) {
   //echo that checkbox checked 
}else{
   //echo that checkbox not checked 
}

or in JS:

if (window.location.href.indexOf("button") > -1) {
      //check checkbox with JS
  }

Other option is that you use JS localStorage , when you click a button set localStorage button: true, then read that on contact-page.php and if its true check checkbox with JS


推荐阅读