首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将双值从HTML传递给PHP

如何将双值从HTML传递给PHP
EN

Stack Overflow用户
提问于 2015-06-08 05:26:07
回答 3查看 3K关注 0票数 0

我想将一个Double值(i.e. 10.9)从一个HTML传递给PHP。以下是我的HTML代码:

代码语言:javascript
复制
        <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代码:

代码语言:javascript
复制
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行。

EN

回答 3

Stack Overflow用户

发布于 2015-06-08 05:32:24

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

代码语言:javascript
复制
$price = '';
$price = urlencode($_POST['price']);
$price_float_value = floatval($price);
echo $price_float_value;

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

此外,附带说明:浮点数对货币交易是危险的。您可能需要使用整数并将值分解为美元和美分,以避免丢失准确性。

HTML答案:如果您不支持代码翻译,那么您可以用以下方法来设置HTML的准确性:

代码语言:javascript
复制
<input type="number" step="0.01"> 

step将允许给定的一个数字的准确性。

票数 1
EN

Stack Overflow用户

发布于 2015-06-08 05:34:02

您可以使用floatval()

代码语言:javascript
复制
$price = '';
$price = urlencode($_POST['price']);
$price_float_value = floatval($price);
echo $price_float_value;
票数 1
EN

Stack Overflow用户

发布于 2015-06-08 05:35:25

无论您试图将任何数据发布到PHP脚本,都将以字符串的形式发布,而不管HTML页面上的数据类型如何。如果要验证数据,可以使用PHP函数来确定数据是否为int、double等。

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

代码语言:javascript
复制
if ($price == (string) (float) $price) {

    // $price is a float/double

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

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30701764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档