YigalB Posted February 1, 2012 Share Posted February 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/ Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313311 Share on other sites More sharing options...
Drongo_III Posted February 1, 2012 Share Posted February 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313312 Share on other sites More sharing options...
YigalB Posted February 1, 2012 Author Share Posted February 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313313 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 you can use round Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313317 Share on other sites More sharing options...
YigalB Posted February 1, 2012 Author Share Posted February 1, 2012 you can use round round is risky because of the gray area between rounding up and down. Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313324 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313332 Share on other sites More sharing options...
YigalB Posted February 1, 2012 Author Share Posted February 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313335 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256184-handling-a-form/#findComment-1313340 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.