Problem with $_POST[] - not working

Discussion in 'PHP' started by AlphaDarkMatter, Jul 17, 2012.

  1. #1
    Hey everyone, I am encountering a problem with $_POST[]; here goes my code:
    <?php
    // create short variable names
    $tireqty = $_POST['5'];
    $oilqty = $_POST['10'];
    $sparkqty = $_POST['90'];
    $find = $_POST['find'];
    ?>
    <html>
    <head>
    <title>Bob's Auto Parts - Order Results</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Results</h2>
    <?php


    echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";


    echo "<p>Your order is as follows: </p>";


    $totalqty = 0;
    $totalqty = $tireqty + $oilqty + $sparkqty;
    echo "Items ordered: ".$totalqty."<br />";




    if ($totalqty == 0) {


    echo "You did not order anything on the previous page!<br />";


    } else {


    if ($tireqty > 0) {
    echo $tireqty." tires<br />";
    }


    if ($oilqty > 0) {
    echo $oilqty." bottles of oil<br />";
    }


    if ($sparkqty > 0) {
    echo $sparkqty." spark plugs<br />";
    }
    }




    $totalamount = 0.00;


    define('TIREPRICE', 100);
    define('OILPRICE', 10);
    define('SPARKPRICE', 4);


    $totalamount = $tireqty * TIREPRICE
    + $oilqty * OILPRICE
    + $sparkqty * SPARKPRICE;


    echo "Subtotal: $".number_format($totalamount,2)."<br />";


    $taxrate = 0.10; // local sales tax is 10%
    $totalamount = $totalamount * (1 + $taxrate);
    echo "Total including tax: $".number_format($totalamount,2)."<br />";


    if($find == "a") {
    echo "<p>Regular customer.</p>";
    } elseif($find == "b") {
    echo "<p>Customer referred by TV advert.</p>";
    } elseif($find == "c") {
    echo "<p>Customer referred by phone directory.</p>";
    } elseif($find == "d") {
    echo "<p>Customer referred by word of mouth.</p>";
    } else {
    echo "<p>We do not know how this customer found us.</p>";
    }


    ?>
    </body>
    </html>
    When I go to preview the result my HTML comes up like this:
    Bob's Auto Parts

    Order Results

    Order processed at 23:29, 17th July 2012
    Your order is as follows:
    Items ordered: 0
    You did not order anything on the previous page!
    Subtotal: $0.00
    Total including tax: $0.00
    We do not know how this customer found us.
    PS: register_globals is turned on.
     
    AlphaDarkMatter, Jul 17, 2012 IP
  2. Microsuck

    Microsuck Greenhorn

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    8
    #2
    Can you provide all of the code? Because in your HTML form, you're going to need to set the name attribute for every input element that you plan to catch with $_POST.
     
    Microsuck, Jul 17, 2012 IP
  3. AlphaDarkMatter

    AlphaDarkMatter Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Basically I don't have the HTML yet, I am running the tests by manually altering the code and previewing in my browser... and nothing happens. :-(
     
    AlphaDarkMatter, Jul 17, 2012 IP
  4. dennison21

    dennison21 Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    well you should recheck to see if all your symbols are placed correctly and whats your method of manual altering and testing?
     
    dennison21, Jul 17, 2012 IP
  5. jeet25

    jeet25 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think you should integrate assignment operation with some event like click on submit button
     
    jeet25, Jul 19, 2012 IP
  6. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #6
    It would help if you actually told us what your problems are. You should also always check that keys in an array exists before using them:

    
    if(array_key_exists("key", $_POST))
    {
        $var = $_POST["key"];
    }
    
    Code (markup):
    Otherwise you will get an "undeclared index" error if they don't exist.

    My first advice would be to do some white-box testing, have you tried printing/echoing the post values to make sure that they're even set?

    What does this mean? You can't directly set values within the global $_POST variable. You have to use an HTTP post request either through an HTML form or with cURL.

    I just realised something, why is such a large index being used with $_POST? I hope you don't think that $_POST['90'] sets anything to 90... I think you should go back to basics and learn what arrays are and what they do. Why do you have an integer within quotes? You're also converting 90 into a string!
     
    Last edited: Jul 21, 2012
    BRUm, Jul 21, 2012 IP