jiveturkey420 Posted August 24, 2012 Share Posted August 24, 2012 I'm having a problem with a shopping cart project I'm doing for school. <font size="6"><b>Add a New Product</b></font><hr/> <form method="POST" action="products.php"> <b>Name:</b> <input type="text" name="prod_name" size="25" value="" /> <b>Price:</b> <input type="text" name="prod_price" size="25" value="" /><br/> <b>Description:</b> <input type="text" name="prod_desc" size="53" value="" /><br/> <b>Weight</b> <i>(in lbs):</i> <input type="text" name="prod_weight" size="4" value="" /> <b>Amount in Stock:</b> <input type="text" name="prod_stock" size="5" value="" /><br/> <input type="submit" name ="action" value="Add"><hr></form> <?php if ($_POST['prod_name'] >= '5') { echo "Product name is greater than or equal to 5<br/>"; } if (is_float($_POST['prod_price'])) { echo "Product price is a float<br/>"; } if (strlen($_POST['prod_weight']) < 4) { echo "Product weight is under 1,000lbs<br/>"; } echo $_POST['prod_price']."<br/>"; if(is_float($_POST['prod_name'])) { echo "Superglobal POST prod_name is a float"; } ?> Everything works, except for the is_float() function. It wont echo out "Product price is a float". Am I using it properly? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 24, 2012 Share Posted August 24, 2012 Text input fields pass the data as "text". In fact, I think all POST data is passed as text except for maybe file uploads. So, when you use if (is_float($_POST['prod_price'])) $_POST['prod_price'] is always a string - even if the string is '1.23'. Although PHP is a loosely typed language and you could use the string '1.23' in a match function, the is_float() function is specifically checking if the element has the value of a float AND is of a numeric type. is_float() Finds whether the type of a variable is float I think you want to be using is_numeric() Finds whether a variable is a number or a numeric string Quote Link to comment Share on other sites More sharing options...
gristoi Posted August 24, 2012 Share Posted August 24, 2012 your problem is due to the fact that the posted data is actually a string. so even though you have put in something like 2.55 it will actually be '2.55' which is a string. you can check it is numeric using is_numeric() . you can also check using a regular expression. Or type cast the value into a float : (float)$_POST['prod_price'] Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 24, 2012 Share Posted August 24, 2012 you can also check using a regular expression. Or type cast the value into a float : (float)$_POST['prod_price'] Regular Expression is a great tool, but it should only be used when there are no alternatives. Forcing the value to a float would work, but it means you would potentially be changing the value from what the user entered. If the user accidentally entered "1.2.3" and really meant to enter "1.23", then forcing the value to a float would end up with "1.2". I would rather capture the error and send back to the user to correct. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 25, 2012 Share Posted August 25, 2012 This should work fine for form field validation: if( ctype_digit(str_replace( '.', '', $num, $count) ) && $count < 2 ) { Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.