[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

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>
0
jezella
Asked:
jezella
1 Solution
 
emphaticDigitalCommented:
Hello,
Try this:
<?php
session_start();
if(isset($_POST['formsubmit'])){
    $_SESSION['userType'] = $_POST['formsubmit'];
}else{
     $_SESSION['userType'] = 'guest';
}
?>
0
 
CWS (haripriya)Commented:
<?php
session_start();
$_SESSION['userType'] = $_POST['formsubmit'];
?>
0
 
gowniCommented:
<?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';

}

?>
0
 
siva_sivaCommented:
<?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>
0
 
jezellaAuthor Commented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now