change php session value by click

33,121

Solution 1

'onclick' will not fire php code. It will trigger javascript though. You can use javascript to make AJAX calls to a php page that'd in turn be able to set your session values (and ajax would help you do so without a page refresh on the button click.

#('.btn-auth btn-facebook large').click(function(){
// fire off the request to /redirect.php
request = $.ajax({
    url: "/redirect.php",
    type: "post",
    data: 'facebook'
});

// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
    // log a message to the console
    console.log("Hooray, it worked!");
});

// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
    // log the error to the console
    console.error(
        "The following error occured: "+
        textStatus, errorThrown
    );
    });
});

In your redirect.php

<?php

$_SESSION['role'] = $_POST['data'];

?>

Solution 2

A possible solution is to add a GET parameter in redirect.php and change the SESSION variable in redirect.php.

Change the following :

<p><a class="btn-auth btn-facebook large" href="redirect.php" onclick="<?php $_SESSION['role']="facebook" ?>" > Sign in with <b>Facebook</b> </a></p>

<p><a class="btn-auth btn-twitter large" href="redirect.php" onclick="<?php $_SESSION['role']="twitter" ?>" > Sign in with <b>Twitter</b> </a></p>

<p><a class="btn-auth btn-google large" href="redirect.php" onclick="<?php $_SESSION['role']="google" ?>" > Sign in with <b>Google</b> </a></p>

to :

<p><a class="btn-auth btn-facebook large" href="redirect.php?role=facebook"> Sign in with <b>Facebook</b> </a></p>

<p><a class="btn-auth btn-twitter large" href="redirect.php?role=twitter"> Sign in with <b>Twitter</b> </a></p>

<p><a class="btn-auth btn-google large" href="redirect.php?role=google"> Sign in with <b>Google</b> </a></p>

and add this at the top of the redirect.php

<?
session_start(); 
$_SESSION['role']=$_GET['role'];
?>

Solution 3

You can also add another php file to change session variable.

Like this:

<p><a class="btn-auth btn-facebook large" href="pass.php"> Sign in with <b>Facebook</b> </a></p>

and in pass.php you have to add this code:

<?php
    session_start();
    $_SESSION['role']="facebook"; 
    header("Location: your_first_php.php");
?>
Share:
33,121
user2828251
Author by

user2828251

Updated on August 27, 2020

Comments

  • user2828251
    user2828251 over 3 years

    i want to give a value to a session by clicking on

    i tried to do this , but its not working:

    <?php session_start(); 
    $_SESSION['role']="";?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
    <title></title>
    <link href="auth-buttons.css" rel="stylesheet" />
    <link href="StyleSheet.css" rel="stylesheet" />
       </head>
    <body>
    
    <div id="wrap">
    <div id="wrapHome">
    <p><a class="btn-auth btn-facebook large" href="redirect.php" onclick="<?php $_SESSION['role']="facebook" ?>" > Sign in with <b>Facebook</b> </a></p>
    
    <p><a class="btn-auth btn-twitter large" href="redirect.php" onclick="<?php $_SESSION['role']="twitter" ?>" > Sign in with <b>Twitter</b> </a></p>
    
    <p><a class="btn-auth btn-google large" href="redirect.php" onclick="<?php $_SESSION['role']="google" ?>" > Sign in with <b>Google</b> </a></p>
    </div>
    </div>
    </body>
    </html>
    
  • DS.
    DS. over 10 years
    This won't achieve what he is looking for I believe. This would take him to another page on the button click. I'd think he needs to use AJAX.
  • Subin
    Subin over 10 years
    No, He already made a href attribute in the url's. I think he wants to change the session value before going to redirect.php. The answer do the same task, but in a different way.
  • user2828251
    user2828251 over 10 years
    thank you so much ! both answers helped me... but now there is a another problem - the "redirect page" moves to a third page - where i also need the session there...
  • DS.
    DS. over 10 years
    @user2828251, Once you have set the session variable, and your session is active on all your pages, the changes you make in redirect.php should be available on your third page as well.
  • Gem
    Gem over 4 years
    @Subin onclick="this.disabled=true" if page refresh disabled event unset, how to make sustain disabled if page refresh?