Results 1 to 9 of 9

Thread: PHP order form?

  1. #1
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059

    PHP order form?

    I want to set up an order form and I'm hoping PHP will help me achieve the following:

    I have about 30 products, all identically priced. I want to have a form with a checkbox for each product where the customer checks off as many products as he wants to order. At 10 products, there is a discount. And at 20 products, there's another discount.

    Is there a way with PHP to automatically calculate the total order cost based on how many products the customer selects?

    Thanks for your help.

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  2. #2
    Join Date
    Jul 2001
    Location
    Coventry, England
    Posts
    130
    PHP Code:
    --- orderform.php ---

    <
    input type="checkbox" name="product[]">Product A<br>
    <
    input type="checkbox" name="product[]">Product B<br>
    <
    input type="checkbox" name="product[]">Product C<br>
    <
    input type="checkbox" name="product[]">Product D<br>
    <
    input type="checkbox" name="product[]">Product E<br>
    ...

    --- 
    orderprocess.php ---

    $totalproducts count($_POST['product']);
    $productprice 5;

    $discountfor10 5;
    $discountfor20 10;

    $discount = ($totalproducts >= 10 && $totalproducts 20) ? $discountfor10 : (($totalproducts 20) ? $discountfor20 0);

    $total = ($productprice $totalproducts) - $discount
    HTH
    Last edited by benoire; 09-04-2002 at 06:48 PM.

  3. #3
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059
    Thank you benoire. If you don't mind, I'd like to ask you to elaborate a bit. I'm really quite new at PHP. Actually, I'm quite pathetic at it...

    You show input tags that are in orderform.php. Does it have to be a .php page? Can the code fall inside an HTML page? If so, do I put the opening and closing php tags around the form? In name="product[]", what do I replace? Just product? And leave the brackets?

    And I'm not sure where to put that code for the orderprocess.php.

    Sorry, I told you I really sucked at PHP...

    Thanks very much for your help.

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  4. #4
    Join Date
    Jul 2001
    Location
    Coventry, England
    Posts
    130
    Heh, no problem, I guess I assumed you knew more than you do, not to worry

    The names I gave each file were irrelevant... the name of the first one is entirely up to you, you can call it something.html if you want, I just tend to call all my pages .php for the sake of continuity, plus the fact that I tend to use PHP on every page anyway. The code I gave under 'orderform.php' is just your bogstandard HTML form, there is no PHP coding involved at this stage. Ensure that your form's <form action=""> tag points to somefilename.php - in this case I used orderprocess.php. The input tags should be part of the same form as the rest of the details that are required as part of placing the order.

    The 'product[]' bits stay as they are - each textbox should be named 'product[]'. The reason for this is that when the data is passed on to the second page, you end up with a variable $product which is an array due to the fact that it contains many values - in this case, it contains 'TRUE' x times where x = number of checkboxes ticked.

    The code in orderprocess.php goes in whichever page you post all your form data too. It can go anywhere, all it does is take the submitted checkbox data, counts the number of values in the array $product and then determines what the discount is based on this value. If you want to use a name other than product, simply change all the name="product[]" parts of orderform.php (or whatever you call it), and alter the $_POST['product'] part of orderprocess.php to $_POST['whatevernameyouchose'].

    You'll no doubt have more code to put in orderprocess.php to email you the order or submit it to the database etc, this can go before or after the code I gave you.

    Hope this helps clear things up a bit, if it doesn't, just say so and I'll try and explain things further

  5. #5
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059
    benoire:

    Thanks very much for taking the time to explain. I think I understand better. I will have a go at it, and let you know if I run into any problems.

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  6. #6
    Join Date
    Jul 2001
    Location
    Coventry, England
    Posts
    130
    Any time Just shout if you get stuck

  7. #7
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059
    benoire:

    Hmm. Actually I'm not sure this setup is going to work. You said that in my form it should read action="orderprocess.php". Obviously, that is in order to pass the info onto orderprocess.php to calculate totals. Problem: If all it's doing is passing totals, I have no way of knowing which products the customer wants to order.

    Originally, I thought of just setting up a form using phpformmail, and when submitted, it would email me the contents of the form (a list of all the products checked off), and I would just put a redirect tag in the form to send the customer to page 2 in the ordering process. On the page 2, he would have to manually select whether he bought a 10-pack, a 20-pack, or whatever. Then when submitting, he would be taken into my cc processor's payment system.

    That's the easiest way to do it. But then I'm relying on the customer to make the right selection on page 2. What I am trying to do instead is automatically display his order on page 2 (based on his selections on page 1), but I still need the particulars of the order emailed to me from page 1.

    Does that make sense? Sorry if I wasn't more specific the first time around...

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  8. #8
    Join Date
    Jul 2001
    Location
    Coventry, England
    Posts
    130
    Good point, that's something I missed The way round this would be to use product[productname] instead of product[] in the name="" tags. Then on the orderprocess.php page, you could use the following code to get a list of products:

    PHP Code:
    $products $_POST['product'];

    foreach (
    $product as $key => $var) {
    $productlist .= $product[$key]."\n";
    }

    $emailbody "Your email message in here with all your other data etc etc.\n\n".$productlist
    Hopefully that should do the trick - but its 2am here and I'm rapidly reaching brain burnout time so I could have missed something

  9. #9
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059
    Aaaaargh.

    I appreciate your help, benoire, but I will have to use my original plan for now just to get the site functional. When it's running and I have time, I will come back to this thread and try again. I just couldn't get it to work properly.

    Thanks again for your help. I'll be back, though...

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

Related Posts from theWHIR.com

Posting Permissions

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