Page 1 of 1

Submitting Session Values

Posted: Fri Nov 19, 2010 12:13 pm
by brmcdani44
I have a event registration form that I want to submit the user's username or userID to the event database as well as the event information so I can build another form that will allow the user to edit his/her post. I am just trying to figure out how to pull their username from the session and submit it without the user having to enter it into a text box. Can anyone help me out with this?

Thanks in advance.

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 12:35 pm
by s992
Are you trying to submit this information along with a form or are you using it to query the database before presenting a form?

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 12:41 pm
by $var
You can do it with PHP or AJAX, I think AJAX might be a little easier.

But since this is a PHP board:
A tutorial on how to use MySQL INSERT with PHP to interact with the MySQL database: http://www.tizag.com/mysqlTutorial/mysqlinsert.php
One on using MYSQL SELECT to get the data from the database: http://w3schools.com/PHP/php_mysql_select.asp

And here is an example of what a select form looks like looped:
<label>*Package Type:</label>
<select name="package-type" id="package-type" class="validate[required]">
<option value="" selected="selected">Select</option>
<?php while($row = mysql_fetch_array($result))
{
print '<option value="'.$r->type.'">'.$r->name.'</option>';
} ?>
</select>

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 12:50 pm
by Neilos
Do you want to store the form data in a db?

input_form1.php

Code: Select all

<form name="input" action="input.php" method="post">
Username: <input type="text" name="username" maxlength="30" />
Info: <input type="text" name="info" maxlength="100" />
<input type="submit" value="Submit" />
</form>
then have input.php

Code: Select all

<?php
$dbhost = 'localhost';
$dbname = 'blah';
$dbuser = 'blah';
$dbpass = 'blah';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);

if (!$conn) {
	session_destroy();
	die('Could not connect: ' . mysql_error());
}

	$username = $_POST['username'];
	$info = $_POST['info'];

	//sanitize username email and number
	$username = mysql_real_escape_string($username);
	$info = mysql_real_escape_string($info);

$query = "INSERT INTO data ( username, info )
	VALUES ( '$username' , '$info' );";
					
mysql_query($query);

header('Location: retrieve_form.php')

?>
Then have retrieve_form.php

Code: Select all

<form name="input" action="retrieve.php" method="post">
Username: <input type="text" name="username" maxlength="30" />
<input type="submit" value="Submit" />
</form>

Then have retrieve.php

Code: Select all

<?php
session_start();

$dbhost = 'localhost';
$dbname = 'blah';
$dbuser = 'blah';
$dbpass = 'blah';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);

if (!$conn) {
	session_destroy();
	die('Could not connect: ' . mysql_error());
}

$username = $_POST['username'];

$query = mysql_query("SELECT * FROM data WHERE username = '$username';");

$num_rows = mysql_num_rows($query);

if($num_rows == 0) {
	//no rows selected
	die();
} else {
	$userData = mysql_fetch_array($query, MYSQL_ASSOC);

	$username_ = $userData['username'];
	$userinfo = $userData['info'];

	$_SESSION['username'] = $username_;
	$_SESSION['userinfo'] = $userinfo;

	header('Location: anotherplace.php')
}

?>
Then when you make anotherplace.php just make sure you have;

Code: Select all

<?php
session_start();
?>
At the top and you can access the session variables and use them however you wish.

Here we used a form to take input, write it to a table, then read it from the table depending on the username input and stored it as session variables.

I hope this was helpful and what you are looking for else you'll have to be more specific.

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 12:56 pm
by Neilos
did you edit your post? lol.

Well if you have the username saved as a session variable then you can submit that to the db easy. Similarly when they visit a new page just pull the record for that username and fill out stuff according to that.

If it is for a form then input type=text has a value attribute that serves as the default value, I think, although it's been a while that these are displayed in the text field on loading, just plug the user data into these.

Is that what you wanted?

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 1:42 pm
by brmcdani44
Yeah I did edit the original post it was a little confusing! lol

Here is what I have built so far:

A calendar that has a mySQL backend that users within my website can post basketball tournaments to which they are hosting after they have registered for the service. I have a way for users to edit their postings but all they have to do is enter their tournament date and retrieve the data then update the data. The problem is they get to choose from all of the tournaments rather or not it is theirs or not to edit.

My question is how should I insert the users username with the tournament they posted dynamically without them having to re enter their username in a text box first? I want to just pull it from the session data. I hope this a little more clear sorry about the previous post!

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 2:22 pm
by Neilos
How are you storing details about the tournaments at the moment? ie tell me about the table and how you update it. It'll be easier to work off the code you already have than create something new for a problem I don't fully understand! :D

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 3:14 pm
by brmcdani44
Well currently there is not user data being stored in the table. I have added the field 'username' so it will be ready for action! Here is the code for that posting form.

