SIMPLE php question from a newb.

Discussion in 'PHP' started by PhilGlass, Jan 24, 2008.

  1. #1
    ok i admit i'm a newbie.

    now that thats out of the way heres my problem.

    I'm creating a simple php calculator.

    enter 2 numbers press submit and it multiplies the numbers and outputs the answer on the same page.

    my problem (I THINK!) is trying to get it to output on the same page.

    if someone could point me to a good tutorial or if possible copy and paste some code for such a simple thing.

    thank you and remember I AM A NEWBIE! *bows* :D
     
    PhilGlass, Jan 24, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    There's nothing wrong with trying to get the output on the same page.

    You can post your code so we can try to fix it. Or you can Google for "PHP calculator", and I'm sure you'll find something.
     
    nico_swd, Jan 24, 2008 IP
  3. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sounds like you want the form action to be set to $_SERVER['PHP_SELF'], posting to itself.
    Use php to check if the form has been submitted, ex.

    
    <?php
    if( isset( $_POST['submit'] ) ) {
    // Clean $_POST variables here and process math functions
    } 
    echo '<!-- Put form here -->
    <form method="POST" action="' .$_SERVER['PHP_SELF']. '" name="adminForm">
    Add form stuff here
    <input type="submit" name="submit" value="Submit" />
    </form>';
    
    ?>
    
    Code (markup):
    Additionally, you can use an else clause after you check if the form has been submitted to show the form only if the form hasn't been submitted. In the example above, the form will always been shown.
     
    xxKillswitch, Jan 24, 2008 IP
  4. PhilGlass

    PhilGlass Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok i kinda got it working...

    this is my code:
    <?php
    if(!$_POST['submit']){ //If the form hasn't been submitted

    //Output a simple form
    echo '<form method="post">
    <input type="text" name="height"><br>
    <input type="text" name="width"><br>
    <input type="submit" name="submit" value="Calculate">
    </form>';

    }else{ //Once the form has been submitted

    $height = $_POST['height']; //Get the equation from the form
    $width = $_POST{'width'};
    $eq = "$height*$width";
    if(ereg('[^0-9\+-\*\/\(\)]',$eq)){ //Check if the equation contains invalid characters

    echo 'This equation contiains invalid characters!'; //Output an error if so

    }else{ //If the equation only contains valid cahracters...

    $answer = eval("echo ".$eq.";"); //Evaluate the equation using eval()
    $cost = $answer * 0.25;

    echo "<br> $answer Square inches"; //Ouput the answer
    echo "<br> estimated cost is $cost";
    }

    }
    ?>


    i put in 12 for both fields press calculate and get

    144
    Square inches
    estimated cost is 0

    this is my exact output (on new lines and everything)

    thanks for the fast reply i appreciate it.

    credit to most of source code to flabberghast.com i just grabbed and edited some
     
    PhilGlass, Jan 24, 2008 IP