Link to home
Start Free TrialLog in
Avatar of jezella
jezellaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

variable $_SESSION in Form Submit

Hi all.  Hope you can help a php learner.

I have created a form with 2 submit buttons so that a choice between registering or logging in is offered.

When the form is sent I wish to be able to change the value of $_SESSION['userType'] = 'guest' from guest to either registration request or loginRequest.

Help here would be appreciated where my code is below. I feel that this could be achieved onClick before the form is posted though perhaps this may be better.

<?php
session_start();
$sessionValue = $_POST['formsubmit'];
$_SESSION['userType'] = $sessionValue;
?>

Many thanks

Jezella


<?php
session_start();

      $_SESSION['userType'] = 'guest';
?>

<br/><h2 align = "center">Welcome to the Ashley Site</h2> <h2 align = "center">You Must be a Logged in Member to Use
this Site</h2> <h3 align = "center">Select Login or Register<h3>

<table align = "center" border = "1" width = "250">
      <form method = "post" action = "index.php">
            <tr>
                  <td>Register</td>

                  <td>Login</td>
            </tr>

            <tr>
                  <td><input type = "submit" name = "formsubmit" value = "register" title = "Register"></td>

                  <td><input type = "submit" name = "formsubmit" value = "login" title = "Login"></td>
            </tr>
      </form>
</table>
ASKER CERTIFIED SOLUTION
Avatar of emphaticDigital
emphaticDigital

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<?php
session_start();
$_SESSION['userType'] = $_POST['formsubmit'];
?>
<?php
session_start();
if(isset($_POST['formsubmit]))
{
   if( $_POST['formsubmit] == "register")  $_SESSION['userType'] = 'register';
   if( $_POST['formsubmit] == "login")  $_SESSION['userType'] = 'login';

  }else
{
$_SESSION['userType'] = 'guest';

}

?>
<?php
session_start();
if(isset($_POST['registersubmit'])){
      $_SESSION['userType'] = $_POST['registersubmit'];
      header('location: register.php');
}
else if(isset($_POST['loginsubmit'])){
      $_SESSION['userType'] = $_POST['loginsubmit'];
      header('location: login.php');
}
else{
     $_SESSION['userType'] = 'guest';
}
?>

<br/><h2 align = "center">Welcome to the Ashley Site</h2> <h2 align = "center">You Must be a Logged in Member to Use
this Site</h2> <h3 align = "center">Select Login or Register<h3>

<table align = "center" border = "1" width = "250">
      <form method = "post" action = "index.php">
            <tr>
                  <td>Register</td>

                  <td>Login</td>
            </tr>

            <tr>
                  <td><input type = "submit" name = "registersubmit" value = "register" title = "Register"></td>

                  <td><input type = "submit" name = "loginsubmit" value = "login" title = "Login"></td>
            </tr>
      </form>
</table>
Avatar of jezella

ASKER

Thank you to all and sorry for the delay in my reply. I have read all the comments here and feel that all will achieve what I wanted to do.  I think that the answer supplied by emplaticdigital may however be a shorter method.

Many thanks

Jezella.