-11

I wrote a php file Calculator.php that posts result to info.php, and I wrote another php file - Calculator2.php that posts results to the same page. How can I combine them together? I tried to put one after another and it doesnt work.

Calculator.php

<FORM action="info.php" method="post">
<P>
<LABEL for="firstNum">First Number: </LABEL>
<INPUT type="text" name="firstNum" id="firstNumberLabel"><BR>
<LABEL for="secondNum">Second Number: </LABEL>
<INPUT type="text" name="secondNum" id="secondNumberlabel"><BR>

<INPUT type="radio" name="calc" value="1"> Add<BR>
<INPUT type="radio" name="calc" value="2"> Subtract<BR>
<INPUT type="radio" name="calc" value="3"> Multiply<BR>
<INPUT type="radio" name="calc" value="4"> Divide<BR>
<INPUT type="submit" value="Send">
<INPUT type="reset">
</P>
</FORM>

info.php file

<?php

switch ($_POST["calc"]){
case "1":
 echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>";
 break;
case "2":
 echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>";
 break;
case "3":
 echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>";
 break;
case "4":
 echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>";
 break;
default:
 break;
}
?>

Calculator2.php

<form action= <?php echo $_SERVER['PHP_SELF'] ?> method="post">
    <label for="text_field">First Number: </LABEL>
    <input type="text" name="user_text" id="text_field"><BR>
    <label for="text_field2">Second Number: </LABEL>
    <input type="text" name="user_text2" id="text_field2"><BR>

 <input type="submit" value="Add">

 </form>

 <?php
   if(isset($_POST['user_text'])&&isset($_POST['user_text2']))
   echo $_POST['user_text']+$_POST['user_text2'];
 ?>
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
HoKy22
  • 4,057
  • 8
  • 33
  • 54
  • 2
    Show us your code... maybe we can help you. Without see your code we can't help you. – Sena Jun 21 '12 at 18:08
  • Thank you Sena, I just added the code, please take a look if you got a chance! – HoKy22 Jun 21 '12 at 20:26
  • possible duplicate: http://stackoverflow.com/questions/2996806/can-one-form-have-multiple-actions – Don Jun 21 '12 at 20:54

3 Answers3

1
<FORM action="info.php" method="post">
 <P>
 <LABEL for="firstNum">First Number: </LABEL>
 <INPUT type="text" name="firstNum" id="firstNumberLabel"><BR>
 <LABEL for="secondNum">Second Number: </LABEL>
<INPUT type="text" name="secondNum" id="secondNumberlabel"><BR>

<INPUT type="radio" name="calc" value="1"> Add<BR>
<INPUT type="radio" name="calc" value="2"> Subtract<BR>
<INPUT type="radio" name="calc" value="3"> Multiply<BR>
<INPUT type="radio" name="calc" value="4"> Divide<BR>
<INPUT type="submit" value="Send">
<INPUT type="reset">
</P>
</FORM>


<form action= <?php echo $_SERVER['PHP_SELF'] ?> method="post">
<label for="text_field">First Number: </LABEL>
<input type="text" name="firstNum" id="text_field"><BR>
<label for="text_field2">Second Number: </LABEL>
<input type="text" name="secondNum" id="text_field2"><BR>

<INPUT type="radio" name="calc" value="1"> Add<BR>
<INPUT type="radio" name="calc" value="2"> Subtract<BR>
<INPUT type="radio" name="calc" value="3"> Multiply<BR>
<INPUT type="radio" name="calc" value="4"> Divide<BR>
<INPUT type="submit" value="Send">


</form>


<?php
 switch ($_POST["calc"]){
case "1":
 echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>";
 break;
case "2":
 echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>";
 break;
case "3":
 echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>";
 break;
case "4":
 echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>";
 break;
default:
break;
}
?>

and with the same info.php as before

<?php
 switch ($_POST["calc"]){
case "1":
 echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>";
 break;
case "2":
 echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>";
 break;
case "3":
echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>";
 break;
case "4":
echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>";
 break;
default:
 break;
}
?>

the problem with it is i think there is quite a lot of repeated code

HoKy22
  • 4,057
  • 8
  • 33
  • 54
0

Insert your data into the below brackets where needed.

<?php
if(sizeof($_POST) == 0){
    // echo html here
}
else{
    // Perform php actions here
}
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
  • Thanks for your help, it didn't work that way and I just added the code, would you be open to take a look again? – HoKy22 Jun 21 '12 at 19:41
0

Here is a neat solution. Add a common script which process the submission of form and then redirect it with result to wherever require.

Aakash
  • 695
  • 3
  • 10
  • 25
  • Thanks @Aakash I just learned php a week ago, I am actually not sure about what you are saying. Can you give me an example? – HoKy22 Jun 22 '12 at 16:01