EDIT: I managed to do it. Here is the solution if it can help :
First the .phtml form:
<form id="form-configurator" class="form-configurator " name="formconfigurator" action="" method="post" enctype="multipart/form-data">
<div class="item" id="item-1">
<label class="conf_label">BlaBla</label>
<div class="inputselect">
<select class="selector" name="name1" id="input-1"><option value="2" selected="selected">2</option><option value="4">4</option></select>
</div>
</div>
<div class="item" id="item-2">
<label class="conf_label">Quantity :</label>
<div class="inputselect">
<input id="input-quantity" name="quantity" type="number" value="1" min="1" max="50" style="width: 63px;" maxlength="2" class="input-text required-entry"></input>
</div>
</div>
<strong><span class="devis_Nb" id="Devis_totalF"></span></strong>
</form>
Th JS/ajax script (which send data to php controller and treat the answer):
<script type="text/javascript">
//<![CDATA[
(function($) {
function doAjax() {
$.ajax({
url : "<?php echo Mage::getBaseUrl().'mymodule/index/mymethode' ?>",
type: "POST",
data : $("#form-configurator").serializeArray(),
dataType: 'json',
success: function(data)
{
console.log(data);
$("#Devis_totalF").html(data['total_price']);
},
error: function ()
{
}
});
}
// for number type input scan
$('input[type=number]').change(function() {
doAjax();
}
});
// for selectinput scan
$("select").change(function() {
doAjax();
});
//To run script at page load
doAjax();
})(jQuery);
//]]>
</script>
and the controller.php file:
<?php
class Mine_mymodule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function mymethodeAction()
{
//if ($post = $this->getRequest()->getPost()) {
$output = array();
$output = array('total_price' => 10);
echo json_encode($output);
}
}
?>
In php this type of work implement on session variable.
Magento is doing by
set success message
using ->addSuccess('YOUR MSG');
.
and set error message
using ->addError('YOUR ERROR MSG')
Those magento functions are Object of
Mage::getSingleton('core/session'
)
Mage::getSingleton('customer/session')
Mage::getSingleton('catalog/session')
So you can set this message.
Mage::getSingleton('core/session')->addSuccess('YOUR MSG');
Mage::getSingleton('customer/session')->addSuccess('YOUR MSG');
etc
This message is shown at phtml file using add below code:
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
Most Important Note:
This session message is one available up to one render
.
ON Message display page
,you need to initialized
this session using code initLayoutMessages()
and you need add this code before $this->renderLayout();
function i.e Start of render layout
Just like:
$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');
Suppose you have set message at Mage::getSingleton('customer/session')
and on message display page you have not initialized $this->_initLayoutMessages('customer/session');
Then you did not get this message.
A Good example is contact page:
public function postAction()
{
........
if ( $post ) {
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
.............
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
$this->_redirect('*/*/');
return;
}
} else {
$this->_redirect('*/*/');
}
}
Message is show at indexAction
:
public function indexAction()
{
$this->loadLayout();
$this->getLayout()->getBlock('contactForm')
->setFormAction( Mage::getUrl('*/*/post') );
$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');
$this->renderLayout();
}
Best Answer
Thanks to you pointing me in the right place to look I figured out something that worked. I changed
Element.show('formSuccess');
toElement.show('formSuccess2');
and added the div like this<div id="formSuccess2" style="display:none">hello</div>