PHP from submit help

I was having trouble with this basic calculator program on my wordpress site I am designing for class. I inserted the program into a page and the calculator interface shows up, but when i hit submit, it takes me back to the home page of my wordpress site. When i was testing the code before putting it into the page, the calculations were all displayed on the same page underneath the calculator. I would like to get the calculations to display on the same page. I would assume the problem is with the line, Any help would be appreciated !

[php]<?php

$page = $_GET[‘page’];

class calc {
public $number1;
public $number2;

      function add($number1,$number2) 
      { 
               $result =$number1 + $number2; 
                echo("The sum of $number1 and $number2 is $result<br><br>"); 
                echo("$number1 + $number2 = $result"); 
                exit; 
       } 

      function subtract($number1,$number2) 
      { 
               $result =$number1 - $number2; 
                echo("The difference of $number1 and $number2 is $result<br><br>"); 
                echo("$number1 &#045 $number2 = $result"); 
                exit; 
       } 

      function divide($number1,$number2) 
      { 
               $result =$number1 / $number2; 

                echo("$number1 divided by $number2 is $result<br><br>"); 
                echo("$number1 ÷ $number2 = $result"); 
                exit; 
       } 

      function multiply($number1,$number2) 
      { 
               $result =$number1 * $number2; 
                echo("The product of $number1 and $number2 is $result<br><br>"); 
                echo("$number1 x $number2 = $result"); 
                exit; 
       } 

}
$calc = new calc();
?>

PHP Calculator v1 Number 1:
Number 2:
Operation: Addition Subtraction Division Multiplication
<?php if($page == "calc"){ $number1 = $_POST['value1']; $number2 = $_POST['value2']; $oper = $_POST['oper']; if(!$number1){ echo("You must enter number 1!"); exit; } if(!$number2){ echo("You must enter number 2!"); exit; } if(!$oper){ echo("You must select an operation to do with the numbers!"); exit; }
 if($oper == "add"){ 
      $calc->add($number1,$number2); 
 } 
 if($oper == "subtract"){ 
      $calc->subtract($number1,$number2); 
 } 
 if($oper == "divide"){ 
      $calc->divide($number1,$number2); 
 } 
 if($oper == "multiply"){ 
      $calc->multiply($number1,$number2); 
 } 

}
?>[/php]

try:

<form name="calc" action="<? echo $_SERVER['PHP_SELF'];?>?page=calc" method="POST">

I changed some of my code, and i added the line you posted, but it always kicks me back to the home page.

example: my calculator appears on this page: http://localhost/wordpress/?page_id=71

but when i click submit i go to this page: http://localhost/wordpress/index.php?page=calc

[php]<?php

class calc {
public $number1;
public $number2;

      function add($number1,$number2) 
      { 
               $result =$number1 + $number2; 
                echo("The sum of $number1 and $number2 is $result<br><br>"); 
                echo("$number1 + $number2 = $result"); 
                exit; 
       } 

      function subtract($number1,$number2) 
      { 
               $result =$number1 - $number2; 
                echo("The difference of $number1 and $number2 is $result<br><br>"); 
                echo("$number1 &#045 $number2 = $result"); 
                exit; 
       } 

      function divide($number1,$number2) 
      { 
               $result =$number1 / $number2; 

                echo("$number1 divided by $number2 is $result<br><br>"); 
                echo("$number1 ÷ $number2 = $result"); 
                exit; 
       } 

      function multiply($number1,$number2) 
      { 
               $result =$number1 * $number2; 
                echo("The product of $number1 and $number2 is $result<br><br>"); 
                echo("$number1 x $number2 = $result"); 
                exit; 
       } 

}
$calc = new calc();
?>

PHP Calculator v1 Number 1:
Number 2:
Operation: Addition Subtraction Division Multiplication
<?php
 if($oper == "add"){ 
      $calc->add($number1,$number2); 
 } 
 if($oper == "subtract"){ 
      $calc->subtract($number1,$number2); 
 } 
 if($oper == "divide"){ 
      $calc->divide($number1,$number2); 
 } 
 if($oper == "multiply"){ 
      $calc->multiply($number1,$number2); 
 } 

?>[/php]

well that’s basically what your original code was telling me you wanted it to do anyway.

http://localhost/wordpress/?page_id=71 is not a proper address! You’re missing the entire webpage. You supply a directory and query, but no actual web page. This page is expecting a query string with the name ‘page’ (and calc as it’s value). so if your web page is called mycalculator.php, your form action should be ‘mycalculator.php?page=calc’. You may need to supply a full path.

Your original post was missing the page address too.

aztech: you don’t actually need to specify the file if it’s the index file.

Example: this topic could be found at http://www.phphelp.com/forum/?topic=17015

In this case the server renames your browser bar and adds the index.php.
He’s clearly stated the calculator is on http://localhost/wordpress/?page_id=71 which would be index.php?page_id=71 and the form would take him to index.php?page=calc

brad try this:
[php]

<?php class calc { public $number1; public $number2; function add($number1,$number2) { $result =$number1 + $number2; echo("The sum of $number1 and $number2 is $result

"); echo("$number1 + $number2 = $result"); exit; } function subtract($number1,$number2) { $result =$number1 - $number2; echo("The difference of $number1 and $number2 is $result

"); echo("$number1 &#045 $number2 = $result"); exit; } function divide($number1,$number2) { $result =$number1 / $number2; echo("$number1 divided by $number2 is $result

"); echo("$number1 ÷ $number2 = $result"); exit; } function multiply($number1,$number2) { $result =$number1 * $number2; echo("The product of $number1 and $number2 is $result

"); echo("$number1 x $number2 = $result"); exit; } } ?> PHP Calculator v1 Number 1:
Number 2:
Operation: Addition Subtraction Division Multiplication
<?php if (isset($_POST['submit'])) { $oper = $_POST['oper']; $number1 = $_POST['value1']; $number2 = $_POST['value2']; $calc = new calc(); if($oper == "add"){ $calc->add($number1,$number2); } if($oper == "subtract"){ $calc->subtract($number1,$number2); } if($oper == "divide"){ $calc->divide($number1,$number2); } if($oper == "multiply"){ $calc->multiply($number1,$number2); } } ?> [/php]

well he said he added a calculator to a web page. I assumed it was not his index page. Either way he needs to pass ‘calc’ through a GET to the page for the code to work.

That depends on what page it is on. If it is on page http://localhost/wordpress/?page_id=71 like he said it was, then the last code i posted will work perfectly from what i can gather.

<?php

$page = $_GET[‘page’];

This is the first two lines of his code. It is expecting a value passed to the script using the name ‘page’ via GET.

Further down the code is…

<?php if($page == "calc"){ $number1 = $_POST['value1'];

This tests the value of $page for ‘calc’. If it is, the vars $number1, $number2, and $oper are populated via POST from the form. ‘page=calc’ must be part of the query sent to the script.

Frankly, I don’t know where to go from here but I do know the above statements are true and correct.

Maybe… http://localhost/wordpress/?page_id=71&page=calc

The last one I posted for him doesn’t check for $page, and neither does the latest one he posted. If a user updates their code you have to look at their update not the original posted code.

Thank you for the help guys, i actually got it working with the code from Raythxc. I didnt have it on my website yet it was just running on my localhost. It is up and running now tho !

Sponsor our Newsletter | Privacy Policy | Terms of Service