Addition print result

Posted 24 February 2013 - 07:53 AM

Hello there,
I'm learning PHP and I'm having trouble with printing results of two numbers, when I put two numbers in cells it should pop up next cell with result, but it's not working and I've not idea what I've wrong. Also I'm getting one error on line 30. Thank you for any tip/help.

The function is named empty() not is_empty(), and the font and center tags are OBSOLETE.

Thank you for help.

It's showing now result, but outside of cell. Is there any function which can set result in cell? And when it show up result it also show up that phrase "RARE FATAL ERROR", but it would be suppose show just in case if numbers aren't set and user want press button.

Re: Addition print result

If you mean to insert the results in the inputs then set them as the value of the inputs.

I assume that you corrected the following line as well?

if(is_empty($_POST["c1" || "c2"])){

Ok, I'll try do it via CSS. Thanks again, value working and now it's show correct in cell. Yes i corrected that line too. But still persistent problem with this sort of code, when I load page first it show "Please fill the cells" and if I submit numbers it show up result correct but with that "RARE FATAL CODE" under. I was about to put if inside first if but anyway not working.

Re: Addition print result

Posted 24 February 2013 - 10:50 AM

You shouldn't use isset() and empty() independently, although it probably still works. If isset() is false - so the value isn't set/does not exist - then testing a non-existent variable against empty() is not viable.

Re: Addition print result

You need to distinguish between the form not having been submitted, and having been submitted but with no input-numbers. I would use a hidden input for this:

you can also test, whether the request was done using POST or GET. another way is testing, whether the form fields were set at all (a form submit with invalid data is different from a "form" without data) i.e. the difference between isset() and isset() && is_numeric(). a nice function for that are the filter functions:

$c1 = filter_input(INPUT_POST, "c1", FILTER_VALIDATE_FLOAT);
// returns the float if the submitted input is a float/number
// returns FALSE if the submitted input is not a float/number
// returns NULL if there is no value posted as 'c1'

Re: Addition print result

Posted 24 February 2013 - 02:14 PM

though IMHO, the real problem is that the output (the form) and the form processing script are in the same file. and therefore you need an explicit distinguation, whether the script should do one or the other.

one way of avoiding that problem is to use two different script files (though that comes with other problems).
another way that has been established is to use only a single script file for requesting data and to decide inside this script (based on the passed parameters) what to do (or to pass it farther to script files that handle the task). this way is also known as the Front Controller Pattern.

Re: Addition print result

You need to distinguish between the form not having been submitted, and having been submitted but with no input-numbers. I would use a hidden input for this:

you can also test, whether the request was done using POST or GET. another way is testing, whether the form fields were set at all (a form submit with invalid data is different from a "form" without data) i.e. the difference between isset() and isset() && is_numeric(). a nice function for that are the filter functions:

$c1 = filter_input(INPUT_POST, "c1", FILTER_VALIDATE_FLOAT);
// returns the float if the submitted input is a float/number
// returns FALSE if the submitted input is not a float/number
// returns NULL if there is no value posted as 'c1'