【问题标题】:How to pass double value from HTML to PHP如何将双精度值从 HTML 传递到 PHP
【发布时间】:2015-08-22 11:36:44
【问题描述】:

我想将 Double 值 (i.e. 10.9) 从 HTML 文件传递​​给 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\Typ...', '价值', '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...', 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->__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 行

【问题讨论】:

  • 您需要将价格翻倍吗?
  • urlencode() 需要一个字符串,而不是一个数值作为输入。也许错误发生在其他地方?
  • stackoverflow.com/questions/3194932/… 将帮助您了解双重类型
  • 是的@KTAnj。我想将一个双精度值从 HTML 传递给 PHP。
  • $price= (double) $_POST['price']; 你试过了吗?

标签: php html ebay-api


【解决方案1】:

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

$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"> 

step 将允许数字的给定精度。

【讨论】:

    【解决方案2】:

    您可以使用floatval()

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

    【讨论】:

      【解决方案3】:

      无论 HTML 页面上的数据类型是什么,您尝试 POST 到 PHP 脚本的任何数据都将作为字符串发布。如果要对数据进行校验,可以使用PHP函数判断数据是否为int、double等。

      要检查该值是否是您可以使用的双精度值,

      if ($price == (string) (float) $price) {
      
          // $price is a float/double
      
          $price = (float) $price; // converts the var to float
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-09
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-05
        相关资源
        最近更新 更多