我想将一个Double值(i.e. 10.9)从一个HTML传递给PHP。以下是我的HTML代码:
<form action="AddProduct.php" method="POST" target="_self">
<table>
<tr>
<label class="lbl"> Title: </label>  
<input class="form-control" type="text" name="title" maxlength="100" size=20>
<label class="lbl"> Price: </label>    
<input class="form-control" type="text" name="price" maxlength=100>
</tr>
<br>
<tr>
<label class="lbl"> Description: </label>   <br>
<textarea rows="10" cols="37" name="description"></textarea>
</tr>
<br>
<tr>
<input class="btn" type="Submit" name="save" value="Save">
</tr>
</table>
</form>下面是我的PHP代码:
require __DIR__.'/../vendor/autoload.php';
$config = require __DIR__.'/../configuration.php';
use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;
$siteId = Constants\SiteIds::US;
$service = new Services\TradingService(array(
'apiVersion' => $config['tradingApiVersion'],
'sandbox' => true,
'siteId' => $siteId,
'authToken' => $config['sandbox']['userToken'],
'devId' => $config['sandbox']['devId'],
'appId' => $config['sandbox']['appId'],
'certId' => $config['sandbox']['certId'],
));
$request = new Types\AddFixedPriceItemRequestType();
$request->RequesterCredentials = new Types\CustomSecurityHeaderType();
$request->RequesterCredentials->eBayAuthToken = $config['sandbox']['userToken'];
$item = new Types\ItemType();
$item->Title = urlencode($_POST['title']);
$item->Description = urlencode($_POST['description']);
$item->StartPrice = new Types\AmountType(array('value' => urlencode($_POST['price'])));
$request->Item = $item;
$response = $service->addFixedPriceItem($request);
if (isset($response->Errors)) {
foreach ($response->Errors as $error) {
printf("%s: %s\n%s\n\n",
$error->SeverityCode === Enums\SeverityCodeType::C_ERROR ? 'Error' : 'Warning',
$error->ShortMessage,
$error->LongMessage
);
}
}
if ($response->Ack !== 'Failure') {
printf("The item was listed to the eBay Sandbox with the Item number %s\n",
$response->ItemID
);
}致命错误:带有消息‘无效属性类型:DTS\eBaySDK\Trading\Types\AmountType::预期值的未命名异常“in /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php:433 Stack trace:#0 /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php(263):DTS\eBaySDK\Types\BaseType::ensurePropertyType('DTS\eBaySDK\Typ...',‘value”,'16.99') #1 /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php(231):DTS\eBaySDK\Types\BaseType->set('DTS\eBaySDK\Typ...',‘值’,(“16.99”) #2 /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/DoubleType.php(51):DTS\eBaySDK\Types\BaseType->setValues('DTS\eBaySDK\Typ...',数组) #3 /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk-trading/src/DTS/eBaySDK/Trading/Types/AmountType.php(49):DTS\eBaySDK\Types\DoubleType->__construct(Array) #4 /var/www/h在/var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php中,位于第433行。
发布于 2015-06-08 05:32:24
PHP答案:在$price中处理的值是一个字符串,在floatval()代码中将其更改为
$price = '';
$price = urlencode($_POST['price']);
$price_float_value = floatval($price);
echo $price_float_value;这里有更多信息:http://php.net/manual/en/function.floatval.php
此外,附带说明:浮点数对货币交易是危险的。您可能需要使用整数并将值分解为美元和美分,以避免丢失准确性。
HTML答案:如果您不支持代码翻译,那么您可以用以下方法来设置HTML的准确性:
<input type="number" step="0.01"> step将允许给定的一个数字的准确性。
发布于 2015-06-08 05:34:02
您可以使用floatval()
$price = '';
$price = urlencode($_POST['price']);
$price_float_value = floatval($price);
echo $price_float_value;发布于 2015-06-08 05:35:25
无论您试图将任何数据发布到PHP脚本,都将以字符串的形式发布,而不管HTML页面上的数据类型如何。如果要验证数据,可以使用PHP函数来确定数据是否为int、double等。
若要检查该值是否为可以使用的双倍,
if ($price == (string) (float) $price) {
// $price is a float/double
$price = (float) $price; // converts the var to float
}https://stackoverflow.com/questions/30701764
复制相似问题