{"id":1149,"date":"2010-08-11T16:31:50","date_gmt":"2010-08-11T15:31:50","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1149"},"modified":"2010-08-11T16:43:16","modified_gmt":"2010-08-11T15:43:16","slug":"cs71-ass1-finance-part-2-javascript-and-common-php-page","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/08\/11\/cs71-ass1-finance-part-2-javascript-and-common-php-page\/","title":{"rendered":"CS71 &#8211; Ass1 &#8211; Finance &#8211; Part 3 &#8211; javascript and common php page"},"content":{"rendered":"<p><span id=\"zipfile\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/08\/cs75-ass1.zip\"><\/a><\/span>I am doing the <a href=\"http:\/\/www.harvard.edu\/\">Harvards<\/a> building dynamic websites called <a href=\"http:\/\/www.cs75.net\/\">CS-75<\/a> (also could be called E-75), because someone told me about it and I just thought might as well, it is all learning \ud83d\ude42 even if allot of it you may already know.<\/p>\n<p>Before any of the actual pages start, they will all call this php file so that it will build up the classes and connect to the database, so to start with need to pull in there classes and also start the session.  The two functions are the HTML header and footer parts of each page, so that do not have to alter every page to just add in another style as such, the last part is when we are creating the objects for the database\/users\/stocks.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n\trequire(\"database.php\");\r\n\trequire(\"getstocks.php\");\r\n\trequire(\"user.php\");\r\n\t\r\n\tsession_start();\r\n\t\r\n\tfunction HTMLHeader($titleTag,$loggedIn = true)\r\n\t{\r\n?>\r\n<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/strict.dtd\"> \r\n<html>\r\n  <head>\r\n    <title><?php echo $titleTag;?><\/title>\r\n\t<link href=\"style.css\" rel=\"stylesheet\" type=\"text\/css\"\/>\r\n\t<script src=\"ass1.js\" language=\"javascript\"><\/script>\r\n  <\/head>\r\n <body>\r\n<div id=\"mainpage\"> \r\n\t<ul id=\"menuoptions\">\r\n\t<?php if ($loggedIn) { ?>\r\n\t<li><a href=\"viewdetails.php\">View your stock<\/a><\/li>\r\n\t<li><a href=\"buystock.php\">Buy stock<\/a><\/li>\r\n\t<li><a href=\"index.php?logout=true\">Logout<\/a><\/li>\r\n\t<li><a href=\"changepassword.php\">Change password<\/a><\/li>\r\n\t<?php } else { ?>\r\n\t<li><a href=\"index.php\">Login<\/a><\/li>\r\n\t<li><a href=\"register.php\">Register<\/a><\/li>\r\n\t<li><a href=\"forgotten.php\">Forgotten password<\/a><\/li>\r\n\t<?php } ?>\r\n\t<\/ul>\r\n<div id =\"container\">\r\n<?php\r\n\t}\r\n\t\r\n\tfunction HTMLFooter()\r\n\t{\r\n?>\r\n<\/div>\r\n<\/div>\r\n<\/body>\r\n<\/html>\r\n<?php\r\n\t}\r\n\t\r\n\t$db= new mysqlConnect(\"localhost\", \"username\",\"password\", \"cs75_project1\");\r\n\t$theStock = new StocksDetails();\r\n\t$theUser = new User();\r\n?><\/pre>\n<p>Because in the HTMLHeader function I am linking to the ass1.js file, which is the javascript code that will allow for a better user experience because we can do some input tests before sending to the server.<\/p>\n<p>This is the function that checks the users input when they are trying to sell some of the stock that they have<\/p>\n<pre lang=\"javascript\">\r\nfunction checkSell()\r\n{\r\n\tvar formElem = document.forms[0];\r\n\tvar elemsEmpty =0;\r\n\tfor (var i = 0; i < formElem.length; i++)\r\n\t{\r\n\t\tvar elem = formElem[i];\r\n\t\tif (elem.type == \"checkbox\" &#038;&#038; elem.checked)\r\n\t\t\telemsEmpty++;\r\n\t}\r\n\tif (elemsEmpty ==0)\r\n\t{\r\n\t\talert(\"Please select a item to sell\");\r\n\t\treturn false;\r\n\t}\r\n\treturn true;\r\n}\r\n<\/pre>\n<p>When the user is on the buying stock page, it would be good to make sure that the value is a integer value before the user actually sends the request to the server, it will also check to make sure that there is a symbol to search for, saves on just clicking on the submit without any data.<\/p>\n<pre lang=\"javascript\">\r\nfunction CheckBuy()\r\n{\r\n\tvar symbol = document.getElementById(\"searchSymbol\");\r\n\tvar amount = document.getElementById(\"AMOUNT\");\r\n\t\r\n\tif (symbol.value.length < 3 &#038;&#038; amount == null)\r\n\t{\r\n\t\talert(\"Please insert a search symbol\");\r\n\t\tsymbol.focus();\r\n\t\treturn false;\r\n\t}\r\n\t\r\n\tif (amount != null &#038;&#038; amount.length >0)\r\n\t{\r\n\t\tif (isNaN(amount) && amount.value > 0)\r\n\t\t{\r\n\t\t\tif (!document.getElementById(\"BUYME\").checked)\r\n\t\t\t{\r\n\t\t\t\talert(\"Please select the tick box next to the amount\");\r\n\t\t\t\treturn false;\r\n\t\t\t}\t\r\n\t\t}\r\n\t\telse \r\n\t\t{\r\n\t\t\talert(\"Please enter a valid amount to buy\");\r\n\t\t\tdocument.getElementById(\"AMOUNT\").focus();\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\treturn true;\r\n}\r\n<\/pre>\n<p>This function actually checks the users input on the amount of stock to buy and making sure that it is only numeric values.<\/p>\n<pre lang=\"javascript\">\r\n\/\/ only allow integer values within the box\r\nfunction CheckKey(boxToCheck)\r\n{\r\n\tvar boxValue=\"\";\r\n\tfor (i = 0; i < boxToCheck.value.length; i++)\r\n\t{\r\n\t\tif (!isNaN(boxToCheck.value[i]))\r\n\t\t\tboxValue += boxToCheck.value[i];\r\n\t}\r\n\tboxToCheck.value = boxValue;\r\n}\r\n<\/pre>\n<p>This function, checks to make sure that the registration screen has a valid email address (does another check on the server if the user turns off javascript), and also checks the password for the correct format.<\/p>\n<pre lang=\"javascript\">\r\nfunction CheckRegister(justUsername)\r\n{\r\n\tvar username = document.getElementById(\"username\");\r\n\tvar password = document.getElementById(\"password\");\r\n\tif (username.value.match(\/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+\/)) {\r\n\t\tif (justUsername)\r\n\t\t\treturn true;\r\n\t\treturn CheckPassword(password);\r\n    }else{   \r\n\t\talert(\"Incorrect email format\"); \r\n    }\r\n\treturn false;\r\n}\r\n<\/pre>\n<p>This the actual function that will check the password is in the correct format, e.g. 1+ numeric\/alpha characters. and more than 6 in length.<\/p>\n<pre lang=\"javascript\">\r\nfunction CheckPassword(password)\r\n{\r\n\tif (password.value.length >=6) {\r\n\t\tvar addLet = 0;\r\n\t\tfor (var i =0; i < password.value.length; i++) {\r\n\t\t\tif (isNaN(password.value[i]))\r\n\t\t\t\taddLet++;\r\n\t\t}\r\n\t\tif (addLet == password.value.length)\r\n\t\t{\r\n\t\t\talert(\"Please enter a password that contains at least 1 aplha\/numeric character\");\r\n\t\t} else\r\n\t\t\treturn true;\r\n\t} else\r\n\t\talert(\"Please enter a password that is at least 6 characters long\");\r\n\treturn false;\r\n}\r\n<\/pre>\n<p>Here on the change password, I am making sure that the first password conforms to the above function check and also that the two passwords are the same in value.<\/p>\n<pre lang=\"javascript\">\r\nfunction CheckChangePasswords()\r\n{\r\n\tif (CheckPassword(document.getElementById(\"password1\"))) \r\n\t{\r\n\t\tif (document.getElementById(\"password1\").value == document.getElementById(\"password2\").value)\r\n\t\t\treturn true;\r\n\t\telse\r\n\t\t\talert(\"Passwords do not match\");\r\n\t}\r\n\treturn false;\r\n}<\/pre>\n<p>Next is the actual php pages that the user will interact with.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am doing the Harvards building dynamic websites called CS-75 (also could be called E-75), because someone told me about it and I just thought might as well, it is all learning \ud83d\ude42 even if allot of it you may already know. Before any of the actual pages start, they will all call this php &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/08\/11\/cs71-ass1-finance-part-2-javascript-and-common-php-page\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CS71 &#8211; Ass1 &#8211; Finance &#8211; Part 3 &#8211; javascript and common php page<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17],"tags":[246,249,271],"class_list":["post-1149","post","type-post","status-publish","format-standard","hentry","category-javascript","category-php","tag-assignment-1","tag-cs75","tag-finance"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1149"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1149\/revisions"}],"predecessor-version":[{"id":1155,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1149\/revisions\/1155"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}