Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Unableto add in PHP

General discussions related to php

Moderators: macek, egami, gesf

Unableto add in PHP

Postby naveensrikan » Sat May 31, 2014 2:39 am

HI,

I am using the below code to perform a simple addition. I can see the 2 fields on the form but when i click on submit button it is unable to perform the addition. It does not throw any error but it is not giving me any results. Please help

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
   <form action="" method="post">
   <input type="number" name="1" placeholder="number" />
   <input type="number" name="2" placeholder="number2" />
   <input type="submit" value="Sum" />
   
   <?php
   if ((!empty($_POST['1'])) && (!empty($_POST['2'])))
   {
   $number = $_POST['1'];
   $number2 = $_POST ['2'];
   $total = $number + $number2;
   Print  "$total";
   }
   ?>
</form>
</body>
</html>

naveensrikan
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sat May 31, 2014 2:16 am

Re: Unableto add in PHP

Postby peterachutha » Sat May 31, 2014 10:45 pm

The code worked very well on my server. No problems.

Just a suggestion: I would not use '1' and '2' as later on when you code gets more complex you might mis-read it.
peterachutha
New php-forum User
New php-forum User
 
Posts: 10
Joined: Sat May 31, 2014 10:35 pm

Re: Unableto add in PHP

Postby developerahusain » Sat May 31, 2014 10:45 pm

Instead of Print %total,you use echo $total.That would be more ideal.
Developerahusain
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Unableto add in PHP

Postby peterachutha » Sat May 31, 2014 10:58 pm

May be you miss-interpreted how placeholder works. It does not transfer a value into the field.

If you wanted a default value transferred into the fields you could have written it as:-

<input type="number" name="1" placeholder="number" value="<?php echo $var1; ?>"/>
<input type="number" name="2" placeholder="number2" value="<?php echo $var2; ?>" />

I hope this helps.
peterachutha
New php-forum User
New php-forum User
 
Posts: 10
Joined: Sat May 31, 2014 10:35 pm


Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.