{"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 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 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=IQoJb3JpZ2luX2VjEMn%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJHMEUCIQDyTuSdHYwi6yyjVqwhBDeBQXvmHq4xYwwZUv%2BqHlQffgIgQogEL%2Ft%2BM4nUHcxwuomrAmEJFgYgAiyL4vA6foVaI4sqwgUIkf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAFGgwyNzM1NzAxOTU0MzMiDCjZ5wNguqigR7Z%2F%2ByqWBfY9clQj6H1Q8gdc8vNXu%2Bu4ASWOYX0woG7nYi9eJy0YASAACe%2B9ghk8o0Kxi1P2hv1qlngZJlAZ6UXtNeCIIKaCV8XlOIdBD%2FJV5wUbhvqugkCTX1xrjhLrwXSqntbpZZzOUkRLeiu5UR2eBgHbyrdDnghzh7%2BhwnCewcYoRmQZ1JcZ8G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J2%2F20260820%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260820T164910Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=46aa3951f838a7b048e17e08bac0443a3cf6510984529251e9b93c036f35f377\" 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}]}}