Jump to content

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';

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.