Need Help Processing This Dynamic Form

Reply

Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Need Help Processing This Dynamic Form

 
0
  #1
Oct 28th, 2008
Hi everyone and thanks for reading.

I started a project a while ago and am stuck on how to proceed processing this form. It's a very simple form but I'm still at a mental roadblock regarding how to proceed from this point. The idea is that the form is a dynamic purchase order form where the user can add as many items to the form as they please, and remove them all except for one. I have this functionality accomplished with JavaScript already. The function I wrote uses a hidden counter to count how many boxes there are, and this is also passed as a $_POST variable to the PHP script.

I now want to validate the $_POST entries, and create an error array from them so that I can reshow the form to the user and highlight erronerous boxes in red and say ok you need to do this or that.

I've posted the code I have currently below, and hopefully someone can get my wheels going again!

  1. index.php
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>Form Test</title>
  8. <script type="text/javascript" src="includes/items.js"></script>
  9. <?php
  10. require_once("includes/functions.php");
  11. ?>
  12. </head>
  13.  
  14. <body>
  15. <?
  16. if (isset($_POST["submit"])) {
  17. // I set up two arrays, one catching all posted variables and one catching any errors.
  18. $postArray = array();
  19. $errorArray = array();
  20.  
  21. // This selects the key and value of each post item. The $$ is a global only used for $_POST data.
  22. // It takes the $_POST name as the new variable name and assigns the $_POST's value as the variable's value.
  23. foreach ($_POST as $key => $value) {
  24. $$key = trim($value);
  25. $postArray[$key] = trim($value);
  26. validValue($key, $value);
  27. }
  28.  
  29. // Unset the counter and submit variables, they're not needed within the array.
  30. unset($postArray["counter"]);
  31. unset($postArray["submit"]);
  32.  
  33. // Echo out the variables within $postArray.
  34. echo "<p>The post array looks as follows: ";
  35. print_r($postArray);
  36. echo "</p>\r\n\r\n";
  37.  
  38. // Echo out the variables within $errorArray.
  39. echo "<p>The error array looks as follows: ";
  40. print_r($errorArray);
  41. echo "</p>\r\n\r\n";
  42.  
  43. // Echo out how many sets of textboxes the script finds.
  44. echo "<p>There are <strong>" . $counter . "</strong> sets of text boxes.</p>\r\n";
  45.  
  46. // Iterating through the error array.
  47. echo "<ul>\r\n";
  48. foreach ($errorArray as $key => $value) {
  49. echo "<li>Key: $key<br />Value: $value</li>\r\n";
  50. }
  51. echo "</ul>\r\n";
  52.  
  53.  
  54. }
  55. ?>
  56. <p><a href="javascript:addItem();">Click Me To Add</a></p>
  57. <p><a href="javascript:removeItem();">Click Me To Delete</a></p>
  58.  
  59. <div id="formDiv">
  60. <form id="purchaseOrder" name="purchaseOrder" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  61.  
  62. <div id="test" style="width: 650px;">
  63. <label for="reference1" id="reference1lbl" name="reference1lbl">Reference</label>
  64. <input id="reference1" name="reference1" type="text" value="" />
  65. <label for="quantity1" id="quantity1lbl" name="quantity1lbl">Quantity</label>
  66. <input id="quantity1" name="quantity1" type="text" value="" />
  67. <label for="details1" id="details1lbl" name="details1lbl">Details</label>
  68. <input id="details1" name="details1" type="text" value="" />
  69. </div>
  70.  
  71. <input name="counter" id="counter" type="text" value="1" />
  72. <input name="submit" id="submit" type="submit" />
  73. </form>
  74. </div>
  75. </body>
  76. </html>
  77.  
  78. functions.php
  79.  
  80. <?
  81. /*
  82.   This function initially tests to see if the value passed is empty. If so, it then runs more if statements that take
  83.   the first letter of the key and see if it's r(reference), q(quantity) or d(details). It then updates the array.
  84. */
  85. function validValue($key, $value) {
  86. // By declaring $errorArray global within the function, all references to it will refer to the global version.
  87. global $errorArray;
  88.  
  89. if (empty($value)) {
  90. if (strtolower(substr($key, 0, 1)) == "r") {
  91. $stripped = substr($key, 9);
  92. $errorArray[$key] = "Item $stripped: You have left the reference field empty. Please fill it in.";
  93. }
  94.  
  95. if (strtolower(substr($key, 0, 1)) == "q") {
  96. $stripped = substr($key, 8);
  97. $errorArray[$key] = "Item $stripped: You have left the quantity field empty. Please fill it in.";
  98. }
  99.  
  100. if (strtolower(substr($key, 0, 1)) == "d") {
  101. $stripped = substr($key, 7);
  102. $errorArray[$key] = "Item $stripped: You have left the details field empty. Please fill it in.";
  103. }
  104. }
  105.  
  106. // If the first letter of the key is q, and the value isn't empty, test to see if it's numeric.
  107. if ((strtolower(substr($key, 0, 1)) == "q") && (!empty($value))) {
  108. if (!is_numeric($value)) {
  109. $stripped = substr($key, 8);
  110. $errorArray[$key] = "Item $stripped: The value in the quantity field is not a valid number. Please try again.";
  111. }
  112. }
  113.  
  114. }
  115. ?>

