Guest User

Untitled

a guest
Aug 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. Is it possible to POST a fractional number from a form?
  2. <select name="inverse_num">
  3. <option value="2">2 </option>
  4. <option value="1">1 </option>
  5. <option value="1/2">1/2 </option>
  6. <option value="1/3">1/3 </option>
  7. </select>
  8.  
  9. $my_inverse=1/($_POST[inverse_num]); // returns 1 , but..
  10. $my_inverse=1/(1/2); // returns 2, which is required.
  11.  
  12. function simple_fractions ($value) {
  13. if (is_numeric($value)) {
  14. return $value;
  15. }
  16. preg_match('/^(d+)/(d+)$/', $value, $matches);
  17. if ($matches[1] && $matches[2]) {
  18. return $matches[0] / $matches[1];
  19. }
  20. if ($matches[1] == 0) {
  21. return 0;
  22. }
  23. return false;
  24. }
  25.  
  26. $my_inverse=1/($_POST[inverse_num]); // array keys shall be quoted
  27. $my_inverse=1/(1/2);
  28.  
  29. $my_inverse=1/"1/2";
  30. $my_inverse=1/(1/2);
  31.  
  32. <select name="inverse_num">
  33. <option value="a">2 </option>
  34. <option value="b">1 </option>
  35. <option value="c">1/2 </option>
  36. <option value="d">1/3 </option>
  37. </select>
  38.  
  39. if($_POST['inverse_num'] == 'a'){
  40. $value = 2;
  41. }elseif($_POST['inverse_num'] == 'b'){
  42. $value = 1;
  43. }elseif($_POST['inverse_num'] == 'c'){
  44. $value = 1/2;
  45. }elseif($_POST['inverse_num'] == 'd'){
  46. $value = 1/3;
  47. }
Add Comment
Please, Sign In to add comment