?
Solved

PHP passing values using session

Posted on 2011-05-06
10
Medium Priority
?
299 Views
Last Modified: 2012-05-11
I am trying to pass some value from a php page either using session or POST GET methods to a second page, but the receiving  page is not pulling the value  , I have very little knowledge on php and dont know what can be the issue, can someone help?
Receiving page
<?php
/*
Note that this line: /F (http://www.mysite.com/CompletionForm.pdf)
determines the PDF File to be filled in. Change to point to the
PDF file on your site. txtName variable is the user's name to populate.
*/
$txtName       = $_GET["txtName"];
$txtCourseName = $_GET['txtCourseName'];
$txtCourseName = $_GET['txtCompletionDate'];

echo $_GET["txtName"]."<br>";
//$txtCourseName = $_GET['sessionId'];
/*
header("Content-type: application/vnd.fdf");
echo <<<EOT
%FDF-1.2
1 0 obj
<<
/FDF
<< 
/Fields
[ 
<<
/T (txtName)
/V ({$txtName})
>> 
<<
/T (txtCourseName)
/V ({$txtCourseName})
>> 
<<
/T (txtCompletionDate)
/V ({$txtCompletionDate})
>> 
]
/F (http://appdevelsvr:8080/shquiz/media/certificate/CourseCertificate.pdf)

>> 
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
EOT;

*/
?>

Open in new window

Sending Page  (default.php)
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?>

<?php
    
    $url = JRoute::_("components/com_jquarks/assets/stylesheets/jquarks_style.css");
    $document =& JFactory::getDocument();
    $document->addHeadLink($url, "stylesheet", "rel");
?>
<style type="text/css">
#errormsg p {
	color: #F00;
}

.certificate {
	
	color:#000;
	font-size:16px;
}
</style>


<div class="componentheading">
	<?php echo JText::_('Quiz Results'); ?>
</div>

<!-- Rick Modified ........ Old line 11 was echo JText::_('SESSION_RESULTS'); ?> -->


<!-- Score and answers status -->
<?php
    $score = $this->session->score;
    $totalScore = $this->totalQuestions - $this->totalInput + $this->inputEvaluated;
    $percent = $score * 100 / $totalScore;
?>

<!-- Rick Modified  Redirect if less than 100 %---><br />
<div class="contentheading">
<p>
     <span class="session-title"><?php echo JText::_('QUIZ').': ' ;?></span>
    <?php echo $this->session->title; ?>
 </p>
 <p>
     <span class="session-title"><?php echo JText::_('SCORE').': ' ;?></span>
    <?php echo  round($percent, 2) . '%&nbsp;&nbsp;&nbsp;'; ?>
     <span class="note-small">
          <?php echo JText::_('INPUT_QUESTIONS_ARE_NOT_RECORDED_IN_THE_SCORE'); ?>
     </span>
 </p>
 <p>
     <span class="session-title"><?php echo JText::_('IS_UNANSWERED').': ' ; ?></span>
    <?php echo $this->session->unanswered; ?>
 </p>
</div>
 
 <div id="certificate" class="certificate">
 <!-- Rick added Line to show 100 percent is required to pass test --> 
  </br>
  <p>
  
  <?php
 
 
if( $percent >=100 )
{ 
$_POST["txtName"]= $this->session->givenname ;
echo $this->session->givenname ;  echo ',' ; echo $this->session->familyname;
?>
        </br>
<?php

echo " Have completed :\n";
echo $this->session->title;
echo '<br>' ;
echo " Completion Date: " ;
echo date("F j, Y, g:i a");
echo '<br>' ;
echo '<br>' ;
echo '<br>' ;
echo " Please print this page and keep it for your records ";

} 

else 
{
    echo "A score of 100 is required to complete this course.
	Please click the link below to re-take the test !!.";
	echo '<br>' ;
	echo '</br>';
    echo '<a href="index.php?option=com_jquarks&view=quiz&id=1&Itemid=2">Re-take test</a>.'; 
	
}
 ?>

  <a href="http://appdevelsvr:8080/shquiz/components/com_jquarks/views/_session/tmpl/certificate.php" title="Sexual Harassment Certificate of Completion" target="_blank">Print Certificate</a>
</p>
</div>
<!-- Answers details -->
<br />

Open in new window

0
Comment
Question by:TonyReba
  • 4
  • 3
  • 3
