dmaar Posted April 9, 2010 Share Posted April 9, 2010 This is seriously giving me an ulcer...I've been working on this reasonably simple code all day, and can not get it to work for the life of me. Here's the rundown: On the html page the user inputs the quantity desired for each item. When they submit, the PHP is supposed to print a table with the name, cost, quantity desired, and price for each item. It's pretty straight forward, so I have to be overlooking something fairly basic. Basically what is happening is that it prints 0 for both quantity desired and item cost no matter what the user inputs. I opted to to use arrays because I'm pretty new to PHP, and this way makes more sense to me. Here's the code: if ($SG=="") $SG = 0; if ($lespaul=="") $lespaul = 0; if ($musicman=="") $musicman = 0; if ($jazz=="") $jazz = 0; if ($mackie=="") $mackie = 0; if ($mackie2=="") $mackie2 = 0; if ($firestudio=="") $firestudio = 0; $sg_cost = 1899.99 * $sg; $lespaul_cost = 1315.99 * $lespaul; $musicman_cost = 1499.99 * $musicman; $jazz_cost = 1225.99 * $jazz; $mackie_cost = 2125.99 * $mackie; $mackie2_cost = 629.99 * $mackie2; $firestudio_cost = 649.99 * $firestudio; $total_price = $sg_cost + $lespaul_cost + $musicman_cost + $jazz_cost + $mackie_cost + $mackie2_cost + $firestudio_cost; $total_items = $sg + $lespaul + $musicman + $jazz + $mackie + $mackie2 + $firestudio; ?> <h4> Customer Info: </h4> <?php print ("$name <br /> $street <br /> $city <br />"); ?> <table border = "1"> <caption> Order Information </caption> <tr> <th> Product </th> <th> Unit Price </th> <th> Quantity Ordered </th> <th> Item Cost </th> </tr> <tr align = "center"> <td> SG </td> <td> $1899.99 </td> <td> <?php print ("$sg"); ?> </td> <td> <?php print ("$sg_cost"); ?> </td> </tr> <tr align = "center"> <td> Les Paul </td> <td> $1315.99 </td> <td> <?php print ("$lespaul"); ?> </td> <td> <?php print ("$lespaul_cost"); ?> </td> </tr> <tr align = "center"> <td>Music Man</td> <td> $1499.99 </td> <td> <?php print ("$musicman"); ?> </td> <td> <?php print ("$musicman_cost"); ?> </td> </tr> <tr align = "center"> <td> Fender Jazz Bass </td> <td> $1225.99 </td> <td> <?php print ("$jazz"); ?> </td> <td> <?php print ("$jazz_cost"); ?> </td> </tr> </table> There's a little more code before that for processing the contact information, but that works fine so I didn't think it was relevant. Let me know if you need me to post anything else. I would really appreciate any help you guys could give, Thanks. Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/ Share on other sites More sharing options...
Ken2k7 Posted April 9, 2010 Share Posted April 9, 2010 Anything multiplied by 0 = 0. Did you skip that class in school? Also $sg is undefined in the code above. I think you meant $SG. But yeah, please show the beginning portion of the code where you specified all those variables. Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039662 Share on other sites More sharing options...
ddubs Posted April 9, 2010 Share Posted April 9, 2010 He is using * 0 to make sure their cost is zero'ed out for his massive addition of variables at the end. Anyway we can see the html for so I can see how its submitting? (And yea - lowercase the $SG to match the rest of your code) Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039664 Share on other sites More sharing options...
dmaar Posted April 9, 2010 Author Share Posted April 9, 2010 Yeah, the 0's are used to set the value to 0 "if" the value the user entered was "" (nothing). I changed the uppercase SG right after I posted this. Here's the html page with the form: http://mktg-content.bsa.kent.edu/~sequela/products.html Here's the html code for the form (scroll to the left): <strong>Instruments</strong> <table border="1" cellspacing="20" cellpadding="5"> <tr> <td> <a href="sg.jpg" target="_blank"><img src="sg.jpg" alt="picture of a SG guitar" width="100" height="100"/></a> </td> <td class="product"> <b><span style="color:#242D6B">Gibson SG Reissue</span></b> <br/> P-90 pickups re-create '60s sound and look, all-mahogany body, bound rosewood fingerboard with dot inlays <br/><br/> <span style="color:#cc0000"><b>$</b>1,899.99</span> <br/> Quantity? <form action=""> <input type="text" name="sg" size="1"/> </form> </td> </tr> <tr> <td> <a href="gibson.jpg" target="_blank"><img src="gibson.jpg" alt="picture of a Gibson Les Paul Guitar" width="100" height="100" /></a> </td> <td class="product"> <b><span style="color:#242D6B">Gibson Les Paul Studio</span></b> <br/> Mahogany neck with '60s profile, ebony fretboard, trapezoid inlays <br/><br/> <span style="color:#cc0000"><b>$</b>1,315.99</span> <br/> Quantity? <form action=""> <input type="text" name="lespaul" size="1"/> </form> </td> </tr> <tr> <td> <a href="bass.jpg" target="_blank"><img src="bass.jpg" alt="picture of a Music Man Bass Guitar" width="100" height="100" /></a> </td> <td class="product"> <b><span style="color:#242D6B">Music Man Stealth Stringray Bass</span></b> <br/> 4 string, matte black finish, maple neck, ebony fretboard <br/><br/> <span style="color:#cc0000"><b>$</b>1,499.99</span> <br/> Quantity? <form action=""> <input type="text" name="musicman" size="1"/> </form> </td> </tr> <tr> <td> <a href="fender.jpg" target="_blank"><img src="fender.jpg" alt="picture of an American Fender Bass Guitar" width="100" height="100" /></a> </td> <td class="product"> <b><span style="color:#242D6B">Fender American Jazz Bass</span></b> <br/> Alder body, graphite-reinforced neck, rosewood fretboard, chrome hardware <br/><br/> <span style="color:#cc0000"><b>$</b>1,225.99</span> <br/> Quantity? <form action=""> <input type="text" name="jazz" size="1"/> </form> </td> </tr> </table> <strong>Pro Audio</strong> <table border="1" cellspacing="20" cellpadding="5"> <tr> <td> <a href="mackie2.jpg" target="_blank"><img src="mackie2.jpg" alt="picture of a Mackie Onyx Mixer" width="100" height="100" /></a> </td> <td class="product"> <b><span style="color:#242D6B">Mackie Onyx Premium 32-Channel Mixer</span></b> <br/> 32 channel analog, 32 Onyx preamps, built-in limiter/compressor <br/><br/> <span style="color:#cc0000"><b>$</b>2125.99</span> <br/> Quantity? <form action=""> <input type="text" name="mackie" size="1"/> </form> </td> </tr> <tr> <td> <a href="mackie1.jpg" target="_blank"><img src="mackie1.jpg" alt="picture of a Mackie CFX16 Mixer" width="100" height="100" /></a> </td> <td class="product"> <b><span style="color:#242D6B">Mackie CFX16 Mixer</span></b> <br/> Low noise high-headroom Mackie preamps, 12 mono mic channels, built in digital effects processor <br/><br/> <span style="color:#cc0000"><b>$</b>629.99</span> <br/> Quantity? <form action=""> <input type="text" name="mackie2" size="1"/> </form> </td> </tr> <tr> <td> <a href="firestudio.jpg" target="_blank"><img src="firestudio.jpg" alt="picture of a Presonus Firestudio Recording Interface" width="100" height="100" /></a> </td> <td class="product"> <b><span style="color:#242D6B">Presonus FireStudio Recording Interface</span></b> <br/> Firewire, 8 class A mic preamps, Mac and Windows compatible, includes software suite <br/><br/> <span style="color:#cc0000"><b>$</b>649.99</span> <br/> Quantity? <form action=""> <input type="text" name="firestudio" size="1"/> </form> </td> </tr> </table> <br/><hr/><h3>Order Form</h3> <hr/> <form action="products.php" method="post"> Full Name: <br/> <input type="text" name="name" size="30"/><br/> Street Address:<br/> <input type="text" name="street" size="70"/> <br/> City, State, Zip:<br/> <input type="text" name="city" size="70"/><br/> Phone Number (ddd-ddd-dddd) <br/> <input type="text" name="phone" size="30"/> <br/> <strong>Payment Method</strong><br/> <input type="radio" name="payment" value= "Visa"/> Visa<br/> <input type="radio" name="payment" value ="Mastercard"/> Mastercard<br/> <input type="radio" name="payment" value= "Discover"/> Discover<br/> <input type="radio" name="payment" value ="American Express"/> American Express<br/> <input type="submit" value="Checkout"/> <input type="reset" value="Clear All Fields"/> </form> Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039674 Share on other sites More sharing options...
Ken2k7 Posted April 9, 2010 Share Posted April 9, 2010 This part if ($SG=="") $SG = 0; if ($lespaul=="") $lespaul = 0; if ($musicman=="") $musicman = 0; if ($jazz=="") $jazz = 0; if ($mackie=="") $mackie = 0; if ($mackie2=="") $mackie2 = 0; if ($firestudio=="") $firestudio = 0; What's above those lines? Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039681 Share on other sites More sharing options...
dmaar Posted April 9, 2010 Author Share Posted April 9, 2010 Here's the full PHP code: <?php extract($_POST); if (empty ($name)) { die('You did not enter your Name. Please press the back button on your browser and complete all of the required fields'); } if (empty ($phone)) { die('You did not enter your Phone Number. Please press the back button on your browser and complete all of the required fields'); } if (!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $phone)) { die('You did not enter your Phone Number in the correct format. Please go back and enter your Phone Number again.'); } if (empty ($street)) { die('You did not enter your Street Address. Please press the back button on your browser and complete all of the required fields'); } if (empty ($city)) { die('You did not enter your City, State, or Zipcode. Please press the back button on your browser and complete all of the required fields'); } if (empty ($payment)) { die('You did not enter your Credit Card information. Please press the back button on your browser and complete all of the required fields'); } if ($SG=="") $SG = 0; if ($lespaul=="") $lespaul = 0; if ($musicman=="") $musicman = 0; if ($jazz=="") $jazz = 0; if ($mackie=="") $mackie = 0; if ($mackie2=="") $mackie2 = 0; if ($firestudio=="") $firestudio = 0; $sg_cost = 1899.99 * $sg; $lespaul_cost = 1315.99 * $lespaul; $musicman_cost = 1499.99 * $musicman; $jazz_cost = 1225.99 * $jazz; $mackie_cost = 2125.99 * $mackie; $mackie2_cost = 629.99 * $mackie2; $firestudio_cost = 649.99 * $firestudio; $total_price = $sg_cost + $lespaul_cost + $musicman_cost + $jazz_cost + $mackie_cost + $mackie2_cost + $firestudio_cost; $total_items = $sg + $lespaul + $musicman + $jazz + $mackie + $mackie2 + $firestudio; ?> <h4> Customer Info: </h4> <?php print ("$name <br /> $street <br /> $city <br />"); ?> <table border = "1"> <caption> Order Information </caption> <tr> <th> Product </th> <th> Unit Price </th> <th> Quantity Ordered </th> <th> Item Cost </th> </tr> <tr align = "center"> <td> SG </td> <td> $1899.99 </td> <td> <?php print ("$sg"); ?> </td> <td> <?php print ("$sg_cost"); ?> </td> </tr> <tr align = "center"> <td> Les Paul </td> <td> $1315.99 </td> <td> <?php print ("$lespaul"); ?> </td> <td> <?php print ("$lespaul_cost"); ?> </td> </tr> <tr align = "center"> <td>Music Man</td> <td> $1499.99 </td> <td> <?php print ("$musicman"); ?> </td> <td> <?php print ("$musicman_cost"); ?> </td> </tr> <tr align = "center"> <td> Fender Jazz Bass </td> <td> $1225.99 </td> <td> <?php print ("$jazz"); ?> </td> <td> <?php print ("$jazz_cost"); ?> </td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039689 Share on other sites More sharing options...
DavidAM Posted April 9, 2010 Share Posted April 9, 2010 You have all of the items in separate forms, and they are separate from the one with the contact information. When you click a submit button ONLY the FORM that contains that button is submitted. Make the whole page ONE big FORM and you should be good to go. Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039737 Share on other sites More sharing options...
dmaar Posted April 9, 2010 Author Share Posted April 9, 2010 Thanks DavidAM, worked perfectly. I don't know what I was thinking when I set it up like that. Link to comment https://forums.phpfreaks.com/topic/198148-form-handling-code-having-lots-of-troublewont-print-right/#findComment-1039804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.