J
J
Julia Julia2014-07-27 10:27:37
PHP
Julia Julia, 2014-07-27 10:27:37

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();
}
}

when I type the URL webinse-test.loc/Webinseform.php
nothing comes up. No errors, no form.
What am I doing wrong?
Is it possible to display a simple form? Without creating a module?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Gerasimenko, 2014-07-27
@juliajune

> 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.

M
misheniata, 2014-11-30
@misheniata

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();

PS: Only form elements are wrapped in , and I would like to see them on a new line. Do you really need to finish drawing manually or is there some beautiful way?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question