Non numeric value causing php error

Hi Guys i am getting a php warning non numerical value entered on these lines ?

Blockquote

$sum1=$quantity1*$price1;

$sum2=$quantity2*$price2;

$sum3=$quantity3*$price3;

$sum4=$quantity4*$price4;

$sum5=$quantity5*$price5;

Blockquote

You aren’t giving us enough clues as to what you are doing.

Where does $sum 1, $quantity1 and %price1 come from?

My guess would be that you are trying to multiply a number by a string. But it might be that $sum1 has previously been used as a string.

Are these variables coming from a form? I need more information.

Here is the form code

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Test</title>

<meta name="generator" content="WYSIWYG Web Builder 15 - http://www.wysiwygwebbuilder.com">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="default/jquery.mobile.theme-1.4.5.css" rel="stylesheet">

<link href="default/jquery.mobile.icons-1.4.5.min.css" rel="stylesheet">

<link href="jquery.mobile.structure-1.4.5.min.css" rel="stylesheet">

<link href="toolstation_mobile.css" rel="stylesheet">

<link href="page1.css" rel="stylesheet">

<script src="jquery-1.12.4.min.js"></script>

<script>

$(document).on("mobileinit", function()

{

   $.mobile.ajaxEnabled = false;

});

</script>

<script src="jquery.mobile-1.4.5.min.js"></script>

</head>

<body>

<div data-role="page" data-theme="a" data-title="Toolstation" id="page1">

<div class="ui-content" role="main">

<img src="images/1280px-Toolstation_logo.svg.png" id="Image1" alt="">

<div id="wb_Form1" style="">

<form name="Form1" method="post" action="invoice.php" enctype="multipart/form-data" data-ajax="false" data-transition="pop" id="Form1" style="display:inline;">

<div id="wb_Text2">



</div>

<div id="wb_Text3">

<span style="color:#000000;font-family:Arial;font-size:13px;">What Date Do You Want On Your Receipt.</span>

</div>

<label for="Editbox2"></label>

<input type="date" id="Editbox2" style="" name="date" value="" spellcheck="false" placeholder="Date">

<hr id="Line1" style="">

<div id="wb_Text8">

<span style="color:#000000;font-family:Arial;font-size:13px;">Your Address Or Business Address.</span>

</div>

<label for="Editbox7"></label>

<input type="text" id="Editbox7" style="" name="contact1" value="" spellcheck="false" placeholder="Address 1">

<label for="Editbox8"></label>

<input type="text" id="Editbox8" style="" name="contact2" value="" spellcheck="false" placeholder="Address 2">

<label for="Editbox9"></label>

<input type="text" id="Editbox9" style="" name="contact3" value="" spellcheck="false" placeholder="Address 3">

<label for="Editbox10"></label>

<input type="text" id="Editbox10" style="" name="contact4" value="" spellcheck="false" placeholder="Address 4">

<label for="Editbox11"></label>

<input type="text" id="Editbox11" style="" name="contact5" value="" spellcheck="false" placeholder="Address 5">

<div id="wb_Text9">

<span style="color:#000000;font-family:Arial;font-size:13px;">Depot Branch (e.g. Rotherham)</span>

</div>

<label for="Editbox12"></label>

<input type="text" id="Editbox12" style="" name="depot" value="" spellcheck="false">

<div id="wb_Text10">

<span style="color:#000000;font-family:Arial;font-size:13px;">Your Name Or Company Name</span>

</div>

<label for="Editbox13"></label>

<input type="text" id="Editbox13" style="" name="company" value="" spellcheck="false">

<div id="wb_Text1">

<span style="color:#000000;font-family:Arial;font-size:13px;">Title (e.g. Mr / Mrs / Miss)</span>

</div>

<label for="Editbox14"></label>

<input type="text" id="Editbox14" style="" name="title" value="" spellcheck="false">

<div id="wb_Text12">

