开发者

PHP - put a variable in session only if link is clicked

开发者 https://www.devze.com 2023-03-25 07:18 出处:网络
how can I put a variable in session only if link is cl开发者_Python百科icked? Note: My link have to submit a form at the same time.If you\'re using someting like this in your HTML to submit your form

how can I put a variable in session only if link is cl开发者_Python百科icked?

Note: My link have to submit a form at the same time.


If you're using someting like this in your HTML to submit your form

<input type="submit" name="sender" value="Send" />

you can check the $_POST['sender'] Variable. It will output ether Send (clicked on button) or nothing (clicked on link).


You can make a hidden input inside the form

<input type="hidden" id="link_is_clicked" name="link_is_clicked" value="0"/>

then when clicking the link, change the value of that input

<a href="..." onclick="document.getElementById('link_is_clicked').value = 1" ...

then check for the value of the element on the server side.


Just check whether your form is submitted or not, if so set the session variable.

For example:

if(isset($_POST) && count($_POST) > 0)) //Assuming your form's action is post, use GET otherwise
{
  $_SESSION['somevar'] = 'somevalue';
}


I assume you're using JavaScript to make the link submit the form? You can add a hidden input that has value 0. When the user clicks on the link, change the value of the input to 1 before submitting.

0

精彩评论

暂无评论...
验证码 换一张
取 消