Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: php calculator

  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts

    php calculator

    I am new to php and don't really have a clue what i'm doing. I have created a html file with five text boxes for entry of 5 numbers and 4 check box options what I want to do is have the numbers entered into the text boxes posted to a backend php program but i dont know how to do this. Any help would be greatly appreciated.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, have you worked with forms at all (with PHP)? And control structures?

    Rather then us just feeding you code, why don't you tell us/show us what you've tried, and we can help you get it working and teach along the way.

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    I have tried in the past but have struggled with it. This is all I have so far:

    Code:
    <h1 align="center">Simple Calculator</h1>
    <form name="myform" action="" method="POST">
    <div align="center"><br>
    <input type="radio" name="calculator" value="Highest"> Highest<br>
    <input type="radio" name="calculator" value="Lowest" checked> Lowest<br>
    <input type="radio" name="calculator" value="Average"> Average<br>
    <input type="radio" name="calculator" value="Sort in ascending order"> Sort in ascending order<br>
    </div>
    </form>
    
    <form name="myform" action="" method="post">
    <div align="center"><br>
    <input title="Please Enter a whole number" id="first" name="number" type="number" size="12" maxlength="3" value="number" /><br />
    <input title="Please Enter a whole number" id="last" name="number" type="number" size="18" maxlength="3" value="number" /><br />
    <input title="Please Enter a whole number" id="first" name="number" type="number" size="12" maxlength="3" value="number" /><br />
    <input title="Please Enter a whole number" id="first" name="number" type="number" size="12" maxlength="3" value="number" /><br />
    <input title="Please Enter a whole number" id="first" name="number" type="number" size="12" maxlength="3" value="number" /><br />
    <input type="submit" value="SUBMIT" />
    </div>
    If you could even help with how to start the php I would really appreciate it

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Sure, well, first things first:

    You have 2 forms, which do not work together. Think of a form as an envelope. In your first envelope, you have 4 radio buttons, which having the same name, will submit 1 piece of data. Your second envelope has 5 inputs and a submit. The 5 inputs have the same name, so they'll also only submit 1 piece of data. Submitting a form is like of like having one stamp... you have two envelopes with different data and one stamp... either you'll send your radio buttons data or your input (see right below here for why your input setup is wrong).

    So the first lesson: names must be unique to submit uniquely. If you think about it, how does the system know which "number" to submit? Remember that variables must be unique in the same scope. So you first want to change the names to different things. The exception to that is radio buttons. Because radio buttons are about choosing one item out of a set, you give them the same name. Next, you need to review HTML specs for inputs. There is no "number" type, so your HTML instantly fails and won't work properly. Following specs is very important, as some browsers might be smart and figure it out, while others *cough*IE*cough* are sometimes very finicky.

    After that, you should understand how forms submit. Once the data goes through successfully, the data submits via the method you specify. You put post, and PHP accesses post variables through the $_POST superglobal. So you would access your radio button through $_POST['calculator']. You can always print_r($_POST); to see what the post contains (it'll give you an idea as you experiment).

    Next, I recommend googling something like "php form tutorial" or variations thereof. You'll find plenty with great basics. I would only recommend shying away from W3Schools tutorials/guides... they're great for basic info, but have a tendency to give wrong information.

  • #5
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    This is what I have now, can you have a look at my code to see if im the right tracks?

    Code:
    <html>
    <title></title>
    <body>
    <h1>Simple Calculator</h1>
    
    <form name="myform" action="calculation.php" method="POST">
    <h2>Please Select an Option</h2>
    <div>
    <input type="radio" name="calculator" value="Highest"> Highest<br>
    <input type="radio" name="calculator" value="Lowest" checked> Lowest<br>
    <input type="radio" name="calculator" value="Average"> Average<br>
    <input type="radio" name="calculator" value="Sort in ascending order"> Sort in ascending order<br>
    </div>
    <input type="submit" />
    </form>
    
    <h2>Insert five numbers in the form and hit submit button</h2>
    <form name="form" action="calculation.php" method="post"> 
    Firstnumber: <input name="num1" type="text" /><br>
    Secondnumber: <input name="num2" type="text" /><br> 
    Thirdnumber: <input name="num3" type="text" /><br> 
    Fourthnumber: <input name="num4" type="text" /><br> 
    Fithnumber: <input name="num5" type="text" /><br>  
    <input type="submit" />
    </form>
    </body>
    </html>
    Code:
    <html>
    <head>
    <title>Simple Math With User Input</tite>
    </head>
    <body>
    <?php
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $num3 = $_POST['num3'];
    $num4 = $_POST['num4'];
    $num5 = $_POST['num5'];
    $a = $num1 + $num2 + $num3 + $num4 + $num5;
    $b = $num1 - $num2 + $num3 + $num4 + $num5;
    echo "The sum of the five numbers is ". $a;
    echo "The difference of the five numbers is ". $b;
    ?>
    </body>
    </html>
    The php isn't complete as the chosen option should also be posted to the backend php program.
    Last edited by helen11; 06-20-2012 at 12:58 PM.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well done. Now lets refine what you have.

    First, your form is still setup wrong. You have two submits (which isn't an issue, but is unnecessary) and you close your form half way through the code. Try running your code and doing the print I posted earlier: see what it does and figure out what its doing.

    Next, every time you load the calculation page, it will try to find $_POST['num1'] et all. Since someone might accidentally go to that page without going through the form first, if any of those post variables are not set, they'll get an error. So you want to first verify that they are set. Its commonly done via the function isset (which as it suggests, checks if a variable is set). Be sure to read through php.net... documenation will be your best friend! So you'll want to give your submit button a name (lets call it submit) and do something like this:

    PHP Code:
    if (isset($_POST['submit'])) {
    $num1 $_POST['num1'];
    $num2 $_POST['num2'];
    $num3 $_POST['num3'];
    $num4 $_POST['num4'];
    $num5 $_POST['num5'];
    $a $num1 $num2 $num3 $num4 $num5;
    $b $num1 $num2 $num3 $num4 $num5;
    echo 
    "The sum of the five numbers is "$a;
    echo 
    "The difference of the five numbers is "$b;

    So it will check if you've actually submitted the form. That's obviously very weak checking though, but it gives you an idea of where to go from here.

    Next, I don't see a doctype defined. That's very important, as it tells the page how to figure stuff out. Most people go for the HTML5 doctype these days, and I think its the simplest (and of course, the most future proof, for now). Read up on doctypes.

    But great progress!

  • Users who have thanked Keleth for this post:

    helen11 (06-20-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •