{"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=IQoJb3JpZ2luX2VjEF8aCWV1LXdlc3QtMSJHMEUCIQCM8hGYyh5osUpEds5xsKa9XRt1gsfwdTbvJN3KtC9RlwIgV9KhwJ3iUZwclZgk4WEFfwo0P0Vf%2B1wOZZR3beeNkeQquQUIKBAFGgwyNzM1NzAxOTU0MzMiDMi79bcXpe%2Fjb8ejMCqWBfUzDC9UX%2Bpjm1avtZxpDS%2F8exiOTKkjnWBD6YNwTUMRXBAoasy65TdHQ08X2mvcXBWwiJrfAt8NrVVYKon%2BnNl2ZAOS2%2BxhDz6v8gORmgpOFE9XjaycoSoMRETxWLTWDE9ew4g%2FbwtWsFiK4GFc8VwtEWDE1u5Y9w3v0xTToMc63hkcU1VO0yg6CwnfQdHBNW6qGvJndu61fD6028kA2ZxPN9vnk%2B5NwfBlr94dSJEnG6P9lJj%2BtKxthW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20260725T232103Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=b968b7bc7252a94612944208874a0923f1c0bf7f3c526850622ef0f5b9b5300a\" 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}]}}