Answer the question
In order to leave comments, you need to log in
How to create a custom form in Magento?
Good afternoon!
I need to create a form with fields using Varien_Data_Form and Varien_Data_Forrm_Element_*
Form fields
- name
- email
- firstname
- lastname
- password
- confirm_password
File and folder structure of my project
webinse_test.loc - my project folder
-index.php - magento index file
...
... magento files and folders
...
-Webinseform.php - the file with my form
-Webinseprocess.php - the file where the data from the form will come and the
contents of Webinseform.php will be written to the database
<?php
include_once 'app/Mage.php';
Mage::init();
class Webinseform {
public function _conctruct(){
$form= new Varien_Data_Form(array(
'id'=> 'webinseform',
'action'=> $this->getUrl('webinse-test.loc/Webinseprocess.php',
array('id'=> $this- >getRequest()->getParam('id'))),
'method'=> 'post',
'enctype'=> 'multipart/form-data'));
$form->addField('name','text', array('label'=>'Name'));
$form->addField('email','text',array('label'=>'Email'));
$form->addField('firstname','text',array('label'=>'Firstname'));
$form->addField('lastname','text',array('label'=>'Lastname'));
$form->addField('password','text',array('label'=>'Password'));
$form->addField('confirm_password','text',array('label'=>'Confirm_password'));
echo $form->toHtml();
}
}
Answer the question
In order to leave comments, you need to log in
> Whether it is possible to deduce simply the form? Without creating a module?
Yes, you can.
> What am I doing wrong?
Your class is declared, but an instance of the class is not created anywhere (respectively, the constructor is not called). Add to the end of the Webinseform.php file
Or another option. Move the code from the constructor directly... to the stream code of the file, i.e. get rid of the Webinseform class, which is questionable in this context.
It was necessary to declare a variable in the class, which is initialized in the constructor. And create a class object in flow code:
<?php
include_once 'app/Mage.php';
Mage::init();
class Webinseform {
public $form;
public function __construct() {
$this->$form = new Varien_Data_Form(array(
'id'=> 'webinseform',
'action'=> $this->getUrl('webinse-test.loc/Webinseform.php '),
array('id'=> $this->getRequest()->getParam('id'))),
'method'=> 'post',
'enctype'=> 'multipart/form-data'));
$form->addField('name','text', array('label'=>'Name'));
$form->addField('email','text',array('label'=>'Email'));
$form->addField('firstname','text',array('label'=>'Firstname'));
$form->addField('lastname','text',array('label'=>'Lastname'));
$form->addField('password','text',array('label'=>'Password'));
$form->addField('confirm_password','text',array('label'=>'Confirm_password'));
echo $form->toHtml();
}
}
$form1 = new Webinseform();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question