SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form question with PHP

    Ok, I'm using a regular HTML form but I need to add some PHP code to make it a little more interactive. The form asks the user how many posters they would like to buy and they have the option of a couple of different numbers via a drop down menu. Each poster costs let's say $2.50. Is there a way I can (through PHP) display to the user how much that would cost once they choose the number of posters they want to purchase. I just want the PHP to add x amount of posters X $2.50. I just don't know how to make the attribute names in the HTML into a PHP variable. If anyone could explain this to me, it'd be great.

  2. #2
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if you want to display this information before the form is submitted, you need to use something like javascript to make this possible.

    Don't ask me how, though. :-P
    Patrick Smith
    PHP Programmer

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is the basis of what you want. When the user clicks off the select box the total cost value changes.

    HTML Code:
     <html>
     <head>
     <script type="text/javascript">
     
     function totalCost() {
     	if(!document.getElementById) return;
     	var qty = document.getElementById('quantity').value;
     	cost = qty*2.5; //Multiply by $2.50 per poster
     	cost = cost.toFixed(2);
     	
     	document.getElementById('totalCost').innerHTML = cost;
     }
     </script>
     </head>
     <body>
     	
     
     <form method="post" action="action.php">
     <p> Quantity
     <select name="quantity" id="quantity" onblur="totalCost()">
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="5">5</option>
     <option value="10">10</option>
     <option value="15">15</option>
     <option value="20">20</option>
     </select>
     </p>
     
     <p><strong>Total</strong> $ <span id="totalCost">2.50</span></p>
     </form>
     
     </body>
     </html>

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh JavaScript is needed to do this? Ok cool, I know JavaScript better. Can somebody back him up please, and also tell me how I would do this is JavaScript.

  5. #5
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, use some javascript, or a little more advanced, AJAX

  6. #6
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cranial bore thanks for the help. I still need help with 2 more things. Ok, there is also an option that you can check that would add 15 dollars to the final total. Could somebody please explain to me how I would add that in to the previous code cranial_bore gave me? I know it probably involves conditional statements but I'm a noob.

  7. #7
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anybody help me with this?

  8. #8
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there you go...
    cheers


    Code:
     <html>
     <head>
     <script type="text/javascript">
     
     function totalCost() {
     	if(!document.getElementById) return;
     	var qty = document.getElementById('quantity').value;
     	cost = qty*2.5; //Multiply by $2.50 per poster
    	var add15 = document.getElementById('add15').value;
    	if(add15 = 1)
    	{
    	cost += 15;
    	}	
     	cost = cost.toFixed(2);
    	
     	document.getElementById('totalCost').innerHTML = cost;
    	
    	
     }
     </script>
     </head>
     <body>
     	
     
     <form method="post" action="action.php">
     <p> Quantity
     <select name="quantity" id="quantity" onblur="totalCost()">
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="5">5</option>
     <option value="10">10</option>
     <option value="15">15</option>
     <option value="20">20</option>
     </select>
    </p>
     <p>
       <input type="checkbox" name="add15" value="checkbox"> 
       $15 ?   </p>
     <p><strong>Total</strong> $ <span id="totalCost">2.50</span></p>
     </form>
     
     </body>
     </html>
    I LOVE PHP!!!

  9. #9
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    diskhub - I think if(add15 = 1) should beif(add15 == 1)
    Also the HTML checkbox needs to have the id attribute defined.

  10. #10
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Chicago
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I'll check both ways out to see if they work.


Bookmarks

Posting Permissions

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