kr3m3r Posted July 15, 2007 Share Posted July 15, 2007 Hi guys, thanks for reading this and any help you can give me. I'm new to programming so I appologize if this is a goofy question. I have a page where a customer can input the quanitiy of items they wish to buy of four different things: tires, spark plugs, oil, and engine blocks. I'm trying to ensure that the visitors only put integers into the text boxes but i keep getting errors. Here is my php code that is problematic: $tiretype=gettype($HTTP_POST_VARS['tireqty']); $oiltype=gettype($HTTP_POST_VARS['oilqty']); $sparktype=gettype($HTTP_POST_VARS['sparkqty']); $enginetype=gettype($HTTP_POST_VARS['engineqty']); if(is_int(tiretype)==false || is_int(oiltype'])==false || is_int(sparktype'])==false || is_int(enginetype'])==false) { echo '<p>'; echo '<font color=red>'; echo '<h1>'; echo 'You did not enter numbers!'; echo '</h1>'; echo '</font>'; echo '</p>'; } Thanks again for the help, -Robb Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/ Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 Use is_numeric() instead of is_int() if(!is_numeric($tiretype) || !is_numeric($oiltype) || !is_numeric($sparktype) || !is_numeric($enginetype)) Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-298944 Share on other sites More sharing options...
kr3m3r Posted July 15, 2007 Author Share Posted July 15, 2007 I tried posting that and now it says all input is not a number. If it helps the relavent html code is: td align="center"><input type="text" name="sparkqty" size="3" maxlength="3"> Thanks -Robb Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-298964 Share on other sites More sharing options...
lur Posted July 15, 2007 Share Posted July 15, 2007 gettype() returns a string, thus: $variable = gettype($something); is_int($variable); // always FALSE; $tiretype = $HTTP_POST_VARS['tireqty']; if (FALSE === is_numeric($tiretype)) { // it's not numeric. } Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-298969 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 Change this: $tiretype=gettype($HTTP_POST_VARS['tireqty']); $oiltype=gettype($HTTP_POST_VARS['oilqty']); $sparktype=gettype($HTTP_POST_VARS['sparkqty']); $enginetype=gettype($HTTP_POST_VARS['engineqty']); To: $tiretype = $_POST['tireqty']; $oiltype = $_POST['oilqty']; $sparktype = $_POST['sparkqty']; $enginetype = $_POST['engineqty']; Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-298971 Share on other sites More sharing options...
xyn Posted July 15, 2007 Share Posted July 15, 2007 preg_match(); Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-298982 Share on other sites More sharing options...
GingerRobot Posted July 15, 2007 Share Posted July 15, 2007 Using regular expressions to check for numeric data is a bit unnecessary. There are quicker methods. Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-298987 Share on other sites More sharing options...
kr3m3r Posted July 15, 2007 Author Share Posted July 15, 2007 If I may ask, what are the quicker methods? I've just started working my way through my PHP book, and I've been trying to use what has been brought up so far. The book suggested as a challenge to myself I try to implement some sort of error protection. So I'd love to know more. Thanks to everyone for replying so far, I really appreciate it. -Robb ps what does "preg_match();" refer to? Thanks again! Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-299041 Share on other sites More sharing options...
drewbee Posted July 15, 2007 Share Posted July 15, 2007 Please note that is_numeric allows floating numbers to be entered as well. Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-299091 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 Give this a try: <?php if (!is_numeric($tiretype) || strstr($tiretype, '.')){ echo "Bad Input!"; } else { echo "Good Input!"; } ?> Link to comment https://forums.phpfreaks.com/topic/60100-solved-how-do-i-ensure-that-numbers-are-entered-into-a-variable/#findComment-299097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.