10 Comments
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 35707496
sessions are pretty easy to use.
you need to put session_start(); before anything is output to the page on any page that you are going to use sessions on.
then all you need to do to set a session variable is $_SESSION['name']=some value;
To retrieve the value all you need to do is $variable=$_SESSION['name'];
Here is a tutorial on sessions http://www.tizag.com/phpT/phpsessions.php
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35707936
I think @haloexpertsexchange summed it up pretty well.  A couple of additional considerations might be in order.

For sessions to work, you almost certainly have to have your browser online and accepting cookies.  Make sure you don't have a goofy setting like "work offline" in place.

If you are using Firefox with Firebug you may have some problems.  I do not know why or whether this has been fixed, but I've seen PHP sessions forget things when the Firebug plug in was active.  This is rare.

The code posted above is incomplete, and Joomla may already be starting its own sessions or have a specialized session handler.  If that is the case, you may find that your session_start() statement collides with Joomla's session.

Whenever you are debugging any piece of code make sure that the first line of every script is error_reporting(E_ALL);

HTH, ~Ray
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35708207

using error_reporting(E_ALL);
on the receiving page I see--->

Notice: Undefined index: givenname in

certificate.php on line 23
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 35708243
can we see that page?
I am not familiar with Joomla but is this $this->session->givenname the way it normally handles sessions?
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 35708278
here is a page where it explains how to use session in Joomla http://www.howtojoomla.net/how-tos/development/how-to-use-sessions-in-joomla.
But from what I can see you still need to start the session before using it with this $session = JFactory::getSession(); and as far as I can see from the code show you are not doing that.
Hopefully this link will help.
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35708317
Do you know where do I include the directives for JFactory?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35708354
Not sure if you know about this, so I'll post a link:
http://docs.joomla.org/

HTH, ~Ray
0
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 2000 total points
ID: 35708621
I think that you already are since it also mentions JFactory in this line  $document =& JFactory::getDocument();
 which is line 7 of the sending page.
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35708663
My last try is this I downloaded Jumy since on previous attemps creating a standar php I couldnt even find the JFactory class, now when I usee

var_dump($_SESSION) ;  i get

array(1) { ["__default"]=> array(8) { ["session.client.browser"]=> string(171) "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" ["session.counter"]=> int(75) ["registry"]=> object(JRegistry)#12 (3) { ["_defaultNameSpace"]=> string(7) "session" ["_registry"]=> array(1) { ["session"]=> array(1) { ["data"]=> object(stdClass)#13 (0) { } } } ["_errors"]=> array(0) { } } ["user"]=> &object(JUser)#14 (19) { ["id"]=> int(0) ["name"]=> NULL ["username"]=> NULL ["email"]=> NULL ["password"]=> NULL ["password_clear"]=> string(0) "" ["usertype"]=> NULL ["block"]=> NULL ["sendEmail"]=> int(0) ["gid"]=> int(0) ["registerDate"]=> NULL ["lastvisitDate"]=> NULL ["activation"]=> NULL ["params"]=> NULL ["aid"]=> int(0) ["guest"]=> int(1) ["_params"]=> object(JParameter)#15 (7) { ["_raw"]=> string(0) "" ["_xml"]=> NULL ["_elements"]=> array(0) { } ["_elementPath"]=> array(1) { [0]=> string(62) "C:\xampp\htdocs\shquiz\libraries\joomla\html\parameter\element" } ["_defaultNameSpace"]=> string(8) "_default" ["_registry"]=> array(1) { ["_default"]=> array(1) { ["data"]=> object(stdClass)#16 (0) { } } } ["_errors"]=> array(0) { } } ["_errorMsg"]=> NULL ["_errors"]=> array(0) { } } ["session.token"]=> string(32) "4e483ed6baed2b199de2e14ce5fb8952" ["session.timer.start"]=> int(1304704568) ["session.timer.last"]=> int(1304708213) ["session.timer.now"]=> int(1304708260) } }

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35708792
That looks very much like a PHP session, and I am almost 100% certain that it is a PHP session.  Be aware that $_SESSION is not immutable before session_start().  This is rare, but I have seen scripts that did not start the session at first, but still put things into the $_SESSION array.  Then later they started the session.  Oops, the earlier data was gone.  I personally think this is a design flaw in PHP, but it is an easy flaw to deal with if you have good coding habits.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question