I want to pass a Double
value (i.e. 10.9
) from an HTML file to PHP. Here is my HTML code:
<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>
And here is my PHP code:
require __DIR__.'/../vendor/autoload.php';
$config = require __DIR__.'/../configuration.php';
use DTSeBaySDKConstants;
use DTSeBaySDKTradingServices;
use DTSeBaySDKTradingTypes;
use DTSeBaySDKTradingEnums;
$siteId = ConstantsSiteIds::US;
$service = new ServicesTradingService(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 TypesAddFixedPriceItemRequestType();
$request->RequesterCredentials = new TypesCustomSecurityHeaderType();
$request->RequesterCredentials->eBayAuthToken = $config['sandbox']['userToken'];
$item = new TypesItemType();
$item->Title = urlencode($_POST['title']);
$item->Description = urlencode($_POST['description']);
$item->StartPrice = new TypesAmountType(array('value' => urlencode($_POST['price'])));
$request->Item = $item;
$response = $service->addFixedPriceItem($request);
if (isset($response->Errors)) {
foreach ($response->Errors as $error) {
printf("%s: %sn%snn",
$error->SeverityCode === EnumsSeverityCodeType::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 %sn",
$response->ItemID
);
}
Fatal error: Uncaught exception ‘DTSeBaySDKExceptionsInvalidPropertyTypeException’ with message
‘Invalid property type: DTSeBaySDKTradingTypesAmountType::value
expected , got ‘ 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): DTSeBaySDKTypesBaseType::ensurePropertyType(‘DTSeBaySDKTyp…’,
‘value’, ‘16.99’) #1
/var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php(231): DTSeBaySDKTypesBaseType->set(‘DTSeBaySDKTyp…’, ‘value’,
‘16.99’) #2
/var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/DoubleType.php(51):
DTSeBaySDKTypesBaseType->setValues(‘DTSeBaySDKTyp…’, Array) #3
/var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk-trading/src/DTS/eBaySDK/Trading/Types/AmountType.php(49):
DTSeBaySDKTypesDoubleType->__construct(Array) #4 /var/www/h in
/var/www/html/gitsamp/ebay-sdk-examples/vendor/dts/ebay-sdk/src/DTS/eBaySDK/Types/BaseType.php
on line 433
3
Answers
PHP Answer:
The value you’re dealing with in
$price
is a string, change it to a double in the PHP code withfloatval()
:More here: http://php.net/manual/en/function.floatval.php
Also, side note: floating point numbers are dangerous with money transactions. You may want to use integers and break the values into dollars and cents to avoid losing accuracy.
HTML Answer:
If you’re not for PHP code translation, then you can set the accuracy in HTML with:
step will allow the given accuracy of a number.
You can use
floatval()
Whatever data you are trying to POST to the PHP script will be posted as a string, regardless of what the data type is on the HTML page. If you want to validate the data, you can use a PHP function to determine whether the data is int, double, etc.
To check if the value is a double you could use,