skip to Main Content

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> &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>

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


  1. PHP Answer:
    The value you’re dealing with in $price is a string, change it to a double in the PHP code with floatval():

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

    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:

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

    step will allow the given accuracy of a number.

    Login or Signup to reply.
  2. You can use floatval()

    $price = '';
    $price = urlencode($_POST['price']);
    $price_float_value = floatval($price);
    echo $price_float_value;
    
    Login or Signup to reply.
  3. 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,

    if ($price == (string) (float) $price) {
    
        // $price is a float/double
    
        $price = (float) $price; // converts the var to float
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search