percept
01-11-2004, 07:16 AM
Well I'm certainly finding out about strings and form validation... but I'm finding it difficult to learn... I've been looking into classes as you suggested but also finding this difficult to absorb. Before I get into classes more to do formvalidation, I would like to code myself in order to establish a good understanding of them.
As you suggested I'm using the strlen() to insert a "0" if either formfield for points or cash are left blank and these work great (thank you).
Then I use the str_replace to replace commas and whitespace out of the user's input for points which works fine if I use this alone, but the minute I add the extra str_replace for the items_cash user input I get an error. I can't see what is so different about it from the points code. I want to do this in the first place in case the user inputs the "$" sign or commas or spaces.
This is the code I'm trying to work with. I've looked at the functions at PHPnet and see if there is something else I can try but I'm once again getting stumped.
if (strlen($_POST['item_points']) < 1) {
$item_points = 0;
} else {
$item_points = strip_tags(trim($_POST['item_points']));
}
if (strlen($_POST['item_cash']) < 1) {
$item_cash = 0;
} else {
$item_cash = strip_tags(trim($_POST['item_cash']));
}
$markup_points = array(',', '.', ' ');
$item_points = (str_replace($markup_points, "", $item_points));
$markup_cash = array(',', '$', ' ');
$item__cash = (str_replace($markup_cash, "", $item__cash));
$item_code = strip_tags(trim($item_code));
$item_desc = strip_tags(trim($item_desc));
$item_desc = htmlentities($item_desc, ENT_QUOTES);
$add_item = "INSERT INTO prod_items (prod_id, item_code, item_desc, item_points, item_cash)
VALUES (".mysql_insert_id().", '$item_code', '$item_desc', $item_points, $item_cash)";
$resultitms = mysql_query($add_item) or die ("Couldn't execute query.");
Thank you... I'm sorry if it looks like I'm not trying on my own... I've been trying for many, many hours and finding that I just can't seem to figure it out...
As you suggested I'm using the strlen() to insert a "0" if either formfield for points or cash are left blank and these work great (thank you).
Then I use the str_replace to replace commas and whitespace out of the user's input for points which works fine if I use this alone, but the minute I add the extra str_replace for the items_cash user input I get an error. I can't see what is so different about it from the points code. I want to do this in the first place in case the user inputs the "$" sign or commas or spaces.
This is the code I'm trying to work with. I've looked at the functions at PHPnet and see if there is something else I can try but I'm once again getting stumped.
if (strlen($_POST['item_points']) < 1) {
$item_points = 0;
} else {
$item_points = strip_tags(trim($_POST['item_points']));
}
if (strlen($_POST['item_cash']) < 1) {
$item_cash = 0;
} else {
$item_cash = strip_tags(trim($_POST['item_cash']));
}
$markup_points = array(',', '.', ' ');
$item_points = (str_replace($markup_points, "", $item_points));
$markup_cash = array(',', '$', ' ');
$item__cash = (str_replace($markup_cash, "", $item__cash));
$item_code = strip_tags(trim($item_code));
$item_desc = strip_tags(trim($item_desc));
$item_desc = htmlentities($item_desc, ENT_QUOTES);
$add_item = "INSERT INTO prod_items (prod_id, item_code, item_desc, item_points, item_cash)
VALUES (".mysql_insert_id().", '$item_code', '$item_desc', $item_points, $item_cash)";
$resultitms = mysql_query($add_item) or die ("Couldn't execute query.");
Thank you... I'm sorry if it looks like I'm not trying on my own... I've been trying for many, many hours and finding that I just can't seem to figure it out...