need to set session variable when submit a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

need to set session variable when submit a form

Post by me666 »

i need to set a session variable when someone submits a form, i was thinking something like onsubmit=""
can any1 help me?
thanks
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: need to set session variable when submit a form

Post by panic! »

onsubmit is for javascript you need to set the session variable in php.

http://uk2.php.net/manual/en/function.session-start.php

Code: Select all

 
<?php
// page1.php
 
session_start();
 
echo 'Welcome to page #1';
 
$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();
 
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';
 
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: need to set session variable when submit a form

Post by aceconcepts »

if someone submits a form you follow a very similar approach as panic has suggested - however, you do need to determine whether a form has been submitted:

Code: Select all

 
if(isset($_POST['submit']))
{
   $_SESSION['form_submitted']=1;   
}
 
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: need to set session variable when submit a form

Post by me666 »

thanks this helped, i edited it slightly, and it works fine, does the job i need it to

thanks
Post Reply