Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Austin Klenk
Austin Klenk
4,399 Points

PHP & Mysql

I currently am working on a project. The project is to keep track of Pieces done "Piece Rate", The employee goes to the webpage and clicks down on their name and picks the product that they did and how many then it submits it to the database. Where i am currently stuck at is how can get the price of the item before it submits it to the database and calculates it.

4 Answers

You'll need to embed an identifier for each product in your form - you could also hide the price in there somewhere if you wanted to quickly display a total for the employee using jquery/javascript (or you could use ajax to look up the price for the product when it's selected). Then submit the product id and quantity to the server, look up the price using the id, calculate the total and store it.

Austin Klenk
Austin Klenk
4,399 Points

how exactly could i do that?

are you getting your products from a table? If so then you probably have an 'id' field, so when you output all of the products, make the value of the option the id - so something like this (but use a loop to output your products as options):

<select name="product">
  <option value="id_1">Product 1</option>
  <option value="id_2">Product 2</option>
  <option value="id_3">Product 3</option>
</select>

I highly recommend Treehouse's 'Mike's t-shirt' projects ('Build a Simple PHP Application' and 'Enhancing a Simple PHP Application' - if you haven't done those yet you should check them out. I also recommend http://stackoverflow.com - lots of very helpful souls over there.

Austin Klenk
Austin Klenk
4,399 Points

I have done the Shirts For Mike project already, actually i learned from it to list items on a page.