I was wondering how you take a value given by the user via textbox/form/etc, and propagate it to other links within my webpage.

For example, I'm building a website that retrieves stock quotes. I have a form where the user can type in a stock symbol (i.e. GOOG, MSFT, KKD, etc) and click "search" which will automatically take them to the "Lookup Symbol" page with that value that was entered. From here I want to make it possible so that the value the user entered in stays that value until a new symbol is entered in. The user can click on the "Quotes" link without typing in the same symbol to look up the quotes for that specific stock quote. I know this has to do with GET and POST but am a little confused on how to propagate the value.


Please help!

Recommended Answers

All 4 Replies

It sounds as if you need to a session variable to store variables like the stock symbol. Initially, the user will enter the symbol into your form and you will retrieve it as a $_POST variable. You should then copy it to a session variable:

$_SESSION['stock_sym'] = $_POST['stock_sym'];

and use the session variable from then on. The session variable persists for that user until the session ends (generally when the browser is closed). You have to use a session_start() in any module where you want to access session variable.

It sounds as if you need to a session variable to store variables like the stock symbol. Initially, the user will enter the symbol into your form and you will retrieve it as a $_POST variable. You should then copy it to a session variable:

$_SESSION['stock_sym'] = $_POST['stock_sym'];

and use the session variable from then on. The session variable persists for that user until the session ends (generally when the browser is closed). You have to use a session_start() in any module where you want to access session variable.

Okay I've tried adding session variables but now the user's input doesn't get stored anywhere. Would you mind showing me how I would add this piece into my code?

Here's one of the many pages that will need the session variables:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
<?php
ob_start();

require ('require/dbUtil.inc');
?>

<html>
<head>
	<link rel="stylesheet" type="text/css" href="default.css" />
	<?php
		include('include/header.php');
	?>
</head>
	
<body>
<div id="tableMain">
	<div class="leftpanel">
		<center>
		<h2><b><u>Directory</u></b></h2>
		<a href="default.php"><b>Home</b></a>
	    <br /><br />
		<a href="findsymbol.php"><b>Find Symbol</b></a>
		<br /><br />
		<a href="quote.php"><b>Quote</b></a>
		<br /><br />
		<a href="history.php"><b>History</b></a>
		<br /><br />
		<hr align="center" width="50"></br></center>
	
		<center><b><u>**DISCLAIMER**</u></b></br></br>
	This website is intended for kicks and play--none of the    information you find within this website should be taken    into consideration when investing in stocks.
    	</center>
     </div>
     
<div class="contentbox">
    <center>
    <div class="fillbox">
    <br />     

	<form action="quote.php" method="get">Symbol:
			<input type="text" name="symbol"/>
			<input type="submit" value="Search"/>
	</form>
    <br />
    
<?php	
$strSymbol = @$_REQUEST["symbol"];
if(! empty($strSymbol))
{	
	$host = "cs.spu.edu";
	$user = "quotesdb";
	$pwd = "quotesdb";
	
	$db = @new mysqli($host, $user, $pwd, 'quotesdb');
	
	if($db->connect_errno)
		die("Could not connect to database. Error[{$db->connect_errno}]");
		
	$query = "SELECT symSymbol, symName, symMarketCap FROM symbols " .
			 "WHERE symSymbol='" . $strSymbol . "'" ;
	
	$result = @$db->query($query);
	
	if(!$result)
	{
		print "Invalid";
	}
	else
	{	
		$row = @$result->fetch_assoc();
		extract($row);
	}
	
}
else
{
	$symSymbol = "N/A";
	$symName = "N/A";
	$symMarketCap = "N/A";
}
?>

	<left><h2>Stock Symbol: <b><font color="yellow">
	<?php
	print $symSymbol;		
    ?>
    </font></b></h2></font></left>
						
	
	<table width=670 border=0 cellspacing=0 cellpadding=0 align=center>
		<tr class=tblHead bgcolor="blue">
			<td align=left colspan=2><b><font color="yellow">Company Name:&nbsp;
            <?php
			print $symName;
			?>
			</font></b></td>
		</tr>
	</table>

