{"id":4513,"date":"2019-08-13T14:26:52","date_gmt":"2019-08-13T14:26:52","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=4513"},"modified":"2019-08-14T13:38:38","modified_gmt":"2019-08-14T13:38:38","slug":"create-a-frontend-form","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/08\/13\/create-a-frontend-form\/","title":{"rendered":"Create a Frontend Form"},"content":{"rendered":"<p>Read: https:\/\/magento.stackexchange.com\/questions\/222423\/how-to-create-a-form-and-save-that-data-in-database-in-magento-2\/222427<br \/>\nCreate a directory Advcha\/MyForm in your magento root project\/app\/code<br \/>\nThen create a new file registration.php:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Advcha_MyForm',\n    __DIR__\n);<\/pre>\n<p>Then in etc\/module.xml:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\n    &lt;module name=\"Advcha_MyForm\" setup_version=\"1.0.0\"&gt;\n    &lt;\/module&gt;\n&lt;\/config&gt;<\/pre>\n<p>Then do this to make sure it&#8217;s okay.<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento module:enable Advcha_MyForm &amp;&amp; php bin\/magento setup:upgrade<\/pre>\n<p>Create a frontend router in etc\/frontend\/routes.xml:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\"&gt;\n    &lt;router id=\"standard\"&gt;\n        &lt;route id=\"myform\" frontName=\"myform\"&gt;\n            &lt;module name=\"Advcha_MyForm\"\/&gt;\n        &lt;\/route&gt;\n    &lt;\/router&gt;\n&lt;\/config&gt;<\/pre>\n<p>I want the url to show the form is \/myform\/myform\/index<br \/>\nSo create a dir and file in Controller\/MyForm\/Index.php. But I need to create a layout first in view\/frontend\/layout\/myform_myform_index.xml:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\"?&gt;\n&lt;page layout=\"2columns-left\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\n    &lt;head&gt;\n        &lt;title&gt;Advcha MyForm&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;referenceContainer name=\"content\"&gt;\n            &lt;block class=\"Advcha\\MyForm\\Block\\MyForm\" name=\"myform.myform\" template=\"Advcha_MyForm::myform.phtml\"\/&gt;\n        &lt;\/referenceContainer&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n<p>Then create a block in Block\/MyForm.php:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\nnamespace Advcha\\MyForm\\Block;\n \nclass MyForm extends \\Magento\\Framework\\View\\Element\\Template\n{\n    \/**\n     * Construct\n     *\n     * @param \\Magento\\Framework\\View\\Element\\Template\\Context $context\n     * @param array $data\n     *\/\n    public function __construct(\n        \\Magento\\Backend\\Block\\Template\\Context $context,\n        array $data = []\n    )\n    {\n        parent::__construct($context, $data);\n       }\n\n    \/**\n     * Get form action URL for POST booking request\n     *\n     * @return string\n     *\/\n    public function getFormAction()\n    {\n        \/\/ companymodule is given in routes.xml\n        \/\/ controller_name is folder name inside controller folder\n        \/\/ action is php file name inside above controller_name folder\n\n        return '\/myform\/myform\/index';\n        \/\/ here controller_name is myform, action is index\n    }\n}<\/pre>\n<p>Then create the template in view\/frontend\/templates\/myform.phtml:<\/p>\n<pre class=\"lang:default decode:true \">&lt;h1&gt;Advcha MyForm Page&lt;\/h1&gt;\n\n&lt;form action=\"&lt;?php echo $block-&gt;getFormAction() ?&gt;\" method=\"post\"&gt;\n    &lt;input name=\"firstname\" type=\"text\"&gt;\n    &lt;input name=\"lastname\" type=\"text\"&gt;\n    &lt;input name=\"phone\" type=\"text\"&gt;\n    &lt;input name=\"bookingTime\" type=\"date\"&gt;\n    &lt;input type=\"submit\" value=\"Send informations\"&gt;\n&lt;\/form&gt;<\/pre>\n<p>Then here is the controller in Controller\/MyForm\/Index.php:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\n\nnamespace Advcha\\MyForm\\Controller\\MyForm;\n\nuse Magento\\Framework\\Controller\\ResultFactory;\n\nclass Index extends \\Magento\\Framework\\App\\Action\\Action\n{\n    \/**\n     * Booking action\n     *\n     * @return void\n     *\/\n    public function execute()\n    {\n        \/\/ 1. POST request : Get booking data\n        $post = (array) $this-&gt;getRequest()-&gt;getPost();\n\n        if (!empty($post)) {\n            \/\/ Retrieve your form data\n            $firstname   = $post['firstname'];\n            $lastname    = $post['lastname'];\n            $phone       = $post['phone'];\n            $bookingTime = $post['bookingTime'];\n\n            \/\/ Doing-something with...\n\n            \/\/ Display the succes form validation message\n            $this-&gt;messageManager-&gt;addSuccessMessage('Send info done !');\n\n            \/\/ Redirect to your form page (or anywhere you want...)\n            $resultRedirect = $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_REDIRECT);\n            $resultRedirect-&gt;setUrl('\/myform\/myform\/index');\n\n            return $resultRedirect;\n        }\n        \/\/ 2. GET request : Render the booking page \n        $this-&gt;_view-&gt;loadLayout();\n        $this-&gt;_view-&gt;renderLayout();\n    }\n}<\/pre>\n<p>DONE! PLS RUN<\/p>\n<pre class=\"lang:default decode:true\">php bin\/magento setup:upgrade &amp;&amp; php bin\/magento setup:static-content:deploy -f en_AU en_US --theme SCM\/Minimal &amp;&amp; php bin\/magento c:f<\/pre>\n<p>CHECK THE PAGE IN URL LIKE: http:\/\/scm.test\/myform\/myform\/index<br \/>\nPLS TRY TO FILL THE FORM THEN PRESS THE SUBMIT BUTTON!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read: https:\/\/magento.stackexchange.com\/questions\/222423\/how-to-create-a-form-and-save-that-data-in-database-in-magento-2\/222427 Create a directory Advcha\/MyForm in your magento root project\/app\/code Then create a new file registration.php: &lt;?php \\Magento\\Framework\\Component\\ComponentRegistrar::register( \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE, &#8216;Advcha_MyForm&#8217;, __DIR__ ); Then in etc\/module.xml: &lt;?xml version=&#8221;1.0&#8243;?&gt; &lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:framework:Module\/etc\/module.xsd&#8221;&gt; &lt;module name=&#8221;Advcha_MyForm&#8221; setup_version=&#8221;1.0.0&#8243;&gt; &lt;\/module&gt; &lt;\/config&gt; Then do this to make sure it&#8217;s okay. php bin\/magento module:enable Advcha_MyForm &amp;&amp; php bin\/magento setup:upgrade Create a frontend &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/08\/13\/create-a-frontend-form\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Create a Frontend Form&#8221;<\/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":[71,98,13],"tags":[],"class_list":["post-4513","post","type-post","status-publish","format-standard","hentry","category-magento","category-magento-tutorial","category-tutorial"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=4513"}],"version-history":[{"count":5,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4513\/revisions"}],"predecessor-version":[{"id":4532,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4513\/revisions\/4532"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=4513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=4513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=4513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}