<span style="color:#000000;font-family:Arial;font-size:13px;">First Name</span>

</div>

<label for="Editbox15"></label>

<input type="text" id="Editbox15" style="" name="firstname" value="" spellcheck="false">

<div id="wb_Text13">

<span style="color:#000000;font-family:Arial;font-size:13px;">Last Name</span>

</div>

<label for="Editbox16"></label>

<input type="text" id="Editbox16" style="" name="lastname" value="" spellcheck="false">

<div id="wb_Text14">

<span style="color:#000000;font-family:Arial;font-size:13px;">Contact Number</span>

</div>

<label for="Editbox17"></label>

<input type="text" id="Editbox17" style="" name="mobile" value="" spellcheck="false">

<div id="wb_Text11">

<span style="color:#000000;font-family:Arial;font-size:13px;">E Mail Address</span>

</div>

<label for="Editbox18"></label>

<input type="text" id="Editbox18" style="" name="email" value="" spellcheck="false">

<div id="wb_Text40">

<span style="color:#000000;font-family:Arial;font-size:16px;">Enter The 5 Digit Reference Code <br><br>Enter Quantity ( How Many You Want )<br><br>Enter A Description Of The Item<br>Enter The Price Of The Item<br><br>Example</span>

</div>

<div id="wb_Text6">

<span style="color:#000000;font-family:Arial;font-size:13px;">Code</span>

</div>

<label for="Editbox1"></label>

<input type="text" id="Editbox1" style="" name="sample1" value="62770" readonly spellcheck="false">

<div id="wb_Text22">

<span style="color:#000000;font-family:Arial;font-size:13px;">Quantity</span>

</div>

<label for="Editbox23"></label>

<input type="text" id="Editbox23" style="" name="sample2" value="1" readonly spellcheck="false">

<div id="wb_Text28">

<span style="color:#000000;font-family:Arial;font-size:13px;">Name</span>

</div>

<label for="Editbox24"></label>

<input type="text" id="Editbox24" style="" name="sample3" value="Fibreglass Claw Hammer 16oz" readonly spellcheck="false">

<div id="wb_Text20">

<span style="color:#000000;font-family:Arial;font-size:13px;">Price (excluding vat e.g 2.25)</span>

</div>

<label for="Editbox29"></label>

<input type="text" id="Editbox29" style="" name="sample4" value="4.19" readonly spellcheck="false">

<hr id="Line2" style="">

<div id="wb_Text4">

<span style="color:#000000;font-family:Arial;font-size:13px;">Item 1 Code</span>

</div>

<label for="Editbox3"></label>

<input type="text" id="Editbox3" style="" name="code1" value="" spellcheck="false">

<div id="wb_Text5">

<span style="color:#000000;font-family:Arial;font-size:13px;">Quantity</span>

</div>

<label for="Editbox6"></label>

<input type="number" id="Editbox6" style="" name="quantity1" value="" spellcheck="false">

<div id="wb_Text7">

<span style="color:#000000;font-family:Arial;font-size:13px;">Name</span>

</div>

<label for="Editbox4"></label>

<input type="text" id="Editbox4" style="" name="name1" value="" spellcheck="false">

<div id="wb_Text27">

<span style="color:#000000;font-family:Arial;font-size:13px;">Price (excluding vat e.g 2.25)</span>

</div>

<label for="Editbox19"></label>

<input type="text" id="Editbox19" style="" name="price1" value="" spellcheck="false">

<hr id="Line3" style="">

<div id="wb_Text17">

<span style="color:#000000;font-family:Arial;font-size:13px;">Item 2 Code</span>

</div>

<label for="Editbox20"></label>

<input type="text" id="Editbox20" style="" name="code2" value="" spellcheck="false">

<div id="wb_Text18">

<span style="color:#000000;font-family:Arial;font-size:13px;">Quantity</span>

</div>

<label for="Editbox21"></label>

