14 Replies - 677 Views - Last Post: 26 February 2013 - 02:57 AM Rate Topic: -----

#1 pepablok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 13

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.

<!DOCTYPE html>
<html>
<head>
	<title>Addition of two numbers</title>
	<link rel="stylesheet" type="text/css" href="blah.css">
</head>
<body>
	<center>
		<center title="text">
			<h1><strong id="element">Addition of two numbers</strong></h1><br><br>
		</center>
      <form action="index.php" method="POST">
			<input type="text" name="c1" class="textInput" placeholder="Enter first number"/><br><br>
			<input type="text" name="c2" class="textInput" placeholder="Enter second number"/><br><br>
			<p class="button">
				<input type="submit" value="MAGIC !" name="calculate"/> 
			</p>
	</form>
</center>

<?php

if(isset($_POST["c1" && "c2"])){
	$c1 = $_POST['c1'];
	$c2 = $_POST['c2'];

	echo "<br><center><input type=\"text\" id=\"\" style=\"text-align:center\"/>".($c1+$c2)."</center>";
}

if(is_empty($_POST["c1" || "c2"])){
    echo "Please fill the cells";
}
else{
    echo "<font color=\"red\" size=\"20\">"RARE FATAL ERROR"</font>"; /* 34	null	unexpected 'RARE' (T_STRING), expecting ',' or ';' */
}
   
?>

</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Addition print result

#2 pepablok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 13

Re: Addition print result

Posted 24 February 2013 - 08:15 AM

I solved that error, I just removed " ". But still, it's not working
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • View blog
  • Posts: 10,309
  • Joined: 08-August 08

Re: Addition print result

Posted 24 February 2013 - 09:36 AM

View Postpepablok, on 24 February 2013 - 11:15 AM, said:

I just removed " ". But still, it's not working

Is that a good description of your problem?

What is this??? $_POST['x' && 'y'] and $_POST['x' || 'y'] It sure doesn't look like PHP.

This post has been edited by CTphpnwb: 24 February 2013 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

Re: Addition print result

Posted 24 February 2013 - 09:38 AM