Check.php is included in the posting form here it is

Code: Select all

<?php

session_start(); 

if(session_is_registered('username')){ 

// Session exists, allow the user to view the page. 

} else { 

// Session doesn't exist, redirect to login and exit the page.

header( "Location: login.php" ); 
exit();

} 

?> 
<b>Here is the actual form coding</B>

Code: Select all

<?php
include('check.php');
include('dbconn.php');
include('header.php');

if(!isset($_POST['new_event']) && !isset($_POST['add_event'])) {
?>

<h3>Add Event</h3>

<form action="" method="post">

<label>Name</label><input type="text" name="name" /><br />
<label>Descrption</label><textarea name="desc" class="tinymce" cols="30" rows="10"/><?php echo $row['description'];?></textarea>
<br /><br />

<label>Location:</label>
<select name="location">
<option value="Alan Henry">Alan Henry</option>
<option value="Amistad">Amistad</option>
<option value="Falcon">Falcon</option>
<option value="Fort Phantom">Fort Phantom</option>
<option value="Hubbard Creek">Hubbard Creek</option>
<option value="Oak Creek Reservoir">Oak Creek Reservoir</option>
<option value="O.H. Ivie">O.H. Ivie</option>
</select>
<br />
<label>Date (Day/Month/Year)</label><input type="text" readonly="true" name="date" id="datepicker" />

<br /><br />

<label>Time From (24hr)</label>
<select name="from">
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
</select>:
<select name="from2">
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="00" selected="selected">00</option>
</select>
<br />

<label>Time Until (24hr)</label>
<select name="until">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
</select>:
<select name="until2">
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="00" selected="selected">00</option>
</select>

<br /><br />

<input type="submit" value="Continue" name="new_event" />

</form>

<?php

}

if(isset($_POST['add_event']) && $_GET['add'] == 'new') {

	// Get POST vars.
	$name 		= unserialize(base64_decode(stripslashes($_POST['name'])));
	$desc 		= unserialize(base64_decode(stripslashes($_POST['desc'])));
	$location 	= unserialize(base64_decode(stripslashes($_POST['location'])));
	$date 		= unserialize(base64_decode(stripslashes($_POST['date'])));
	$from 		= unserialize(base64_decode(stripslashes($_POST['from']))).unserialize(base64_decode(stripslashes($_POST['from2'])));
	$until 		= unserialize(base64_decode(stripslashes($_POST['until']))).unserialize(base64_decode(stripslashes($_POST['until2'])));

	$d_for = explode('/', $date);
	
	$day = $d_for[0];
	$month = $d_for[1];
	$year = $d_for[2];
	
	if(!$name) { echo "<div class='error_message'>You must enter an event name</div>"; exit(); }
	if(!$desc) { echo "<div class='error_message'>Please enter an event description</div>"; exit(); }
	if(!$location) { echo "<div class='error_message'>You must enter a location for your event</div>"; exit(); }
	if(!$date) { echo "<div class='error_message'>Your event must have a date</div>"; exit(); }

	$sql = "INSERT INTO calendar_event (event, description, location, day, month, year, time_from, time_until)
				VALUES (
					'".mysql_real_escape_string($name)."', 
					'".mysql_real_escape_string($desc)."', 
					'".mysql_real_escape_string($location)."', 
					'".mysql_real_escape_string($day)."', 
					'".mysql_real_escape_string($month)."', 
					'".mysql_real_escape_string($year)."', 
					'".mysql_real_escape_string($from)."', 
					'".mysql_real_escape_string($until)."'
				)";
	$query = mysql_query($sql) or die("Fatal error: ".mysql_error());

	echo "<h3>Success!</h3>";
	echo "<div class='success_message'>Your event has been added to the calendar.</div>";

}

if(isset($_POST['new_event'])) {

// Get POST vars.
$name 		= stripslashes(trim($_POST['name']));
$desc 		= stripslashes(trim($_POST['desc']));
$location 	= stripslashes(trim($_POST['location']));
$date 		= stripslashes(trim($_POST['date']));
$from 		= stripslashes(trim($_POST['from'])).stripslashes(trim($_POST['from2']));
$until 		= stripslashes(trim($_POST['until'])).stripslashes(trim($_POST['until2']));
	
?>

<h3>Confirm Details</h3>

<p>Are these details correct?</p>

<?php echo "Event ".$name."<br />"; ?>
<?php echo "Description ".$desc."<br />"; ?>
<?php echo "location ".$location."<br />"; ?>
<?php echo "Date ".$date."<br />"; ?>
<?php echo "From ".$from."<br />"; ?>
<?php echo "Until ".$until."<br />"; ?>

<form action="?add=new" method="post">
<input type="hidden" name="name" value="<?php echo base64_encode(serialize($name)); ?>" />
<input type="hidden" name="desc" value="<?php echo base64_encode(serialize($desc)); ?>" />
<input type="hidden" name="location" value="<?php echo base64_encode(serialize($location)); ?>" />
<input type="hidden" name="date" value="<?php echo base64_encode(serialize($date)); ?>" />
<input type="hidden" name="from" value="<?php echo base64_encode(serialize($from)); ?>" />
<input type="hidden" name="until" value="<?php echo base64_encode(serialize($until)); ?>" />

<br /><br />

<input type="button" class="submit" value="No, back" name="back_event" onClick="history.go(-1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="add_event" value="Confirm" />

</form>

<?php
}

