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!
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