如何将HTML中的double值传递给PHP

时间:2015-06-08 05:26:08

标签: php html ebay-api

我想将HTML文件中的 Double 值(i.e. 10.9)传递给PHP。这是我的HTML代码:

        <form action="AddProduct.php" method="POST" target="_self">
            <table>
                <tr>
                    <label class="lbl"> Title: </label> &nbsp
                    <input class="form-control" type="text" name="title" maxlength="100" size=20>
                    <label class="lbl"> Price: </label> &nbsp &nbsp
                    <input class="form-control" type="text" name="price" maxlength=100>
                </tr>
                <br>
                <tr>
                    <label class="lbl"> Description: </label> &nbsp <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 \ Exceptions \ InvalidPropertyTypeException'   '无效的属性类型:DTS \ eBaySDK \ Trading \ Types \ AmountType :: value   期待,得到了   /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php:433   堆栈跟踪:#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 \典型......“,   '价值','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-&gt; set( 'DTS \ eBaySDK \ Typ ...','value',   '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-&gt; setValues('DTS \ eBaySDK \ Typ ...',Array)#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-&gt; __ construct(Array)#4 / var / www / h in   /var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php   在第433行

3 个答案:

答案 0 :(得分:1)

PHP答案: 您在$price中处理的值是一个字符串,使用floatval()将其更改为PHP代码中的double:

$price = '';
$price = urlencode($_POST['price']);
$price_float_value = floatval($price);
echo $price_float_value;

更多信息:http://php.net/manual/en/function.floatval.php

此外,旁注:浮点数对货币交易是危险的。您可能希望使用整数并将值分解为美元和美分,以避免失去准确性。

HTML答案: 如果您不是用于PHP代码转换,那么您可以使用以下命令设置HTML的准确性:

<input type="number" step="0.01"> 

步骤将允许给定数字的准确性。

答案 1 :(得分:0)

您可以使用floatval()

$price = '';
$price = urlencode($_POST['price']);
$price_float_value = floatval($price);
echo $price_float_value;

答案 2 :(得分:0)

无论HTML页面上的数据类型是什么,无论您尝试POST到PHP脚本的数据都将作为字符串发布。如果要验证数据,可以使用PHP函数来确定数据是int,double等。

要检查该值是否为您可以使用的双倍,

if ($price == (string) (float) $price) {

    // $price is a float/double

    $price = (float) $price; // converts the var to float

}