CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   php coding problem.!!.. (http://www.codingforums.com/showthread.php?t=288067)

sssunny 02-21-2013 05:29 PM

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>


Fou-Lu 02-21-2013 05:32 PM

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.

Arcticwarrio 02-22-2013 04:18 PM

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>



All times are GMT +1. The time now is 01:12 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.