Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2013, 05:29 PM   PM User | #1
sssunny
New Coder

 
Join Date: Jul 2012
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
sssunny is an unknown quantity at this point
php coding problem.!!..

i want to add two number,and then i want complier to ask me,if i want to add more number in previously added number,if i say yes it should add two more number to previous number and if i say no it should show the current result,for ex:if i want to add 5 and 8 it would be 13,then i want compiler to ask if i want to add more number,if no then it should show 13,if yes then it would add another two numbers in 13.

Here is the code i have tried
PHP Code:
<?php
if(isset($_POST['number1']) && isset($_POST['number2']))
{
$number1=$_POST['number1'];
$number2=$_POST['number2'];
$yes='';
$yes=$_POST['yes'];
function 
add($number1,$number2)
{
$number3$number1+$number2;
echo 
$number3;   
echo 
'do you want to add more numbers (y) or (n)';
}
if(
$yes=='y')
{
$sum1=$sum+add($number1,$number2);
echo 
'new total is'.$sum1;   
}
if(
$yes=='n')
{
echo 
$sum;   
}

add($number1,$number2);
}

?>
<form id="form1" name="form1" method="post" action="test.php">
<table width="100%" cellpadding="2">
  <tr>
    <td>number1:
      
        <label>
          <input type="text" name="number1" id="number1" />
        </label>
  </td>
  </tr>
  <tr>
    <td>number2:
      
        <label>
          <input type="text" name="number2" id="number2" />
        </label>
  </td>
  </tr>
  <tr>
    <td>
      <input type="submit" name="submit" id="submit" value="Submit" /></td>
  </tr>
  <tr>
    <td><label>
      <input type="text" name="yes" id="textfield" />
    </label></td>
  </tr>
</table>
</form>
sssunny is offline   Reply With Quote
Old 02-21-2013, 05:32 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can't do that with PHP in a web environment. HTTP is stateless and PHP is a server side language, so it only works on request and response. You can use AJAX for that, but if you're doing that anyways you may as well put the burden entirely on the JS for the calculations.
What you can do is keep track of the sum and pass it through a hidden field on the form. Then you can keep self submitting the form and so long as the sum is > 0, you simply provide only one field to add to it, otherwise you give two fields to add together.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sssunny (02-22-2013)
Old 02-22-2013, 04:18 PM   PM User | #3
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
Arcticwarrio is on a distinguished road
you can try something like this:

PHP Code:
<form id="form1" name="form1" method="post" action="">
<?php
if (isset($_POST['number'])){
    foreach (
$_POST['number'] as $k => $v){
        
$result $result $v;
    }
    
$text 'Add More';
    echo 
'<input type="hidden" name="number[]" value="'.$result.'"/>';
    echo 
$result;
}else{
    
$text 'Submit';
}
?>
<table width="100%" cellpadding="2">
  <tr>
    <td>number1:<input type="text" name="number[]" /></td>
  </tr>
  <tr>
    <td>number2:<input type="text" name="number[]"/></td>
  </tr>
  <tr>
    <td><input type="submit" name="submit" id="submit" value="<?php echo $text ?>" /></td>
  </tr>
</table>
</form>
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:44 AM.


Advertisement
Log in to turn off these ads.