<input type="number" id="Editbox21" style="" name="quantity2" value="" spellcheck="false">

<div id="wb_Text19">

<span style="color:#000000;font-family:Arial;font-size:13px;">Name</span>

</div>

<label for="Editbox22"></label>

<input type="text" id="Editbox22" style="" name="name2" value="" spellcheck="false">

<div id="wb_Text24">

<span style="color:#000000;font-family:Arial;font-size:13px;">Price (excluding vat e.g 2.25)</span>

</div>

<label for="Editbox26"></label>

<input type="text" id="Editbox26" style="" name="price2" value="" spellcheck="false">

<hr id="Line4" style="">

<div id="wb_Text29">

<span style="color:#000000;font-family:Arial;font-size:13px;">Item 3 Code</span>

</div>

<label for="Editbox27"></label>

<input type="text" id="Editbox27" style="" name="code3" value="" spellcheck="false">

<div id="wb_Text30">

<span style="color:#000000;font-family:Arial;font-size:13px;">Quantity</span>

</div>

<label for="Editbox28"></label>

<input type="number" id="Editbox28" style="" name="quantity3" value="" spellcheck="false">

<div id="wb_Text31">

<span style="color:#000000;font-family:Arial;font-size:13px;">Name</span>

</div>

<label for="Editbox30"></label>

<input type="text" id="Editbox30" style="" name="name3" value="" spellcheck="false">

<div id="wb_Text39">

<span style="color:#000000;font-family:Arial;font-size:13px;">Price (excluding vat e.g 2.25)</span>

</div>

<label for="Editbox31"></label>

<input type="text" id="Editbox31" style="" name="price3" value="" spellcheck="false">

<hr id="Line5" style="">

<div id="wb_Text35">

<span style="color:#000000;font-family:Arial;font-size:13px;">Item 4 Code</span>

</div>

<label for="Editbox32"></label>

<input type="text" id="Editbox32" style="" name="code4" value="" spellcheck="false">

<div id="wb_Text36">

<span style="color:#000000;font-family:Arial;font-size:13px;">Quantity</span>

</div>

<label for="Editbox33"></label>

<input type="number" id="Editbox33" style="" name="quantity4" value="" spellcheck="false">

<div id="wb_Text37">

<span style="color:#000000;font-family:Arial;font-size:13px;">Name</span>

</div>

<label for="Editbox34"></label>

<input type="text" id="Editbox34" style="" name="name4" value="" spellcheck="false">

<div id="wb_Text44">

<span style="color:#000000;font-family:Arial;font-size:13px;">Price (excluding vat e.g 2.25)</span>

</div>

<label for="Editbox35"></label>

<input type="text" id="Editbox35" style="" name="price4" value="" spellcheck="false">

<hr id="Line6" style="">

<div id="wb_Text41">

<span style="color:#000000;font-family:Arial;font-size:13px;">Item 5 Code</span>

</div>

<label for="Editbox36"></label>

<input type="text" id="Editbox36" style="" name="code5" value="" spellcheck="false">

<div id="wb_Text46">

<span style="color:#000000;font-family:Arial;font-size:13px;">Quantity</span>

</div>

<label for="Editbox37"></label>

<input type="text" id="Editbox37" style="" name="quantity5" value="" spellcheck="false">

<div id="wb_Text47">

<span style="color:#000000;font-family:Arial;font-size:13px;">Name</span>

</div>

<label for="Editbox38"></label>

<input type="text" id="Editbox38" style="" name="name5" value="" spellcheck="false">

<div id="wb_Text49">

<span style="color:#000000;font-family:Arial;font-size:13px;">Price (excluding vat e.g 2.25)</span>

</div>

<label for="Editbox39"></label>

<input type="text" id="Editbox39" style="" name="price5" value="" spellcheck="false">

<hr id="Line7" style="">

<div id="wb_Text51">

