how to redirect a page using magento button?
-
16-10-2019 - |
Question
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:
thanks in advance.
Solution
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 the
onclick` 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>