Jump to content

[SOLVED] how do I ensure that numbers are entered into a variable?


kr3m3r

Recommended Posts

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

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'];

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.