<span style="color:#000000;font-family:Arial;font-size:16px;">Check All Your Details Then Select Submit. <br><br>This Will Generate Your Receipt and Give You The Option To Print Or Save As PDF.<br><br></span>

</div>

<input type="submit" id="Button1" name="" value="Submit">

</form>

</div>

</div>

</div>

</body>

</html>

Here is the form action code …

<?php
if(isset($_POST["orderno"])){
    $orderno =($_POST["orderno"]);
    
}
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["company"])){$company =($_POST["company"]);}
if(isset($_POST["title"])){$title =($_POST["title"]);}
if(isset($_POST["firstname"])){$firstname =($_POST["firstname"]);}
if(isset($_POST["lastname"])){$lastname =($_POST["lastname"]);}
if(isset($_POST["mobile"])){$mobile =($_POST["mobile"]);}
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"]);}



if(isset($_POST["net"])){$net =($_POST["net"]);}
if(isset($_POST["vat"])){$vat =($_POST["vat"]);}
if(isset($_POST["percentage"])){$percentage =($_POST["percentage"]);}


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




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

$vatrate=20;
$vat=$net/100*20;
$total=$net+$vat;

$vatband=5;


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, '.', '');


//  To read a text file and get number...
$file = 'invoice.txt';
$invoice_number = intval(file_get_contents ( $file ));   // read file data and make sure it is a number
$invoice_number = $invoice_number + 1;   //  Add one to it
file_put_contents( $file, $invoice_number );
?>

Thanks

You have a reply in one of your recent threads showing bbcode code tags -

I have edited your post above to add them.

Sorry to have to state this, but writing out that wall of repetitive code is not programming. It is you sitting in front of a computer pushing keys. You are spending most of your time copy/pasting/over-typing, and then going through it all for every change you have to make. This is a waste of your time, doing things that you should be getting the computer to do for you.

You need to learn how to use arrays. Arrays are for sets of data, where you will be operating on each element in the set in the same way. You can then just use a loop instead of writing out all that code for every possible input.

The non-numerical value errors are probably due to empty form fields being submitted and your code is not validating the input data before using it. You ALWAYS need to validate inputs to your code before using them. If a ‘required’ field is empty, that’s a user error and you should setup and output a unique and helpful message for each field. If a field is not required and is empty don’t use it (in a calculation.)

Your form processing code should -

  1. Be on the same page as the form. This will let you re-populate the form field values when you re-display the form when there are validation errors.
  2. Detect that a post method form was submitted before accessing the form data.
  3. Keep the set of form data as an array, then operate on elements of this array throughout the rest of the code.
  4. Trim, then validate all the inputs, storing validation error messages in an array, using the field name as the array index. This error array is also an error flag. If the array is empty, there are no errors and you can use the submitted form data. You can test/display the content of this array at the appropriate point in the html document to display the errors.
  5. Only un-checked check-boxes/radio-buttons won’t be set. All other form fields will be set once the form has been submitted. All those isset() statements for always-set form fields are a waste of typing. You even have lines of code with isset() for things created in the server-side code and there’s a number of them for things that don’t exist anywhere. Keep It Simple (KISS.)
  6. The repeated code, quantity, name, and price form field names should be arrays. This will let you test, filter, and loop over the values using php array functions.
  7. The use of a text file for the invoice number, without using file locking/testing logic, will end up resetting the invoice number back to 1, when there are concurrent instances of your script running, since they all cannot access the file as the same time. You should use a database for this (whole) process and use an auto-increment integer column to generate the invoice number.

Addition points for the html document, not already mentioned above -

  1. All the label tags are empty. Either use them correctly or don’t put them in at all.
  2. You can eliminate the ids for the labels by putting the label tags around the form fields.
  3. The repeated code, quantity, name, price region should be output using a php loop.

Lastly, when you have more than about 2-3 form fields, you need to dynamically validate and process the form data. This will eliminate all that repetitive bespoke code. The following is an example of a data driven design, where there’s a data structure (array, database table) that defines and controls what general purpose code should do.

