2017-06-09 56 views
0

嗨我無法從我的html form.I需要知道如何從html表單正確獲取數據。其次爲什麼這不起作用?當我點擊「IBM Quote」時,我需要獲得「IBM」的價值。

<!DOCTYPE html> 
<html> 
<head> 
    <title>Stock Quote</title> 

</head> 
<body> 

<form action="soapTest.php" method="post"> 

    <button name="IBM" type="submit">IBM Quote</button> 

</form> 



</body> 
</html> 

PHP代碼

<?php 
$wsdl = "http://www.restfulwebservices.net/wcf/StockQuoteService.svc?wsdl"; 

$client = new SoapClient($wsdl); 
$stock = $_POST["button"]; 
print $stock; 
$parameters= array("request"=>$stock); 
$values = $client->GetStockQuote($parameters); 
$xml = $values->GetStockQuoteResult; 
$currentprice = $xml->Last; 
$volume=$xml->Volume; 
$percentageChange=$xml->PercentageChange; 


if($volume!=null) 
    print "<br />\n Volume: $volume"; 
else 
    print "Volume not set"; 

if($percentageChange!=null) 

    print "<br />\n PercentageChange: $percentageChange"; 
else 
    print "percentageChange not set"; 

?> 

輸出:

Notice: Undefined index: button in /Applications/XAMPP/xamppfiles/htdocs/soapTest.php on line 6 

Fatal error: Uncaught SoapFault exception: [a:InternalServiceFault] Object reference not set to an instance of an object. in /Applications/XAMPP/xamppfiles/htdocs/soapTest.php:9 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/soapTest.php(9): SoapClient->__call('GetStockQuote', Array) #1 /Applications/XAMPP/xamppfiles/htdocs/soapTest.php(9): SoapClient->GetStockQuote(Array) #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/soapTest.php on line 9 
+0

輸入名稱是「IBM」而不是「按鈕」。 –

+0

'$ stock = $ _POST ['IBM'];'會爲你工作。 – Bhavin

回答

1

"IBM"是你的按鈕的名稱,所以你要叫這個名字的崗位價值。

$stock = $_POST['IBM']; 

或者,您可以更改您的html代碼,併爲該按鈕指定名稱按鈕,其值爲IBM。

<button name="button" value="IBM" type="submit">IBM Quote</button> 
+0

第二個正在工作,但第一個給出:注意:未定義的索引:第6行上的/Applications/XAMPP/xamppfiles/htdocs/soapTest.php中的按鈕 致命錯誤:未捕獲的SoapFault異常:[a:InternalServiceFault]未設置對象引用到一個對象的一個​​實例。在/Applications/XAMPP/xamppfiles/htdocs/soapTest.php:9堆棧跟蹤:#0 /Applications/XAMPP/xamppfiles/htdocs/soapTest.php(9):SoapClient - > __ call('GetStockQuote',Array)#1 /應用程序/ XAMPP/xamppfiles/htdocs/soapTest.php(9):SoapClient-> GetStockQuote(Array)#2 {main}引發/Applications/XAMPP/xamppfiles/htdocs/soapTest.php –