Skip to content

Commit

Permalink
Validate POST Input in Sample 45
Browse files Browse the repository at this point in the history
Errors will result if input is not numeric.
  • Loading branch information
oleibman committed Sep 5, 2024
1 parent 59ee38f commit fb42a10
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 45 deletions.
5 changes: 1 addition & 4 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => true,
'braces' => true,
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one', 'property' => 'one']], // const are often grouped with other related const
'class_definition' => false,
'class_keyword_remove' => false, // ::class keyword gives us better support in IDE
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'combine_nested_dirname' => true,
Expand Down Expand Up @@ -113,8 +111,7 @@
'no_spaces_inside_parenthesis' => true,
'no_superfluous_elseif' => false, // Might be risky on a huge code base
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_comma_in_singleline' => ['elements' => ['arguments', 'array_destructuring', 'array', 'group_import']],
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
Expand Down
126 changes: 89 additions & 37 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions samples/Basic/45_Quadratic_equation_solver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
?>
<form action="45_Quadratic_equation_solver.php" method="POST">
Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
Enter the coefficients for Ax<sup>2</sup> + Bx + C = 0
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Expand Down Expand Up @@ -47,7 +47,9 @@
<?php
/** If the user has submitted the form, then we need to execute a calculation * */
if (isset($_POST['submit'])) {
if ($_POST['A'] == 0) {
if (!is_numeric($_POST['A']) || !is_numeric($_POST['B']) || !is_numeric($_POST['C'])) { // validate input
$helper->log('Non-numeric input');
} elseif ($_POST['A'] == 0) {
$helper->log('The equation is not quadratic');
} else {
// Calculate and Display the results
Expand All @@ -59,8 +61,12 @@
$r1Formula = '=IMDIV(IMSUM(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . ')';
$r2Formula = '=IF(' . $discriminant . '=0,"Only one root",IMDIV(IMSUB(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . '))';

$helper->log(Calculation::getInstance()->calculateFormula($r1Formula));
$helper->log(Calculation::getInstance()->calculateFormula($r2Formula));
/** @var string */
$output = Calculation::getInstance()->calculateFormula($r1Formula);
$helper->log("$output");
/** @var string */
$output = Calculation::getInstance()->calculateFormula($r2Formula);
$helper->log("$output");
$callEndTime = microtime(true);
$helper->logEndingNotes();
}
Expand Down

0 comments on commit fb42a10

Please sign in to comment.