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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.