include('footer.php');
?>

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 3:39 pm
by s992
To access anything set in your session variables, use $_SESSION.

For example, at the start of check.php you check is "username" is registered. You can access that variable via $_SESSION['username']. I'm still not sure I fully understand what the problem is, so sorry if this doesn't help.

Also, in check.php, you should avoid using session_is_registered() - it is deprecated. Instead, you can use isset() to check for $_SESSION['username'].

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 4:07 pm
by brmcdani44
s992 $_SESSION['username'] is what I was looking for! So I will insert that into mysql insert statement when the form is submitted?

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 4:11 pm
by s992
Exactly!

I need to stop overthinking these questions and give the easy answers first! :lol:

Re: Submitting Session Values

Posted: Fri Nov 19, 2010 4:16 pm
by brmcdani44
LoL well knowing me I tried to over explain it. Thanks for your help it is appreciated!

Re: Submitting Session Values

Posted: Sat Nov 20, 2010 6:10 am
by Neilos
Neilos wrote:

Code: Select all

} else {
	$userData = mysql_fetch_array($query, MYSQL_ASSOC);

	$username_ = $userData['username'];
	$userinfo = $userData['info'];

	$_SESSION['username'] = $username_;
	$_SESSION['userinfo'] = $userinfo;

	header('Location: anotherplace.php')
}
:cry:

lol

Re: Submitting Session Values

Posted: Sun Nov 21, 2010 8:53 pm
by brmcdani44
I am starting with check.php thanks!

Code: Select all

<?php

session_start(); 

if(isset($_SESSION['username'])){
// Session exists, allow the user to view the page. 

} else { 

// Session doesn't exist, redirect to login and exit the page.

header( "Location: login.php" ); 
exit();

} 

?> 

Code: Select all

if(isset($_POST['add_event']) && $_GET['add'] == 'new') {

	// Get POST vars.
	$username   = unserialize(base64_decode(stripslashes($_SESSION['username'])));
	$name 		= unserialize(base64_decode(stripslashes($_POST['name'])));
	$desc 		= unserialize(base64_decode(stripslashes($_POST['desc'])));
	$location 	= unserialize(base64_decode(stripslashes($_POST['location'])));
	$date 		= unserialize(base64_decode(stripslashes($_POST['date'])));
	$from 		= unserialize(base64_decode(stripslashes($_POST['from']))).unserialize(base64_decode(stripslashes($_POST['from2'])));
	$until 		= unserialize(base64_decode(stripslashes($_POST['until']))).unserialize(base64_decode(stripslashes($_POST['until2'])));

	$d_for = explode('/', $date);
	
	$day = $d_for[0];
	$month = $d_for[1];
	$year = $d_for[2];
	
	if(!$name) { echo "<div class='error_message'>You must enter an event name</div>"; exit(); }
	if(!$desc) { echo "<div class='error_message'>Please enter an event description</div>"; exit(); }
	if(!$location) { echo "<div class='error_message'>You must enter a location for your event</div>"; exit(); }
	if(!$date) { echo "<div class='error_message'>Your event must have a date</div>"; exit(); }

	$sql = "INSERT INTO calendar_event (user, event, description, location, day, month, year, time_from, time_until)
				VALUES (
					'".mysql_real_escape_string($username)."',
					'".mysql_real_escape_string($name)."', 
					'".mysql_real_escape_string($desc)."', 
					'".mysql_real_escape_string($location)."', 
					'".mysql_real_escape_string($day)."', 
					'".mysql_real_escape_string($month)."', 
					'".mysql_real_escape_string($year)."', 
					'".mysql_real_escape_string($from)."', 
					'".mysql_real_escape_string($until)."'
				)";
	$query = mysql_query($sql) or die("Fatal error: ".mysql_error());

	echo "<h3>Success!</h3>";
	echo "<div class='success_message'>Your event has been added to the calendar.</div>";

}

Re: Submitting Session Values

Posted: Mon Nov 22, 2010 1:12 am
by brmcdani44
Got it to work! Had to take off the serialize bit for the session piece. Amazing what walking away from your coding will do for a couple of hours and then coming back. Thanks for everyones help it is appreciated!