Link to home
Start Free TrialLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

asked on

PHP passing values using session

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

Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

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
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
Avatar of TonyReba

ASKER


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

Notice: Undefined index: givenname in

certificate.php on line 23
can we see that page?
I am not familiar with Joomla but is this $this->session->givenname the way it normally handles sessions?
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.
Do you know where do I include the directives for JFactory?
Not sure if you know about this, so I'll post a link:
http://docs.joomla.org/

HTH, ~Ray
ASKER CERTIFIED SOLUTION
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

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
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

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.