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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trouble getting my dropdown box variables...

    Okay so I KNOW I'm making some horrible amateur mistake mainly because I've been doing this now for about a combined total of 8 hours over 2 days...

    So I'm following a PHP learning guide that I'll assume is okay to post... PHP For the Absolute Beginner and so far it's been a really pleasant read. I have very little actual programming background so it's all pretty new candy so far.

    In any case I set a small goal for myself to make a simple "calculator" setup just so I could say I've done it, and preferably before I end up finding a guide where someone explains it. Now the way I've got it setup so far is 2 text boxes divided by a drop down where you select one of four operators (+, -, *, /).

    So I can get it to add just fine... but it seems to ignore any "if/elseif" statements I try after addition, it also seems to ignore a desperate attempt at using switch/case to figure out which operator was selected.

    So with that tl;dr I cannot get my drop downs selected option to be considered when my calculator is running. And like any good amateur I started out with something very simple and it has progressively grown to something more ugly and probably less capable as I've gone on and, clearly, in the wrong direction...

    So here's some HTML, this has barely changed as I've gone on...

    Code:
    <form action="calculator.php" method="post">
              <input type="number" name="num1" size="6">
                   <select name="operator">
                   <option value="1">+</option>
                   <option value="2">-</option>
                   <option value="3">*</option>
                   <option value="4">/</option>
                   </select>
              <input type="number" name="num2" size="6">
         <br />
              <input type="submit" value="Calc">
    </form>
    This was my first and what I thought was a graceful attempt NOTE, I trimmed some areas that I felt were irrelevant to the problem...

    PHP Code:
    $n1 $_POST['num1'];
    $n2 $_POST['num2'];
    $o $_GET['operator'];

        if (
    $o '1') {
            
    $result = ($n1 $n2);
            
    $o2 "+";
            
    $solution true;
        }

        elseif (
    $o '2') {
            
    $result = ($n1 $n2);
            
    $o2 "-";
            
    $solution true;
        }

        elseif (
    $o '3') {
            
    $result = ($n1 $n2);
            
    $o2 "*";
            
    $solution true;
        }

        elseif (
    $o '4') {
            
    $result = ($n1 $n2);
            
    $o2 "/";
            
    $solution true
    And here comes the desperate, inexperienced, attempt at trying to throw more at the problem and hope I could figure it out a different way... Same thing applies trimmed some irrelevant parts...

    PHP Code:
    $n1 $_POST['num1'];
    $n2 $_POST['num2'];
    $o $_GET['operator'];

        switch (
    $o) {
            
            case 
    '1':
                
    $result = ($n1 $n2);
                
    $o2 "+";
                
    $solution true;
            break;
        
        case 
    '2':
                
    $result = ($n1 $n2);
                
    $o2 "-";
                
    $solution true;
            break;
        
        case 
    '3':
                
    $result = ($n1 $n2);
                
    $o2 "*";
                
    $solution true;
            break;
            
        case 
    '4':
                
    $result = ($n1 $n2);
                
    $o2 "/";
                
    $solution true;
            break;
        } 
    So. How bad did I do? (Oh and yes I saw the lines about turning on error reporting and tried those to no avail)

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    $n1 $_POST['num1'];
    $n2 $_POST['num2'];
    $o $_GET['operator']; 
    These should all be coming from $_POST (as dictated by the action attribute of the form). This however is invalid: <input type="number" name="num1" size="6">. There is no input of type "number" in HTML; that should be type="text".

    The switch/case is more accurate than the if/else. The problem with the if/else approach is that you are assigning a value to $o instead of comparing. Use == instead.

    Everything else looks okay, minus the validation/verification of course.

    Edit:
    Also, it appears that HTML5 actually has an input type="number". I would presume that it would then be successful at sending it per normal so that PHP can still find it under name. I haven't a clue what its purpose is otherwise.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So apparently the rabbit hole as a sense of humor.

    Changing the switch case string to $_POST instead of $_GET the operator seems to have fixed it up and it's working great. Now I just can't get my failure halt to work, but that's another story the calculator works fine.

    Any word on why the if-else isn't working? It returns no error and does the same thing, only solves the addition section at the top of the script.

    Woa sneaky fast comment!


    -----------


    Changed that small bit in the if-else string to read == and now it works great too. I also get why, now, the switch-case is more appropriate. Thanks for the clarification mate!
    Last edited by digitaL.braVo; 10-29-2012 at 08:11 PM. Reason: To keep from spammin'


  •  

    Posting Permissions

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