Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    sssunny (02-22-2013)

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    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.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •