Click to See Complete Forum and Search --> : variable values


jrthor2
03-03-2004, 09:02 AM
I have a page that is making a SOAP call to a webservice to get stock prices. I have a field where you can enter the company symbol, but when I submit the page, my variable for $symbol is blank. My input field is named symbol. Below is my code. This is on a Windows box with PHP 4.3.3.

<?
$symbol = "";
require_once('inc/nusoap.php');

$soapclient = new soapclient('http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl',1);
echo "Symbol " . $symbol;

if (!$symbol) {
$rite_aid = $soapclient->call('getQuote',array('symbol'=>'rad'));
$ibm = $soapclient->call('getQuote',array('symbol'=>'ibm'));
$cvs = $soapclient->call('getQuote',array('symbol'=>'cvs'));
$wag = $soapclient->call('getQuote',array('symbol'=>'wag'));
$hershey = $soapclient->call('getQuote',array('symbol'=>'hsy'));
} else {
$results = $soapclient->call('getQuote',array('symbol'=>'$symbol'));
}
?>
<html>
<head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="5">
<tr>
<td colspan="2" nowrap><strong>Stock Prices using PHP NUSoap Webservice </strong></td>
</tr>
<? if (!$symbol) {?>
<tr>
<td width="5%" nowrap>Rite Aid</td>
<td width="95%"><?=$rite_aid?></td>
</tr>
<tr>
<td nowrap>Walgreens</td>
<td><?=$wag?></td>
</tr>

<tr>
<td nowrap>CVS</td>
<td><?=$cvs?></td>
</tr>

<tr>
<td nowrap>IBM</td>
<td><?=$ibm?></td>
</tr>
<tr>
<td nowrap>Hershey Foods</td>
<td><?=$hershey?></td>
</tr>
<? } else {?>
<tr>
<td>
<?=$symbol?>
</td>
<td>
<?=$results?>
</td>
</tr>
<? } ?>
<tr valign="top">
<td nowrap>Enter symbol: </td>
<td>
<form name="stocks" method="get" action="webservice_stocks.php">
<input name="symbol" type="text" id="symbol">
<input type="submit" name="Submit" value="Get Stock Price">
</form> </td>
</tr>

</table>
</body>
</head>
</html>

Jona
03-03-2004, 09:29 AM
Originally posted by jrthor2
} else {
$results = $soapclient->call('getQuote',array('symbol'=>'$symbol'));
}


Why is $symbol in quotes? That would cause PHP to treat it as a string, not a variable. Take those single-quotes out, and see what happens. ;)

[J]ona

jrthor2
03-03-2004, 09:32 AM
nope, that didn't help. I have a print statement at the top of the page to print out $symbol and the values isn't even printing out there. You're change wouldn't have fixed that I don't believe.

Jona
03-03-2004, 09:35 AM
You're setting $symbol to equal an emptry string...


$symbol = "";


...At the beginning of your code.

[J]ona

jrthor2
03-03-2004, 09:39 AM
that's because I was trying to declare the variable. If I take that line out, I get

Undefined variable: symbol in D:\html\users\zluthorg\html\webservice_stocks.php on line 6


Even if i put global $symbol at the top, my $symbol variable is still blank.

Jona
03-03-2004, 09:44 AM
Try setting $symbol to a default ticker symbol such as DIA...

[J]ona

jrthor2
03-03-2004, 09:48 AM
If I set that at the top of the page, then it comes back with jsut the DIA results, which is what it should do. But, if I enter anything into my input box, nothing comes back. I even used form method "get" to make sure the symbol value was getting passed, and it is.

Jona
03-03-2004, 09:55 AM
Try this...


<?
if(isset($_POST["symbol"])){
$symbol = $_POST["symbol"];
}
require_once('inc/nusoap.php');

$soapclient = new soapclient('http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl',1);
echo "Symbol " . $symbol;

if (!isset($symbol)) {
$rite_aid = $soapclient->call('getQuote',array('symbol'=>'rad'));
$ibm = $soapclient->call('getQuote',array('symbol'=>'ibm'));
$cvs = $soapclient->call('getQuote',array('symbol'=>'cvs'));
$wag = $soapclient->call('getQuote',array('symbol'=>'wag'));
$hershey = $soapclient->call('getQuote',array('symbol'=>'hsy'));
} else {
$results = $soapclient->call('getQuote',array('symbol'=>'$symbol'));
}
?>


[J]ona

jrthor2
03-03-2004, 09:59 AM
Nope, there is still nothing in $symbol when printing it out and I get all the results, not just the one I enter.

Jona
03-03-2004, 10:01 AM
Did you change your form method to post?

[J]ona

jrthor2
03-03-2004, 10:02 AM
yes, I got it, thanks!!

Jona
03-03-2004, 10:06 AM
Originally posted by jrthor2
yes, I got it, thanks!!

I'll assume you meant that you got everything to work, not that you just changed the form's method and are excited about it. :p

[J]ona

jrthor2
03-03-2004, 10:08 AM
:D , It is working, thanks!

Jona
03-03-2004, 10:13 AM
Oh, in that case, you're welcome!

[J]ona