Answer the question
In order to leave comments, you need to log in
How to change the value of a function when a link is clicked in php?
Good afternoon, dear toasters. Just now I ran into a problem in implementing a function in php or php+js.
The bottom line is this ... I need to create a function that would change its value (to true or false) when clicking on a specific link on the site. In order to further create a condition of the type based on this:
if(my_function()) {echo 'Ты жмякнул на эту ссылку'} else { echo 'Ты не жмякнул на ссылку'}
if(my_function(true)) {echo 'Ты жмякнул на эту ссылку'} else { echo 'Ты не жмякнул на ссылку'}
Answer the question
In order to leave comments, you need to log in
On the page
in php
<?php
session_start();
if (isset($_REQUEST['version'])) {
$_SESSION['version'] = $_REQUEST['version'];
// Общий код сайта
if ($_SESSION['version'] == 'mobile') {
//Код мобильной версии
echo '<a href="mysite.ru/?version=desktop">На десктопную версию</a>';
} else {
//Код десктопной версии
echo '<a href="mysite.ru/?version=mobile">На мобильную версию</a>';
}
?>
I didn't quite understand the question. There are different ways to solve:
1. Make the link just a link that will lead to the same page but with the parameter ?click=true and in the script you can see whether such a parameter was passed to you or not: if($_GET['click']==' true'){
2. Make a hidden form with input type=hidden and by id javascript shove the desired value there and call submit
3. Call the desired script via ajax when clicking on the
link
That is, if I understand you correctly, you can simply create a link of the form:
And inside the cycle (I use wordpress) create the condition that I described above
Or are additional dances with a tambourine needed?
In order to more accurately convey the idea, I want to make the mechanics of switching from the mobile version of the site to the desktop one, but without using a mirror like m.mysite.ru . With the help of "conditions" I need to cut some functionality inside the wordpress cycle, remove/compress images, disable some styles and scripts =)
Thank you.
@Rsa97
's solution helped (thank you). I want to warn those who may wander here from the search, session_start () should be located in the page header (before the doctype). Spaces, indentation, comments before <?php will crash wp. Save all files in UTF-8 encoding (without BOM). Only one session can exist per page. For parallel sessions, use session_id(). I may be wrong in the latter)
This is what my working version looks like:
<?php session_start();
if (isset($_REQUEST['version'])) {
$_SESSION['version'] = $_REQUEST['version'];
}
function mobileview($on){
$on=$_SESSION['version'] == 'mobile';
return $on;
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question