{"id":809,"date":"2010-03-16T20:38:04","date_gmt":"2010-03-16T20:38:04","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=809"},"modified":"2010-03-17T21:39:51","modified_gmt":"2010-03-17T21:39:51","slug":"web-page-inputs-and-insert-into-database","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/16\/web-page-inputs-and-insert-into-database\/","title":{"rendered":"Web page inputs and insert into database"},"content":{"rendered":"<p>Also from the other posts, <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/03\/11\/join-files-together-into-a-single-file-and-expand-out\/\">Join files together<\/a> and <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/03\/11\/simple-calculator\/\">simple calculator<\/a> , I was also asked to do<\/p>\n<p>&#8220;Accept form input for a user registration form, and store results in a MySQL table, using PHP. Applicant should demonstrate a knowledge of input validation, and using server-side code to avoid sql injection exploits. The user data should include first name, last name, email, an phone number.<\/p>\n<p>Usage of Javascript pre-validation would be a plus, as would forward planning, i.e. adding a unique code to the user details that could be used to validate their email address. Suitable MySQL table schema should be demonstrated.&#8221;<\/p>\n<p>To start with, I started at the data insert within a web page<\/p>\n<pre lang=\"html4strict\">\r\n<body>\r\n<form name=\"input\" action=\"insertData.php\" method=\"post\" onSubmit=\"return checkInputs()\">\r\nFirst Name :\r\n<input type=\"text\" name=\"firstname\"\/>\r\nSecond Name : \r\n<input type=\"text\" name=\"secondname\"\/>\r\nEmail : \r\n<input type=\"text\" name=\"email\"\/>\r\nPhone number : \r\n<input type=\"text\" name=\"phonenumber\"\/>\r\n<input type=\"submit\" value=\"Submit\"\/>\r\n<\/form>\r\n<\/body><\/pre>\n<p>Which takes in the required input&#8217;s and also within the <a href=\"http:\/\/www.w3schools.com\/TAGS\/tag_form.asp\">form<\/a> HTML tag, but before sending to the back end PHP part of the exercise, there was a requirement to do some Javascript checking on the inputs.<\/p>\n<p>So here is the javascript that will check the inputs, within the form onsubmit action I call this function checkInputs and the return value (true\/false) is returned which gives the form either a action to post back to the server (true return) or to wait for the user correct there inputs (false return).<\/p>\n<pre lang=\"javascript\">\r\n    var firstname = document.getElementsByName(\"firstname\").item(0);\r\n<\/pre>\n<p>I get the data from the webpage, <a href=\"http:\/\/www.java2s.com\/Code\/JavaScriptReference\/Javascript-Methods\/getElementsByName.htm\">getElementsByName<\/a> (which since it is a name there could be x amount of elements with that name, so I want the first one (.item(0))<\/p>\n<pre lang=\"javascript\">\r\n  function checkInputs()\r\n  {\r\n    \/\/ obtain inputs\r\n    var firstname = document.getElementsByName(\"firstname\").item(0);\r\n    var secondname = document.getElementsByName(\"secondname\").item(0);\r\n    var email= document.getElementsByName(\"email\").item(0);\r\n    var phonenum = document.getElementsByName(\"phonenumber\").item(0);\r\n    \/* check the inputs *\/\r\n    if (lengthCheck(firstname, \"first name\")) \r\n      if (lengthCheck(secondname, \"second name\"))\r\n\tif (emailChecker(email))\r\n\t  if (checkPhone(phonenum))\r\n\t    return true;\r\n    return false;\r\n  }\r\n<\/pre>\n<p>and then after getting the elements, I then call different additional functions that I did write within javascript to check the inputs gained.  Here I check the length of a element passed within the one of the parameters within the function parameter list, with using objects you can access the objects value.length (javascript object of a element) and also use the focus function with the element which will focus the element on the webpage for the user to know where to check there input (also with a alert window to say why, e.g. &#8220;please insert some data&#8221;.)<\/p>\n<pre lang=\"javascript\">\r\n  function lengthCheck(elem, thename)\r\n  {\r\n    if (elem.value.length> 0) \r\n      return true;\r\n    else\r\n    {\r\n      alert(\"Please insert the \" + thename);\r\n      elem.focus();\r\n    }\r\n  }\r\n<\/pre>\n<p>Here is a way of using <a href=\"http:\/\/en.wikipedia.org\/wiki\/Regular_expression\">regular expression<\/a> to check email inputs, basically it first checks to make sure there is a name before the &#8220;@&#8221; and also a at between 2 and 4 names with a &#8220;.&#8221; better them.<\/p>\n<pre lang=\"javascript\">  \r\n  \/* check a email address, using regular expression *\/\r\n  function emailChecker(elem)\r\n  {\r\n    var reg = \/^[\\w\\-\\.\\+]+\\@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$\/;\r\n    if (elem.value.match(reg))\r\n      return true;\r\n    else\r\n    {\r\n      alert (\"Please insert a valid email address\");\r\n      elem.focus();\r\n      return false;\r\n    }\r\n  }\r\n<\/pre>\n<p>Here is a similar way as above for checking a phone number input between 11-15 numbers<\/p>\n<pre lang=\"javascript\">  \r\n  \/* check against a phone number. a number being between 11-15 numbers*\/\r\n  function checkPhone(elem)\r\n  {\r\n    var reg = \/^[0-9]{11,15}$\/;\r\n    if (elem.value.match(reg))\r\n      return true;\r\n    else\r\n    {\r\n      alert (\"Please insert a valid phone number\");\r\n      elem.focus();\r\n      return false;\r\n    }\r\n  }\r\n  <\/pre>\n<p>This is the web page part, and here is the second part where I insert the data into database with php.<\/p>\n<p>But here is the full web page part of the first part.<\/p>\n<pre lang=\"html4strict\">\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<script language=\"javascript\">\r\n  \/* check the length of the element, focus is none present *\/\r\n  function lengthCheck(elem, thename)\r\n  {\r\n    if (elem.value.length> 0) \r\n      return true;\r\n    else\r\n    {\r\n      alert(\"Please insert the \" + thename);\r\n      elem.focus();\r\n    }\r\n  }\r\n  \r\n  \/* check a email address, using regular expression *\/\r\n  function emailChecker(elem)\r\n  {\r\n    var reg = \/^[\\w\\-\\.\\+]+\\@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$\/;\r\n    if (elem.value.match(reg))\r\n      return true;\r\n    else\r\n    {\r\n      alert (\"Please insert a valid email address\");\r\n      elem.focus();\r\n      return false;\r\n    }\r\n  }\r\n  \r\n  \/* check against a phone number. a number being between 11-15 numbers*\/\r\n  function checkPhone(elem)\r\n  {\r\n    var reg = \/^[0-9]{11,15}$\/;\r\n    if (elem.value.match(reg))\r\n      return true;\r\n    else\r\n    {\r\n      alert (\"Please insert a valid phone number\");\r\n      elem.focus();\r\n      return false;\r\n    }\r\n  }\r\n  \r\n  function checkInputs()\r\n  {\r\n    \/\/ obtain inputs\r\n    var firstname = document.getElementsByName(\"firstname\").item(0);\r\n    var secondname = document.getElementsByName(\"secondname\").item(0);\r\n    var email= document.getElementsByName(\"email\").item(0);\r\n    var phonenum = document.getElementsByName(\"phonenumber\").item(0);\r\n    \/* check the inputs *\/\r\n    if (lengthCheck(firstname, \"first name\")) \r\n      if (lengthCheck(secondname, \"second name\"))\r\n\tif (emailChecker(email))\r\n\t  if (checkPhone(phonenum))\r\n\t    return true;\r\n    return false;\r\n  }\r\n<\/script>\r\n<\/head>\r\n<body>\r\n<form name=\"input\" action=\"insertData.php\" method=\"post\" onSubmit=\"return checkInputs()\">\r\nFirst Name :\r\n<input type=\"text\" name=\"firstname\"\/>\r\n\r\nSecond Name : \r\n<input type=\"text\" name=\"secondname\"\/>\r\n\r\nEmail : \r\n<input type=\"text\" name=\"email\"\/>\r\n\r\nPhone number : \r\n<input type=\"text\" name=\"phonenumber\"\/>\r\n\r\n<input type=\"submit\" value=\"Submit\"\/>\r\n<\/form>\r\n<\/body>\r\n<\/html><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Also from the other posts, Join files together and simple calculator , I was also asked to do &#8220;Accept form input for a user registration form, and store results in a MySQL table, using PHP. Applicant should demonstrate a knowledge of input validation, and using server-side code to avoid sql injection exploits. The user data &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/16\/web-page-inputs-and-insert-into-database\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Web page inputs and insert into database<\/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":[26,17,21],"tags":[147,148,149,146],"class_list":["post-809","post","type-post","status-publish","format-standard","hentry","category-html","category-php","category-sql","tag-data-insert","tag-database","tag-santize","tag-web-page"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/809","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=809"}],"version-history":[{"count":4,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/809\/revisions"}],"predecessor-version":[{"id":821,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/809\/revisions\/821"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}