Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Jak wkleić kod kalkulatora?
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
neneth
Witam, mam pewien problem z kodem kalkulatora pobranego z pewnej strony
  1. <!-- ONE STEP TO INSTALL BODY CALCULATOR:
  2.  
  3. 1. Paste the coding into the BODY of your HTML document -->
  4.  
  5. <!-- STEP ONE: Copy this code into the BODY of your HTML document -->
  6.  
  7. <BODY>
  8.  
  9. <script LANGUAGE="JavaScript">
  10. <!-- Original: Tomer and Yehuda Shiran
  11.  
  12. <!-- This script and many more are available free online at -->
  13. <!-- The JavaScript Source!! [url="http://javascript.internet.com"]http://javascript.internet.com[/url] -->
  14.  
  15. <!-- Begin
  16. function getHeight(sex, age, height) {
  17. height = Math.round(height)
  18. var height5 = 0
  19. var height50 = 0
  20. if (age == 2) {
  21. height50 = 87
  22. height5 = 82
  23. } else
  24. if (age == 3) {
  25. height50 = 95
  26. height5 = 90
  27. } else
  28. if (age == 4) {
  29. height50 = 102
  30. height5 = 95
  31. } else
  32. if (age == 5) {
  33. height50 = 109
  34. height5 = 101
  35. } else
  36. if (age == 6) {
  37. height50 = 114
  38. height5 = 107
  39. } else
  40. if (age == 7) {
  41. height50 = 120
  42. height5 = 112
  43. } else
  44. if (age == 8) {
  45. height50 = 126
  46. height5 = 118
  47. } else
  48. if (age == 9) {
  49. height50 = 131
  50. height5 = 121
  51. } else
  52. if (age == 10) {
  53. height50 = 137
  54. height5 = 127
  55. } else
  56. if (age == 11) {
  57. height50 = 143
  58. height5 = 131
  59. } else
  60. if (sex == "f") {
  61. if (age == 12) {
  62. height50 = 150
  63. height5 = 140
  64. } else
  65. if (age == 13) {
  66. height50 = 157
  67. height5 = 145
  68. } else
  69. if (age == 14) {
  70. height50 = 160
  71. height5 = 148
  72. } else
  73. if (age == 15) {
  74. height50 = 162
  75. height5 = 150
  76. } else
  77. if (age == 16) {
  78. height50 = 162
  79. height5 = 151
  80. } else
  81. if (age == 17) {
  82. height50 = 163
  83. height5 = 153
  84. } else
  85. if (age == 18) {
  86. height50 = 164
  87. height5 = 154
  88. }
  89. } else
  90. if (age == 12) {
  91. height50 = 150
  92. height5 = 137
  93. } else
  94. if (age == 13) {
  95. height50 = 156
  96. height5 = 142
  97. } else
  98. if (age == 14) {
  99. height50 = 162
  100. height5 = 148
  101. } else
  102. if (age == 15) {
  103. height50 = 168
  104. height5 = 155
  105. } else
  106. if (age == 16) {
  107. height50 = 174
  108. height5 = 160
  109. } else
  110. if (age == 17) {
  111. height50 = 175
  112. height5 = 165
  113. } else
  114. if (age == 18) {
  115. height50 = 176
  116. height5 = 165
  117. }
  118. var percent = (height - height5) * (50 - 5) / (height50 - height5) + 5
  119. return percent
  120. }
  121. function getWeight(sex, age, weight) {
  122. weight = Math.round(weight)
  123. var weight5 = 0
  124. var weight50 = 0
  125. if (age == 2) {
  126. weight50 = 12
  127. weight5 = 10
  128. } else
  129. if (age == 3) {
  130. weight50 =14
  131. weight5 = 12
  132. } else
  133. if (age == 4) {
  134. weight50 = 16
  135. weight5 = 14
  136. } else
  137. if (age == 5) {
  138. weight50 = 18
  139. weight5 = 15
  140. } else
  141. if (age == 6) {
  142. weight50 = 20
  143. weight5 = 17
  144. } else
  145. if (age == 7) {
  146. weight50 = 22
  147. weight5 = 18
  148. } else
  149. if (age == 8) {
  150. weight50 = 25
  151. weight5 = 20
  152. } else
  153. if (age == 9) {
  154. weight50 = 29
  155. weight5 = 22
  156. } else
  157. if (sex == "f") {
  158. if (age == 10) {
  159. weight50 = 32
  160. weight5 = 25
  161. } else
  162. if (age == 11) {
  163. weight50 = 37
  164. weight5 = 27
  165. } else
  166. if (age == 12) {
  167. weight50 = 41
  168. weight5 = 30
  169. } else
  170. if (age == 13) {
  171. weight50 = 46
  172. weight5 = 34
  173. } else
  174. if (age == 14) {
  175. weight50 = 50
  176. weight5 = 38
  177. } else
  178. if (age == 15) {
  179. weight50 = 53
  180. weight5 = 40
  181. } else
  182. if (age == 16) {
  183. weight50 = 56
  184. weight5 = 43
  185. } else
  186. if (age == 17) {
  187. weight50 = 57
  188. weight5 = 45
  189. } else
  190. if (age == 18) {
  191. weight50 = 57
  192. weight5 = 46
  193. }
  194. } else
  195. if (age == 10) {
  196. weight50 = 24
  197. weight5 = 31
  198. } else
  199. if (age == 11) {
  200. weight50 = 35
  201. weight5 = 27
  202. } else
  203. if (age == 12) {
  204. weight50 = 40
  205. weight5 = 30
  206. } else
  207. if (age == 13) {
  208. weight50 = 46
  209. weight5 = 35
  210. } else
  211. if (age == 14) {
  212. weight50 = 51
  213. weight5 = 38
  214. } else
  215. if (age == 15) {
  216. weight50 = 57
  217. weight5 = 44
  218. } else
  219. if (age == 16) {
  220. weight50 = 62
  221. weight5 = 48
  222. } else
  223. if (age == 17) {
  224. weight50 = 67
  225. weight5 = 53
  226. } else
  227. if (age == 18) {
  228. weight50 = 69
  229. weight5 = 55
  230. }
  231. var percent = (weight - weight5) * (50 - 5) / (weight50 - weight5) + 5
  232. return percent
  233. }
  234. function printResult(height, weight, sex, age) {
  235. var heightAdj = ""
  236. var weightAdj = ""
  237. var ageAdj = ""
  238. var sexAdj = ""
  239. var gradeAdj = ""
  240. var grade = 0
  241. var propWeight = weight / height
  242. if (height > 70) {
  243. heightAdj = "tall"
  244. grade += 2
  245. } else
  246. if (height < 30) {
  247. heightAdj = "short"
  248. grade += 1
  249. } else {
  250. heightAdj = "medium-height"
  251. grade += 3
  252. }
  253. if (propWeight > 2) {
  254. weightAdj = "over-weight"
  255. grade += 1
  256. } else
  257. if (propWeight < 0.5) {
  258. weightAdj = "slim"
  259. grade += 2
  260. } else {
  261. weightAdj = "medium-weight"
  262. grade += 3
  263. }
  264. ageAdj = ageInput + "-year old"
  265. if (grade >= 5)
  266. gradeAdj = "great-looking"
  267. else
  268. if (grade <= 2)
  269. gradeAdj = "ackward-looking"
  270. else
  271. gradeAdj = "fine-looking"
  272. sexAdj = (sex == "f") ? "female" : "male"
  273. var finalMessage = "You are a " + heightAdj + ", "
  274. finalMessage += weightAdj + ", " + gradeAdj + " "
  275. finalMessage += ageAdj + " " + sexAdj + "."
  276. alert(finalMessage)
  277. if (confirm("Are you interested in scientific results?")) {
  278. scMessage = "height = " + Math.round(height)
  279. scMessage +="%\rweight = " + Math.round(weight)
  280. scMessage += "%\rweight/height = " + propWeight
  281. alert(scMessage)
  282. } else
  283. if (grade <= 4)
  284. alert("Good idea!")
  285. var notice = "Thank you for using the JavaScript weight "
  286. notice += "and height calculator. All calculations are "
  287. notice += "done according to the child-development graph "
  288. notice += "in \"Compton's Encyclopedia\". We appologize "
  289. notice += "if you were insulted by the comments -- that "
  290. notice += "was not our intention. We used them to demonstrate "
  291. notice += "various JavaScript scripting techniques."
  292. alert(notice)
  293. }
  294. function convertInput() {
  295. weightInput *= 0.45359
  296. heightInput *= 2.54
  297. }
  298. var sex = prompt("Enter sex ((m)ale or (f)emale):", "")
  299. var ageInput = parseInt(prompt("Enter age in years (minimum = 2):", ""))
  300. ageInput = Math.round(ageInput)
  301. var systemMessage = "Whould you like to use the (m)etric system "
  302. systemMessage += "or the (e)nglish one?"
  303. var system = prompt(systemMessage, "m")
  304. var heightUnit = (system == "m") ? "centimeters" : "inches"
  305. var weightUnit = (system == "m") ? "kilograms" : "pounds"
  306. var heightInput = prompt("Enter height in " + heightUnit + ":", "")
  307. heightInput = parseInt(heightInput)
  308. var weightInput = prompt("Enter weight in " + weightUnit + ":", "")
  309. weightInput = parseInt(weightInput)
  310. if (system == "e")
  311. convertInput()
  312. if (ageInput > 18)
  313. var age = 18
  314. else
  315. if (ageInput < 2)
  316. var age = 2
  317. else
  318. var age = ageInput
  319. var heightPer = getHeight(sex, age, heightInput)
  320. var weightPer = getWeight(sex, age, weightInput)
  321. heightPer = (heightPer < 1) ? 1 : heightPer
  322. heightPer = (heightPer > 99) ? 99 : heightPer
  323. weightPer = (weightPer < 1) ? 1 : weightPer
  324. weightPer = (weightPer > 99) ? 99 : weightPer
  325. printResult(heightPer, weightPer, sex, age)
  326. // End -->
  327. </SCRIPT>
  328.  
  329. <p><center>
  330. <font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
  331. by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
  332. </center><p>
  333.  
  334. <!-- Script Size: 6.35 KB -->
  335.  

