I wrote a script years ago for a simple php calculator that did multiplication price per square feet, total square feet and visits per week, and the total would be the sum of the three. I cant get it to multiply properly, i can’t even get it to display the price per square feet.
I haven’t written any scripts in so long, i cant remember anymore what I am doing, and I couldn’t find a solution anywhere to help me figure it out again. Or one I could understand anyway. So if anyone could help me figure this out, i would greatly appreciate it. Maybe js would be better? I don’t know it’s literally been years since i have done any of this
php code
$priceper = $_POST['0,10'];
$sqfeet = $_POST['sqfeet'];
$perweek = $_POST['perweek'];
// Calculate the total:
$total = $priceper * $sqfeet * $perweek;
html
<div><p>Fill out this form to calculate the total cost:</p>
<form action="" method="post">
<p> Price per square foot <? echo ' . $priceper . '; ?></p>
<p>Square Feet: <input type="text" name="sqfeet"></p>
<p>Visits per week: <input type="text" name="perweek" size="5"></p>
<input type="submit" name="submit" value="Calculate!">
</form>
<p>TOTAL: <? echo $total; ?>
</div>
2
Answers
The HTML form has three input fields:
sqfeet
,perweek
, and asubmit
button. The action attribute of the form is empty, meaning it will submit the form to the same page (index.php or whatever the file name you are using). The method attribute is set to "post" indicating that form data will be sent as POST parameters.In the PHP code, $_POST is an associative array that contains data sent to the server as part of the HTTP POST request. The keys of this array correspond to the "name" attributes of the form elements.
$sqfeet = $_POST['sqfeet'];
retrieves the value entered in the "sqfeet" field from the POST data and assigns it to the PHP variable$sqfeet
.Similarly for the other fields as well. But I think there is been a mistake in the line
$priceper = $_POST['0,10'];
of PHP code attempts to retrieve a value from the $_POST array using ‘0,10’ as the key. The value assigned to$priceper
would be whatever value is associated with the'0,10'
key in the POST data. It’s uncommon to use a string like ‘0,10’ as a key for form data. Typically, form field names are alphanumeric strings without commas. It’s more common to use field names like ‘priceper’ or any valid string if you are retrieving the data from the posted input field from the Form. And also there is no such input field with "name" attribute'0,10'
to be found in your HTML code.It looks like there are a couple of issues in your PHP code.
Issue 1
First, in the HTML form, the PHP echo statement is incorrect. It should be
<?php echo $priceper; ?>
instead of<? echo ' . $priceper . '; ?>
.Issue 2
Secondly, when retrieving the values from html form using
$_POST
, you should use the correct keys. It seems like you’re trying to access the price per square foot using$_POST['0,10']
. Instead, use a more appropriate name for the input, likepriceper
:Make sure to update the corresponding input field in your HTML form:
Once these adjustments are made, it should help resolve the multiplication issue you’re facing.