| | |
Need Help Processing This Dynamic Form
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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!
Thanks in advance,
Anthony
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!
PHP Syntax (Toggle Plain Text)
index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Test</title> <script type="text/javascript" src="includes/items.js"></script> <?php require_once("includes/functions.php"); ?> </head> <body> <? if (isset($_POST["submit"])) { // I set up two arrays, one catching all posted variables and one catching any errors. $postArray = array(); $errorArray = array(); // This selects the key and value of each post item. The $$ is a global only used for $_POST data. // It takes the $_POST name as the new variable name and assigns the $_POST's value as the variable's value. foreach ($_POST as $key => $value) { $$key = trim($value); $postArray[$key] = trim($value); validValue($key, $value); } // Unset the counter and submit variables, they're not needed within the array. unset($postArray["counter"]); unset($postArray["submit"]); // Echo out the variables within $postArray. echo "<p>The post array looks as follows: "; print_r($postArray); echo "</p>\r\n\r\n"; // Echo out the variables within $errorArray. echo "<p>The error array looks as follows: "; print_r($errorArray); echo "</p>\r\n\r\n"; // Echo out how many sets of textboxes the script finds. echo "<p>There are <strong>" . $counter . "</strong> sets of text boxes.</p>\r\n"; // Iterating through the error array. echo "<ul>\r\n"; foreach ($errorArray as $key => $value) { echo "<li>Key: $key<br />Value: $value</li>\r\n"; } echo "</ul>\r\n"; } ?> <p><a href="javascript:addItem();">Click Me To Add</a></p> <p><a href="javascript:removeItem();">Click Me To Delete</a></p> <div id="formDiv"> <form id="purchaseOrder" name="purchaseOrder" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div id="test" style="width: 650px;"> <label for="reference1" id="reference1lbl" name="reference1lbl">Reference</label> <input id="reference1" name="reference1" type="text" value="" /> <label for="quantity1" id="quantity1lbl" name="quantity1lbl">Quantity</label> <input id="quantity1" name="quantity1" type="text" value="" /> <label for="details1" id="details1lbl" name="details1lbl">Details</label> <input id="details1" name="details1" type="text" value="" /> </div> <input name="counter" id="counter" type="text" value="1" /> <input name="submit" id="submit" type="submit" /> </form> </div> </body> </html> functions.php <? /* This function initially tests to see if the value passed is empty. If so, it then runs more if statements that take the first letter of the key and see if it's r(reference), q(quantity) or d(details). It then updates the array. */ function validValue($key, $value) { // By declaring $errorArray global within the function, all references to it will refer to the global version. global $errorArray; if (empty($value)) { if (strtolower(substr($key, 0, 1)) == "r") { $stripped = substr($key, 9); $errorArray[$key] = "Item $stripped: You have left the reference field empty. Please fill it in."; } if (strtolower(substr($key, 0, 1)) == "q") { $stripped = substr($key, 8); $errorArray[$key] = "Item $stripped: You have left the quantity field empty. Please fill it in."; } if (strtolower(substr($key, 0, 1)) == "d") { $stripped = substr($key, 7); $errorArray[$key] = "Item $stripped: You have left the details field empty. Please fill it in."; } } // If the first letter of the key is q, and the value isn't empty, test to see if it's numeric. if ((strtolower(substr($key, 0, 1)) == "q") && (!empty($value))) { if (!is_numeric($value)) { $stripped = substr($key, 8); $errorArray[$key] = "Item $stripped: The value in the quantity field is not a valid number. Please try again."; } } } ?>
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
What does the script output as it is?
Personally, since you have a
Now that's pseudo because I haven't tested it or anything but you get the idea.
Your
You can then print this to the screen like so: (maybe only do this if there are no errors)
Hope this helps. If you feel like adopting this method you can PM me and I'll help you implement it if needed.
Personally, since you have a
$counter variable I would do the following to retrieve the data that was POSTed to the page: php Syntax (Toggle Plain Text)
if (isset($_POST["submit"])) { $count = $_POST['counter']; $items = array(); //each element has another array inside it for ($i=0; $i<$count; $i++){ $items[$i] = array('r'=>'', 'q'=>0, 'd'=>'', e=>''); //e for errors //(expanded for clarity) $items[$i]['r'] = $_POST['reference' . $i+1]; //$i+1 because you started at "reference1" and arrays start at 0 $items[$i]['q'] = $_POST['quantity' . $i+1]; $items[$i]['d'] = $_POST['details' . $i+1]; //$items[$i]['e'] = validItem($items[$i]); } //I don't think I'm missing anything there... }
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: php Syntax (Toggle Plain Text)
function validItem($item){ $errors = ''; if ($item['d'] /*is invalid*/){ $errors .= 'Description invalid. '; } if ($item['q'] /*is invalid*/){ $errors .= 'Quantity invalid. '; } //etc. return $errors; }
You can then print this to the screen like so: (maybe only do this if there are no errors)
php Syntax (Toggle Plain Text)
<form id="purchaseOrder" name="purchaseOrder" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <? php foreach ($items as $key=>$item){ echo '<div id="test" style="width: 650px;">'; echo '<label for="reference' . $key+1 . '" id="reference' . $key+1 . 'lbl" name="reference' . $key+1 . 'lbl">Reference</label>'; echo '<input id="reference' . $key+1 . '" name="reference' . $key+1 . '" type="text" value="'. $item['r'] .'" />'; echo '<label for="quantity' . $key+1 . '" id="quantity' . $key+1 . 'lbl" name="quantity' . $key+1 . 'lbl">Quantity</label>'; echo '<input id="quantity' . $key+1 . '" name="quantity' . $key+1 . '" type="text" value="'. $item['q'] .'" />'; echo '<label for="details' . $key+1 . '" id="details' . $key+1 . 'lbl" name="details' . $key+1 . 'lbl">Details</label>'; echo '<input id="details' . $key+1 . '" name="details' . $key+1 . '" type="text" value="'. $item['d'] .'" />'; if ($item['e'] != ''){ //errors are present for this item echo '<br />'; echo '<span id="bigAndRed">There is an error with this item: ' . $item['e'] . '</span>'; } echo '</div>'; } ?> <input name="counter" id="counter" type="text" value="1" /> <input name="submit" id="submit" type="submit" /> </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.
★ 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.
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
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
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- dynamically update pages (HTML and CSS)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- What is server and client side code? (ASP.NET)
- AGP Aperture Size Question (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: Adding paypal purchase to existing site
- Next Thread: transfer to a new page through a select event
| Thread Tools | Search this Thread |
5.2.10 action apache api array beginner binary broken cakephp checkbox class classes cms code cron curl database date destroy display dynamic echo echo$_get[x]changingitintovariable... email encode error fcc file files folder form forms function functions google header howtowriteathesis href htaccess html if-else image images include insert ip javascript joomla limit link local login mail memberships menu mlm mod_rewrite multiple multipletables mysql mysqlquery neutrality oop open passwords paypal pdf php provider query radio random record remote rss script search server sessions sockets source space sql strip_tags syntax system table template thesishelp tutorial update upload url validator variable video voteup web window.onbeforeunload=closeme; youtube