Postępuje według indtrukcji, ale nie widać wyników. Proszę o radę, jak go wkleić do pliku .php , żeby był widoczny. Z góry dziękuję za odpowiedź.
patrix007
Po pierwsze - zły dział - tu nie ma nic napisane w PHP.

Po drugie brakuje znaczników html/head, znacznik body otwierasz a gdzie jest jego zamknięcie ?

Popraw błędy, kod trzymaj w pliku o rozszerzeniu html (np. kalkulator.html) i włącz obsługę Java Script w swojej przeglądarce i będzie działać - mi działa.
neneth
Witam, proszę o jakąś podpowiedź, bo ja chciałam zrobić przekierowanie tego pliku do strony głównej i nie wiem, jak to zrobić, żeby współgrało z php (wszyskie przekierowania do podstrony mam w php).
patrix007
Polecam jakiś kursik podstaw html na początek.

Uproszczony schemat:
  1. tu wklej ten skrypt
  2. </body>
  3. </html>


A co do przekierowania no to żadnego problemu tu nie powinno być... robisz:
  1. header('Location: kalkulator.html');

i już, wtedy nawet nie zauważysz, że to przekierowanie...

Albo
  1. echo"<a href=\"kalkulator.html\">Link do kalkulatora</a>";

Jak tak koniecznie chcesz w php no i w to trzeba najpierw kliknąć...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.