Jump to content

[SOLVED] Problem with calc.php program


stbalaji2u

Recommended Posts

I executed this smalllllll calculator program in php and when i cleared all my errors and again tried to execute in a browser but the firefox says that the

 

""Connection Interrupted

The connection to the server was reset while the page was loading   

The network link was interrupted while negotiating a connection. Please try again.""

 

i used a calc.html page to create form and passed the value using the post method

what the hell is wrong with my code.. :'(

this is calc.html's code

<html>
<Title>Calculator</Title>
<body>
<form name=calc action='calc.php' method='post'>
<table border =0 cellspacing =3 cellpadding=2>
<tr><td colspan=4 align=center>
<h3>Calculator</h3>
<tr><td>
<input type=text name=num1>
<td>
<select name=func>
	<option value=add>+</option>
	<option value=sub>-</option>
	<option value=mul>*</option>
	<option value=div>/</option>
</select>
<td>
<input type=text name=num2>
<td>
<input type=submit name=submit value="Go!" >
</table>
</form>
</body>
</html>

 

 

and this is calc.php's code

<?php
$num1=$_POST[num1];
$num2=$_POST[num2];
$func=$_POST[func];
$submit=$_POST[submit];
if($submit==false){
include('calc.php');
}
else if($func==add){
include('form.php');
$num3=$num1 + $num2;
echo "<b>Solution</b>";
echo "<br>";
echo "$num1 + $num2 = $num3";
}
else if($func==sub){
include('calc.php');
$num3=$num1 - $num2;
echo "<b>Solution</b>";
echo "<br>";
echo "$num1 - $num2 = $num3";
}
else if($func==mul){
include('calc.php');
$num3=$num1*$num2;
echo "<b>Solution</b>";
echo "<br>";
echo "$num1 * $num2 = $num3";
}
else if($func==div){
include('calc.php');
$num3=$num1/$num2;
echo "<b>Solution</b>";
echo "<br>";
echo "$num1 / $num2 = $num3";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/119316-solved-problem-with-calcphp-program/
Share on other sites

The form submits to calc.php, however in calc.php you include calc.php! Whats that about? Doing that could cause an infinity loop and thus the error you get.

 

EDIT: This is how I'd do it:

<?php

if(isset($_POST['submit']))
{
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];

    switch($_POST['func'])
    {
        case 'sub':
            $num3 = $num1 - $num2;
            $sum  = "$num1 - $num2 = $num3";
        break;

        case 'mul':
            $num3 = $num1 * $num2;
            $sum  = "$num1 * $num2 = $num3";
        break;

        case 'div':
            $num3 = $num1 / $num2;
            $sum  = "$num1 / $num2 = $num3";
        break;

        case 'add':
        default:
            $num3 = $num1 + $num2;
            $sum  = "$num1 + $num2 = $num3";
        break;
    }

    echo "<b>Solution</b>";
    echo "<br>";
    echo $sum . '<hr />';
}

include 'calc.html';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.