Jump to content

Handling a form


YigalB

Recommended Posts

A form needs to get answers of math quiz, returning numbers, up to 2 digits after the point.

In all form examples I noticed the usage of: 

...<input type="text" name="name" /...

 

Cant it be a number? floating or integer? why "text" ?

 

Also, can I limit the form to accept numerical input only or would I need to learn Ajax for that?

 

Once I have the answer, I need to compare it to the correct answer. The method I have in mind is to multiply the answer and the correct one by 100 and compare the integer part. Is there a pre-made function that can do the same?

Link to comment
Share on other sites

there are several different types of inputs, the one that allows a user to fill in the field with whatever data if type='text' (assuming we aren't including textareas).

 

if you want to validate the user data in real-time before they submit, javascript would be required.

 

once the user submits, you can compare the data however you want and return whatever feedback you want. also, why not simply compare the floats?

Link to comment
Share on other sites

text doesn't necessarily mean it's going to be considered text by php.

 

You can specify the type as 'number' but this is a HTML 5 thing and won't have wide support.

 

Your best options are to either use jquery/javascript to validate at the client side (though this would fail if they didn't have JS turned on) or post your data to a validation function and use something like:

 


$yourinput = $_POST['yourinput'];
filter_var($yourinput, FILTER_VALIDATE_FLOAT)


...with a conditional to check the input validates to a float - i believe it returns true if it's valid.

 

If not then return the user to the form with the appropriate error.

 

 

 

Link to comment
Share on other sites

there are several different types of inputs, the one that allows a user to fill in the field with whatever data if type='text' (assuming we aren't including textareas).

 

if you want to validate the user data in real-time before they submit, javascript would be required.

 

once the user submits, you can compare the data however you want and return whatever feedback you want. also, why not simply compare the floats?

The reason I can;t compare the floats is that the "correct answer" may have endless digits after the point. I.e the correct answer is: 42.377764

I don't want the user to type all the least significant digits, so it seems to me that two are enough.

If I compare 42. 37 and 42.377764 it would be considered as different answer.

 

Link to comment
Share on other sites

you can use round

round is risky because of the gray area between rounding up and down.

 

how is rounding to the hundredths place any more risky then multiply the number by 100?

Maybe I am wrong, but if multiply by 100 and truncate after the dot, I get exactly the same digits as was in the source.

I am not sure what would round do if the exact result should be 14.055 and the user types 14.05. would it round the same?

 

One thing is sure - if I multiply both by 100 and truncate, the result would be 1405 for both.

Link to comment
Share on other sites

you can use round

round is risky because of the gray area between rounding up and down.

 

how is rounding to the hundredths place any more risky then multiply the number by 100?

Maybe I am wrong, but if multiply by 100 and truncate after the dot, I get exactly the same digits as was in the source.

I am not sure what would round do if the exact result should be 14.055 and the user types 14.05. would it round the same?

 

One thing is sure - if I multiply both by 100 and truncate, the result would be 1405 for both.

 

alright, don't listen to me, what do I know anyway, go for it.

 

multiply the numbers by 100, problem solved.

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.