0

I am new to magento, using html button I had create a record and redirect the pages also, when I have create magento button I can't do any operations

what is the difference b/w html and magento buttons? and how the perform action?

my code is:

<form method="post" action="<?php echo Mage::getUrl('practice/index/new') ?>">
    <div class="mycustomstyle">
        <table>
            <tr>
                <td>
                    <label for="nom">First Name</label>
                </td>
                <td>
                    <input type="text" id="nom" name="nom" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="prenom">Last Name</label>
                </td>
                <td>
                    <input type="text" id="prenom" name="prenom" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="nom">Telephone Number</label>
                </td>
                <td>
                    <input type="text" id="telephone" name="telephone" />
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: right;">
                    <input type="submit" value="Save" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="mycustomstyle">
                        <p><button type="button" title="<?php echo $this->__('Create a New Record') ?>" class="button btn-cart" onclick="<?php echo Mage::getUrl('practice/index/new');  ?>"><span><span><?php echo $this->__('Create a New Record') ?></span></span></button></p>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</form>

see image:

enter image description here

thanks in advance.

1 Answer 1

3

Starting of with the buttons. There is no technical difference between the buttons. It's only a styling matter. If you use the Magento button (and it's best to do so) you'll get a nicely styled button. Beyond that, both buttons do the same. The submit the form they're attached to.

How ever, make sure the type of the button is submit and not button.

Second, the redirect. The redirect should be done in the controller and not in an onclick event of the button. The flow is roughly something like this.

user fills out form and clicks button > controller handles the post data and saves it > controller redirects to desired end page

Your controller would look something like this

class [Namespace]_[Module]_IndexController
{
   public function indexAction()
   {
      [...]
      // page that displays your form
      [...]
   }

   public function postoneAction()
   {
      [...]
      // code that handles the post data
      [...]

      // redirect back to the index page, or any other page of your controller
      $this->_redirect('*/*/index');
   }

   public function posttwoAction()
   {
      [...]
      // code that handles the post data differently
      [...]

      // redirect back to the index page, or any other page of your controller
      $this->_redirect('*/*/index');
   }
}

[EDIT] to have 2 different types of 'submit', take a look at this post on Stackoverflow on swapping the action of the form depending on the button.

Basically you remove the action from the form, add a name to the form (myform for example) and add it to theonclick` of the buttons. Something like this.

<button onclick="document.myform.action='practice/index/postone';document.myform.submit();"> .... </button>

<button onclick="document.myform.action='practice/index/posttwo';document.myform.submit();"> .... </button>
7
  • hi sander, when I click it doesn't fire action my function is in indexController.php public function newAction() { $this->loadLayout(); $this->renderLayout(); $this->_redirect('practice/index/new'); } and phtml code is <p><button type="button" title="<?php echo $this->__('Create a New Record') ?>" class="button btn-cart" onclick="<?php echo Mage::getUrl('practice/index/new'); ?>"><span><span><?php echo $this->__('Create a New Record') ?></span></span></button></p> Oct 7, 2013 at 11:25
  • please remove the onclick event, maybe replace it with a form submit() event if you really want but that's not required. And make the button type submit. Also make sure the action` of the form directs to the controllers method that performs the saving of the data Oct 7, 2013 at 11:26
  • ok if I have multiple buttons in one page then how can I give multiple urls? give a example? Oct 7, 2013 at 11:40
  • It totally depends on what you want those buttons to do. But the form only performs one action on one URL per submit. You can't have it do both actions Oct 7, 2013 at 11:45
  • ok for example: in magento save and save and continue edit 2 buttons is there both actions are different but 2 buttons are same page! there is working, but when I had create 2 buttons it's not working? Oct 7, 2013 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.