if(isset($_POST["c1" && "c2"])){

// should be

if(isset($_POST["c1"]) && isset($_POST["c2"])){

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

This post has been edited by andrewsw: 24 February 2013 - 09:39 AM

Was This Post Helpful? 1
  • +
  • -

#5 pepablok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 13

Re: Addition print result

Posted 24 February 2013 - 10:04 AM

View PostCTphpnwb, on 24 February 2013 - 09:36 AM, said:

View Postpepablok, on 24 February 2013 - 11:15 AM, said:

I just removed " ". But still, it's not working

Is that good description of your problem?

What is this??? $_POST['x' && 'y'] and $_POST['x' || 'y'] It sure doesn't look like PHP.


Well, I was thinking that $_POST['x' && 'y'] is same declaration as in C.


View Postandrewsw, on 24 February 2013 - 09:38 AM, said:

if(isset($_POST["c1" && "c2"])){

// should be

if(isset($_POST["c1"]) && isset($_POST["c2"])){

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.

If font and center are obsolete, what I should use instead of it?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

Re: Addition print result

Posted 24 February 2013 - 10:10 AM

View Postpepablok, on 24 February 2013 - 10:04 AM, said:

If font and center are obsolete, what I should use instead of it?

CSS

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"])){

Was This Post Helpful? 0
  • +
  • -

#7 pepablok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 13

Re: Addition print result

Posted 24 February 2013 - 10:20 AM

View Postandrewsw, on 24 February 2013 - 10:10 AM, said:

View Postpepablok, on 24 February 2013 - 10:04 AM, said:

If font and center are obsolete, what I should use instead of it?

CSS

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.
if(empty($_POST["c1"]) || empty($_POST["c2"])){
    echo "Please fill the cells";
}

else{
    echo "<font color=\"red\" size=\"20\">RARE FATAL ERROR</font>";
}


Full code after fix:
<!DOCTYPE html>
<html>
<head>
	<title>Addition of two numbers</title>
	<link rel="stylesheet" type="text/css" href="blah.css">
</head>
<body>
	<center>
		<center title="text">
			<h1><strong id="element">Addition of two numbers</strong></h1><br><br>
		</center>
      <form action="index.php" method="POST">
			<input type="text" name="c1" class="textInput" placeholder="Enter first number"/><br><br>
			<input type="text" name="c2" class="textInput" placeholder="Enter second number"/><br><br>
			<p class="button">
				<input type="submit" value="MAGIC !" name="calculate"/> 
			</p>
	</form>
</center>

<?php

if(isset($_POST["c1"]) && isset($_POST["c2"])){
	$c1 = $_POST['c1'];
	$c2 = $_POST['c2'];
	echo "<br><center><input type=\"text\" value=".($c1+$c2)." style=\"text-align:center\"/></center>";
}

if(empty($_POST["c1"]) || empty($_POST["c2"])){
    echo "Please fill the cells";
}

else{
    echo "<font color=\"red\" size=\"20\">RARE FATAL ERROR</font>";
}
   
?>

</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

Re: Addition print result

Posted 24 February 2013 - 10:42 AM

Yes, that is what you are telling it to do:

if(empty($_POST["c1"]) || empty($_POST["c2"])){
    echo "Please fill the cells";
}

else{
    echo "<font color=\"red\" size=\"20\">RARE FATAL ERROR</font>";
}

If neither cell is empty it displays the message 'RATE FATAL ERROR'. Get rid of the else, or change the message 'Please fill the cells' to your message 'RARE FATAL ERROR'.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

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.

I would do something like this:

<?php

$c1 = false;
$c2 = false;

if (isset($_POST['c1']) && is_numeric($_POST['c1'])) {
    $c1 = intval($_POST['c1'], 10);
}
if (isset($_POST['c2']) && is_numeric($_POST['c2'])) {
    $c2 = intval($_POST['c2'], 10);
}
if ($c1 === false || $c2 === false) {
    echo "Please fill the cells";
} else {
    echo "<br><center><input type=\"text\" value=".($c1+$c2)." style=\"text-align:center\"/></center>";
}
?>

Was This Post Helpful? 0
  • +
  • -

#10 pepablok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 13

Re: Addition print result

Posted 24 February 2013 - 01:28 PM

Thanks, but I'm still getting same issue, when I load page it's show up already with "Please fill the cells".
Posted Image
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

Re: Addition print result

Posted 24 February 2013 - 01:45 PM

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:

<input type="hidden" name="submitted" value="1">

if (isset($_POST['submitted'])) {
    echo "Please fill the cells.";
}


Modifying my previous code:

<?php

$c1 = false;
$c2 = false;

if (isset($_POST['c1']) && is_numeric($_POST['c1'])) {
    $c1 = intval($_POST['c1'], 10);
}
if (isset($_POST['c2']) && is_numeric($_POST['c2'])) {
    $c2 = intval($_POST['c2'], 10);
}
if (($c1 === false || $c2 === false) && isset($_POST['submitted'])) {
        echo "Please fill the cells";
} else {
    echo "<br><input type=\"text\" value=".($c1+$c2)." style=\"text-align:center; margin:0 auto;\"/>";
}
 
?>

Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Addition print result

Posted 24 February 2013 - 01:57 PM

View Postandrewsw, on 24 February 2013 - 09:45 PM, said:

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'

Was This Post Helpful? 1
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

Re: Addition print result

Posted 24 February 2013 - 02:05 PM

Note that the filter functions require PHP >= 5.2.0.

Following on from Dormilich 's post:

if ($_SERVER['REQUEST_METHOD'] == "POST") 

but we can also use

if (!empty($_POST))

Was This Post Helpful? 1
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#15 pepablok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-February 13

Re: Addition print result

Posted 26 February 2013 - 02:57 AM

View PostDormilich, on 24 February 2013 - 01:57 PM, said:

View Postandrewsw, on 24 February 2013 - 09:45 PM, said:

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'



View Postandrewsw, on 24 February 2013 - 02:05 PM, said:

Note that the filter functions require PHP >= 5.2.0.

Following on from Dormilich 's post:

if ($_SERVER['REQUEST_METHOD'] == "POST") 

but we can also use

if (!empty($_POST))



Thank You! Both of this method working well. Thanks for your time and attention.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1