<?php	
$strSymbol = @$_REQUEST["symbol"];
if(! empty($strSymbol))
{	
	$host = "cs.spu.edu";
	$user = "quotesdb";
	$pwd = "quotesdb";
	
	$db = @new mysqli($host, $user, $pwd, 'quotesdb');
	
	if($db->connect_errno)
		die("Could not connect to database. Error[{$db->connect_errno}]");
		
	$query = "select * from quotes WHERE qSymbol='{$strSymbol}' ORDER BY qQuoteDateTime DESC LIMIT 15";
	
	$result = @$db->query($query);
	
	if(!$result)
	{
		print "Invalid";
	}
	else
	{	
		$row = @$result->fetch_assoc();
		extract($row);
	}
	
}
else
{
	$qSymbol = "N/A";
	$qQuoteDateTime = "N/A";
	$qLastSalePrice = "0.00";
	$qNetChangePrice = "0.00";
	$qNetChangePct = "0";
	$qTodaysHigh = "0.00";
	$qTodaysLow = "0.00";
	$qShareVolumeQty = "N/A";
	$qPreviousClosePrice = "0.00";
	$qBidPrice = "0.00";
	$qAskPrice = "0.00";
	$q52WeekHigh = "0.00";
	$q52WeekLow = "0.00";
	$qCashDividendAmount = "0.00";
	$qEarningsPerShare = "0.00";
	$qCurrentPERatio = "0.00";
	$qCurrentYieldPct = "0.00";
	$qTotalOutstandings = "0";
}
?>

	<table id="stockTable1" width=670 border=1 cellpadding=0 cellspacing=0 rules=rows frame=below
		bordercolor="blue" bordercolordark="white" align=center>
        <tr>
          <td width=10 >&nbsp;</td>
          <td width=100>Last</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qLastSalePrice;
		  ?></font></td>
          <td width=30 >&nbsp;</td>
          <td width=100>Prev Close</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qPreviousClosePrice;
		  ?></font></td>
        </tr>
        <tr>
		  <td width=10 >&nbsp;</td>
		  <td width=100>Change</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qNetChangePrice;
		  ?></font></td>
          <td width=30 >&nbsp;</td>
          <td width=100>Bid</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qBidPrice;
		  ?></font></td>
        </tr>
        <tr>
          <td width=10 >&nbsp;</td>
          <td width=100>%Change</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qNetChangePct;
		  ?></font></td>
          <td width=30 >&nbsp;</td>
          <td width=100>Ask</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qAskPrice;
		  ?></font></td>
        </tr>
        <tr>
          <td width=10 >&nbsp;</td>
          <td width=100>High</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qTodaysHigh;
		  ?></font></td>
          <td width=30 >&nbsp;</td>
          <td width=100>52 Week High</td>
          <td width=80 align=right><font color="black">
          <?php
          print $q52WeekHigh;
		  ?></font></td>
        </tr>
        <tr>
          <td width=10 >&nbsp;</td>
          <td width=100>Low</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qTodaysLow;
		  ?></font></td>
          <td width=30 >&nbsp;</td>
          <td width=100>52 Week Low</td>
          <td width=80 align=right><font color="black">
          <?php
          print $q52WeekLow;
		  ?></font></td>
        </tr>
        <tr>
          <td width=10 >&nbsp;</td>
          <td width=100>Daily Volume</td>
         <td width=80 align=right><font color="black">
         <?php
          print $qShareVolumeQty;
		  ?></font></td>
        </tr>
    </table>

    <br />

    <table width=670 border=0 cellspacing=0 cellpadding=0 align=center>
				<tr class=tblTail bgcolor="blue">
					<td align=left colspan=2><b><font color="yellow">Fundamentals</font></b></td>
				</tr>
	</table>

    <table id="stockTable2" width=670 border=0 cellpadding=0 cellspacing=0 rules=rows frame=below
		bordercolor="blue" bordercolordark="white" align=center>
        <tr>
          <td width=10></td>
          <td width=100>PE Ratio</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qCurrentPERatio;
		  ?></font></td>
          <td width=30>&nbsp;</td>
          <td width=100>Earnings/share</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qEarningsPerShare;
		  ?></font></td>
        </tr>
        <tr>
          <td width=30>&nbsp;</td>
          <td width=100># Shrs Out.</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qTotalOutstandings;
		  ?></font></td>
          <td width=10>&nbsp;</td>
          <td width=100>Div/Share</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qCashDividendAmount;
		  ?></font></td>
        </tr>
        <tr>
          <td width=30 >&nbsp;</td>
          <td width=100>Div. Yield</td>
          <td width=80 align=right><font color="black">
          <?php
          print $qCurrentYieldPct;
		  ?></font></td>
          
		  <?php	
			$strSymbol = @$_REQUEST["symbol"];
			if(! empty($strSymbol))
			{	
				$host = "cs.spu.edu";
				$user = "quotesdb";
				$pwd = "quotesdb";
			
				$db = @new mysqli($host, $user, $pwd, 'quotesdb');
		
				if($db->connect_errno)
					die("Could not connect to database. Error[{$db->connect_errno}]");
		
				$query = "SELECT * FROM symbols WHERE symSymbol='" . $strSymbol . "'" ;
	
				$result = @$db->query($query);
				
				extract($row);
			}
			else
			{
				$symSymbol = "N/A";
				$symName = "N/A";
				$symMarketCap = "N/A";
			}
			?>          
          <td width=10>&nbsp;</td>
          <td width=100>Market Cap.</td>
		  <td width=80 align=right><font color="black">
          <?php
          print $symMarketCap
		  ?></font></td>   
        </tr>
	
	</table>

	</center></div></div>

<?php
include('include/footer.php');
?>

</body>
</br>
</html>

<?php
ob_end_flush();
?>

in quote.php // you are submitting the form in quote.php line 43

save the value to session like chrishea said

$_SESSION['stock_symbol'] = $_POST['symbol'];

It seems you forgot session_start();

You always need to include this on the VERY TOP of your page to use sessions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.