A
A
Aligatro2014-02-13 16:29:40
PHP
Aligatro, 2014-02-13 16:29:40

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 'Ты не жмякнул на ссылку'}

or
if(my_function(true)) {echo 'Ты жмякнул на эту ссылку'} else { echo 'Ты не жмякнул на ссылку'}

It will be just gorgeous if this can be done in pure php without js admixture. For example, to catch the page address: mysite.ru/function.php?linkon=1, after which the page will be refreshed and the function will take on a different value.
The task seems to be trivial and simple, but due to little experience in php development, I don’t understand how to transfer the very data about the click on the link. I would be extremely grateful to those who can show me a ready (similar) example or throw a link to one.
To all who responded, I express my deep gratitude in advance for the help and time devoted to me.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Rsa97, 2014-02-13
@Aligatro

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>';
}
?>

A
Andrey Unger, 2014-02-13
@Cobalt

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

A
Aligatro, 2014-02-13
@Aligatro

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.

A
Aligatro, 2014-02-15
@Aligatro

@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;
}
?>

Ps Correction, in order not to suffer with the header and bother with spaces, just place the above code in functions.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question