<?php

// recursive trim function
function _trim($val){
	if(is_array($val)){
		return array_map('_trim',$val);
	} else {
		return trim($val);
	}
}

// empty string array_filter call-back function
function _empty($var){
    return $var !== '';
}

// number format function
function _number_format($var){
	return number_format($var, 2, '.', '');
}


// examine the submitted form data
echo '<pre>'; print_r($_POST); echo '</pre>';

$errors = []; // an array to hold validation/user error messages
$post = []; // an array to hold a trimmed working copy of the form data

// define the expected form fields (only a few, for demonstration purposes are shown)
$fields = [];
$fields['date'] = ['label'=>'Date', 'required'=>true];
// ...
$fields['code'] = ['label'=>'Product Code', 'array'=>true, 'required'=>true];
$fields['quantity'] = ['label'=>'Quantity', 'array'=>true, 'required'=>true];
$fields['price'] = ['label'=>'Price', 'array'=>true, 'required'=>true];
// add definitions for all the rest of the fields...

// notes for the above:
// 1. the main array index is the form field name
// 2. the 'label' element can be used for anything you want, e.g. a label for the form field, part of an error message...
// 3. the 'required' element indicates the field must not be empty
// 4. the 'array' element indicates the field is an array of values
// 5. you must include the 'required'/'array' elements if you want to set them to a true value. you can either leave the element out or set it to a false value to disable the choice.


// post method form processing
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// trim the submitted data
	$post = array_map('_trim',$_POST);
	
	// validate 'required' inputs
	foreach($fields as $field=>$arr)
	{
		// test if the input is 'required'
		if($arr['required'] ?? false)
		{
			// test if the input is expected to be an array of values
			if($arr['array'] ?? false)
			{
				// input is an array, it must contain at least one non-empty element

				// note: depending on the usage of array form fields, you may want to apply array_filter() to actually remove empty values at this point, rather than to just test the values

				// actually remove any non-empty elements
				$post[$field] = array_filter($post[$field],'_empty');

				// the following just tests if the array only has non-empty elements
				if(empty(array_filter($post[$field],'_empty')))
				{
					$errors[$field] = "You must enter at least one {$arr['label']}";
				}
			}
			else
			{
				// input is a scalar (single-dimension) variable, must not be an empty string
				if($post[$field] === '')
				{
					$errors['$field'] = "{$arr['label']} is required.";
				}
			}
		}
	}
	
	// perform other validation tests here...
	
	// if no errors, use the submitted data
	if(empty($errors))
	{
		// $post['quantity'] and $post['price'] are arrays. if you have applied array_filter to remove empty entries, you can just loop over the remaining non-empty entries
		$sum = []; // an array to hold the sums
		foreach(array_keys($post['quantity']) as $key)
		{
			$sum[$key] = $post['quantity'][$key] * $post['price'][$key];
		}
		
		$net = array_sum($sum);
		$vatrate = 20;
		$vat = $net/100 * $vatrate;
		$total = $net + $vat;
	
		// format all the numbers in $sum
		$sum = array_map('_number_format',$sum);
		// format the other numbers using the same user _number_format function
		$net = _number_format($net);
		$vat = _number_format($vat);
		$total = _number_format($total);
		
		// finish processing the submitted data...
	}
	
	// if no errors, success
	if(empty($errors))
	{
		// redirect to the exact same url of this page to cause a get request - PRG Post, Redirect, Get.
		header("Refresh:0");
		exit;
		// note: if you want to display a one-time success message, store it in a session variable, then test, display, and clear that variable in the html document
	}
}
?>


<?php
// display any errors at the appropriate point in the html document
if(!empty($errors))
{
	echo implode('<br>',$errors);
}
?>
Sponsor our Newsletter | Privacy Policy | Terms of Service