Thanks in advance,

Anthony
Last edited by antwan1986; Oct 28th, 2008 at 9:13 am.
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Need Help Processing This Dynamic Form

 
0
  #2
Oct 29th, 2008
What does the script output as it is?

Personally, since you have a $counter variable I would do the following to retrieve the data that was POSTed to the page:

  1. if (isset($_POST["submit"])) {
  2. $count = $_POST['counter'];
  3. $items = array(); //each element has another array inside it
  4. for ($i=0; $i<$count; $i++){
  5. $items[$i] = array('r'=>'', 'q'=>0, 'd'=>'', e=>''); //e for errors
  6. //(expanded for clarity)
  7. $items[$i]['r'] = $_POST['reference' . $i+1];
  8. //$i+1 because you started at "reference1" and arrays start at 0
  9. $items[$i]['q'] = $_POST['quantity' . $i+1];
  10. $items[$i]['d'] = $_POST['details' . $i+1];
  11. //$items[$i]['e'] = validItem($items[$i]);
  12. } //I don't think I'm missing anything there...
  13. }

Now that's pseudo because I haven't tested it or anything but you get the idea.
Your validItem() function could then look like this:
  1. function validItem($item){
  2. $errors = '';
  3. if ($item['d'] /*is invalid*/){
  4. $errors .= 'Description invalid. ';
  5. }
  6. if ($item['q'] /*is invalid*/){
  7. $errors .= 'Quantity invalid. ';
  8. }
  9. //etc.
  10. return $errors;
  11. }

You can then print this to the screen like so: (maybe only do this if there are no errors)
  1. <form id="purchaseOrder" name="purchaseOrder" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  2. <? php
  3. foreach ($items as $key=>$item){
  4. echo '<div id="test" style="width: 650px;">';
  5. echo '<label for="reference' . $key+1 . '" id="reference' . $key+1 . 'lbl" name="reference' . $key+1 . 'lbl">Reference</label>';
  6. echo '<input id="reference' . $key+1 . '" name="reference' . $key+1 . '" type="text" value="'. $item['r'] .'" />';
  7. echo '<label for="quantity' . $key+1 . '" id="quantity' . $key+1 . 'lbl" name="quantity' . $key+1 . 'lbl">Quantity</label>';
  8. echo '<input id="quantity' . $key+1 . '" name="quantity' . $key+1 . '" type="text" value="'. $item['q'] .'" />';
  9. echo '<label for="details' . $key+1 . '" id="details' . $key+1 . 'lbl" name="details' . $key+1 . 'lbl">Details</label>';
  10. echo '<input id="details' . $key+1 . '" name="details' . $key+1 . '" type="text" value="'. $item['d'] .'" />';
  11. if ($item['e'] != ''){ //errors are present for this item
  12. echo '<br />';
  13. echo '<span id="bigAndRed">There is an error with this item: ' . $item['e'] . '</span>';
  14. }
  15. echo '</div>';
  16. }
  17. ?>
  18. <input name="counter" id="counter" type="text" value="1" />
  19. <input name="submit" id="submit" type="submit" />
  20. </form>

Hope this helps. If you feel like adopting this method you can PM me and I'll help you implement it if needed.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Re: Need Help Processing This Dynamic Form

 
0
  #3
Oct 29th, 2008
Hi humbug,

Thank you for going to so much effort to help me with my problem. My work schedule's pretty packed for the next few days but hopefully I'll get round to looking at this over the weekend. I appreciate you offering me any help through PM, and I'll certainly be contacting you!

Talk soon,

Anthony
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC