{"id":386,"date":"2021-06-10T13:35:30","date_gmt":"2021-06-10T12:35:30","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/?page_id=386"},"modified":"2021-06-10T15:46:55","modified_gmt":"2021-06-10T14:46:55","slug":"processing-form-data","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/processing-form-data\/","title":{"rendered":"Processing Form Data"},"content":{"rendered":"<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-196\" src=\"https:\/\/blogs.glowscotland.org.uk\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/05211152\/sqa-ah.png\" alt=\"\" width=\"100\" height=\"97\" \/><a href=\"https:\/\/www.w3schools.com\/pHp\/php_forms.asp\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"image-59 alignright\" src=\"https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/03210120\/w3schools-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&#038;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEMT%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJGMEQCIFIuagJ%2FQbsh6cJaMwxxX26KpyAoAgPrYqCigWyoOv%2B5AiB7WZPdXsqs%2Fi%2Bmu5YCNfh48i%2B67qkp435SEsAUxInvzirABQiN%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAUaDDI3MzU3MDE5NTQzMyIMJTyQuVvRf9Jge%2FeDKpQFz5Cc9c1q02tUEX5LxaEp%2F%2F1ajRx9LvgS3wKuFWThN5opxKrYkwBv9hrpUDwj9DB2NjkX9H3suTRhZR5yWxdaXPtupByCZQCjDdjqs0SA%2F1OnO2KGge%2BhtwnAJnkqpBiO2V4ygXkvG1Qh1RqtNjjR625o7DaT16ySkN1Q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#038;X-Amz-Credential=ASIAT7MQN47UVPVFLOWC%2F20260516%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260516T121546Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=1881e0dc5210f6de7007981923da9f576cf0bf643f0cfac3e11e05bd25cd8966\" alt=\"\" width=\"50\" height=\"47\" \/><\/a>Reading Form Data<\/h1>\n<p>Data from HTMl forms can be sent using GET or POST methods.<\/p>\n<p>PHP needs to use the correct commands to match the method used.<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$forename = $_POST&#x5B;&quot;firstname&quot;];\r\n$surname = $_POST&#x5B;&quot;lastname&quot;];\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$forename = $_GET&#x5B;&quot;firstname&quot;];\r\n$surname = $_GET&#x5B;&quot;lastname&quot;];\r\n<\/pre>\n<p>Either method can be used by checking the method used:<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nif ($_SERVER&#x5B;&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {\r\n    $forename = $_POST&#x5B;&quot;firstname&quot;];\r\n    $surname = $_POST&#x5B;&quot;lastname&quot;];\r\n} elseif ($_SERVER&#x5B;&quot;REQUEST_METHOD&quot;] == &quot;GET&quot;) {\r\n    $forename = $_GET&#x5B;&quot;firstname&quot;];\r\n    $surname = $_GET&#x5B;&quot;lastname&quot;];\r\n}\r\n<\/pre>\n<h1>Checking for Existence of values<\/h1>\n<p>HTML forms only send data that has been entered, or given default values.<\/p>\n<p>A form may have optional data input items, and trying to read these will cause the PHP program to stop. If this is a possibility, then check that the values exist first:<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nforename = &quot;&quot;;\r\nif (isset($_POST&#x5B;&quot;firstname&quot;])) {\r\n    $forename = cleanInput($_POST&#x5B;&quot;firstname&quot;]); \r\n}\r\n<\/pre>\n<p>This can be written, at the expense of readability as:<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$forename = isset($_GET&#x5B;'firstname']) ? $_GET&#x5B;'firstname'] : '';\r\n<\/pre>\n<h1>Pre-Processing Form Data<\/h1>\n<p>Form data will often used to create SQL queries, with the potential for SQL injection attacks.<\/p>\n<p>Although not a requirement for the AH course, clearing form data of any unwanted characters may be useful for project work.<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nfunction cleanInput($data) {\r\n    $data= trim($data);\r\n    $data = stripslashes($data);\r\n    $data = htmlspecialcharacters($data);\r\n    return $data\r\n}\r\n \r\n$forename = cleanInput($_POST&#x5B;&quot;firstname&quot;]);\r\n$surname = cleanInput($_POST&#x5B;&quot;lastname&quot;]);\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reading Form Data Data from HTMl forms can be sent using GET or POST methods. PHP needs to use the correct commands to match the method used. $forename = $_POST&#x5B;&quot;firstname&quot;]; $surname = $_POST&#x5B;&quot;lastname&quot;]; $forename = $_GET&#x5B;&quot;firstname&quot;]; $surname = $_GET&#x5B;&quot;lastname&quot;]; Either method can be used by checking the method used: if ($_SERVER&#x5B;&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) { $forename = $_POST&#x5B;&quot;firstname&quot;]; $surname = $_POST&#x5B;&quot;lastname&quot;]; } elseif ($_SERVER&#x5B;&quot;REQUEST_METHOD&quot;] == &quot;GET&quot;) { $forename = $_GET&#x5B;&quot;firstname&quot;]; $surname<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/processing-form-data\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":343,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-386","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":9,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/386\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/386\/revisions\/423"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/343"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/media?parent=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}