Non numeric value

Hi Guys.

I have a form with 5 input fields

if i fill out all the inputs it works ok

however if i fill out only say 3 inputs i then get a php warning A non-numeric value encountered on the inputs i have not filled out ?

my php version is 7.3

My code

Blockquote

if(isset($_POST["date"])){
    $date =($_POST["date"]);
}
if(isset($_POST["totalvalue"])){
    $totalvalue =($_POST["totalvalue"]);
}
if(isset($_POST["code1"])){
    $code1 =($_POST["code1"]);

}
if(isset($_POST["name1"])){
    $name1 =($_POST["name1"]);
}
if(isset($_POST["price1"])){
    $price1 =($_POST["price1"]);
}
if(isset($_POST["amount"])){
    $amount =($_POST["amount"]);
}
if(isset($_POST["quantity1"])){
    $quantity1 =($_POST["quantity1"]);
}
if(isset($_POST["total"])){
    $total =($_POST["total"]);
}
if(isset($_POST["contact1"])){$contact1 =($_POST["contact1"]);}
if(isset($_POST["contact2"])){$contact2 =($_POST["contact2"]);}
if(isset($_POST["contact3"])){$contact3 =($_POST["contact3"]);}
if(isset($_POST["contact4"])){$contact4 =($_POST["contact4"]);}
if(isset($_POST["contact5"])){$contact5 =($_POST["contact5"]);}
if(isset($_POST["depot"])){$depot =($_POST["depot"]);}
if(isset($_POST["name"])){$name =($_POST["name"]);}

if(isset($_POST["contact"])){$contact =($_POST["contact"]);}
if(isset($_POST["email"])){$email =($_POST["email"]);}
if(isset($_POST["vatband1"])){$vatband1 =($_POST["vatband1"]);}


if(isset($_POST["code2"])){$code2 =($_POST["code2"]);}
if(isset($_POST["name2"])){$name2 =($_POST["name2"]);}
if(isset($_POST["price2"])){$price2 =($_POST["price2"]);}
if(isset($_POST["quantity2"])){$quantity2 =($_POST["quantity2"]);}
if(isset($_POST["vatband2"])){$vatband2 =($_POST["vatband2"]);}

if(isset($_POST["code3"])){$code3 =($_POST["code3"]);}
if(isset($_POST["name3"])){$name3 =($_POST["name3"]);}
if(isset($_POST["price3"])){$price3 =($_POST["price3"]);}
if(isset($_POST["quantity3"])){$quantity3 =($_POST["quantity3"]);}
if(isset($_POST["vatband3"])){$vatband3 =($_POST["vatband3"]);}

if(isset($_POST["code4"])){$code4 =($_POST["code4"]);}
if(isset($_POST["name4"])){$name4 =($_POST["name4"]);}
if(isset($_POST["price4"])){$price4 =($_POST["price4"]);}
if(isset($_POST["quantity4"])){$quantity4 =($_POST["quantity4"]);}
if(isset($_POST["vatband4"])){$vatband4 =($_POST["vatband4"]);}

if(isset($_POST["code5"])){$code5 =($_POST["code5"]);}
if(isset($_POST["name5"])){$name5 =($_POST["name5"]);}
if(isset($_POST["price5"])){$price5 =($_POST["price5"]);}
if(isset($_POST["quantity5"])){$quantity5 =($_POST["quantity5"]);}
if(isset($_POST["vatband5"])){$vatband5 =($_POST["vatband5"]);}



if(isset($_POST["sum1"])){$sum1 =($_POST["sum1"]);}
if(isset($_POST["sum2"])){$sum2 =($_POST["sum2"]);}
if(isset($_POST["sum3"])){$sum3 =($_POST["sum3"]);}
if(isset($_POST["sum4"])){$sum4 =($_POST["sum4"]);}
if(isset($_POST["sum5"])){$sum5 =($_POST["sum5"]);}






$sum1=$quantity1*$price1;
$sum2=$quantity2*$price2;
$sum3=$quantity3*$price3;
$sum4=$quantity4*$price4;
$sum5=$quantity5*$price5;






$total=$sum1+$sum2+$sum3+$sum4+$sum5;




if($sum1!=0){
$sum1 = number_format($sum1, 2, '.', '');
}
else{
    $sum1 = "";
}
if($sum2!=0){
$sum2 = number_format($sum2, 2, '.', '');
}
else{
    $sum2 = "";
}
if($sum3!=0){
$sum3 = number_format($sum3, 2, '.', '');
}
else{
    $sum3 = "";
}
if($sum4!=0){
$sum4 = number_format($sum4, 2, '.', '');
}
else{
    $sum4 = "";
}
if($sum5!=0){
$sum5 = number_format($sum5, 2, '.', '');
}
else{
    $sum5 = "";
}


$net = number_format($net, 2, '.', '');
$vat = number_format($vat, 2, '.', '');
$total = number_format($total, 2, '.', '');

any advice thanks !

Empty form fields submit an empty string, which is not a number, so any use of an empty string in a math expression produces an error.

What is the context/usage of this form data, i.e. what happens if there is less than 5 sets of values? Should the empty fields result in zeros or should the empty fields be ignored and not used at all? The answer to this question determines what the best solution would be, combined with using arrays for the sets of five form fields.

Also, why are there ‘sumx’ fields from the form, but you calculate the sum in the code from the quantityx and pricex fields and overwrite the $sumx variables? This makes no sense. You should also not accept prices, totals, sums,… from user submitted data, as that will let a user buy things cheap by setting the price to .01

This code is filled with unnecessary statements and unnecessary syntax. When a form is submitted, except for unchecked check-boxed/radio-buttons, all form fields will be set. All the isset() statements for always-set fields are unnecessary. Copying variables to other variables is also a waste of time and putting () around variables just forces php to reevaluate the variable that it is already evaluating.

You need to dynamically process form data, rather than to spend your time writing out the same logic over and over for each different form field. Operate on the data as a set using php array functions.

You have 99% more code than you need. Stop creating variables for nothing. You already have the POST variables, just use them.

(Disclaimer: I wrote code exactly like this at one time way back in the day :grimacing:)

1 Like

I’ll predicate my response on, what are you trying to do? As pointed out, most of your code can be removed. Based on the code, I am guessing you could do this far better with a little help as well.

html can use arrays rather than the number format you are doing with price4, the same can be done like this,

<input name='price[]' type='number'>

the fourth would be accessed like this,

$_POST['price'][3]

Hi if only 2 inputs used then only 2 displayed
If 3 used only 3 displayed etc

While that doesn’t directly answer the question asked, I’ll go with you want to ignore, not use, the empty fields.

After you change the form fields to be arrays, use array_filter() in the form processing code to remove the empty entries from the submitted arrays.

Sponsor our Newsletter | Privacy Policy | Terms of Service