I'm facing a strange issue calling a php page by an ajax call.
I have a module with 2 pages:
1) action.schedule.php
2) action.ajax_send.php
Page 1 (action.schedule.php) has a form, displayed by a file template.tpl. In this file template there is an ajax call to page 2, which sends an email message using Swiftmailer library.
The issue: page 2 (action.ajax_send.php) seems to be parsed twice. There is any kind of loop in it, but it always sends the mail twice (I receive two identical mails).
If I directly call action.ajax_send.php, in this way:
Code: Select all
http://www.site.com/modules/MyModule/action.ajax_send.php
This is my code:
Page 1: action.schedule.php:
Code: Select all
<?php
if (!isset($gCms)) exit;
$this->smarty->assign('name_input', $this->CreateInputText($id, 'name', $name, 25, 128));
$this->smarty->assign('email_input', $this->CreateInputText($id, 'email', $email, 25, 128));
$this->smarty->assign('phone_input', $this->CreateInputText($id, 'phone', $phone, 25, 30));
$this->smarty->assign('message_input', $this->CreateTextArea(false,$id,$message, 'message','','message','','', 50,10));
$txt_sa = $this->CreateFrontEndFormStart($id,$returnid,'');
$smarty->assign('formstart_contact',$txt_sa);
$smarty->assign('submit_contact', $this->CreateInputSubmit($id,'submit_contact', $this->Lang('submit')));
$smarty->assign('formend_contact',$this->CreateFormEnd());
$smarty->assign('url_ajax', $this->create_url($id, 'ajax_send', $returnid ));
// PASS FORM ID FOR JQUERY...
$form_id=array('id'=>$id);
$form_id = $form_id["id"];
$this->smarty->assign('form_id', $form_id);
echo $this->ProcessTemplate('template.tpl');
?>
Code: Select all
{$formstart_contact}
<ul>
<li>
<label for="{$form_id}name">name</label></li>
<li>{$name_input}</li>
<li><label for="{$form_id}email">eMail</label></li>
<li>{$email_input}</li>
<li><label for="{$form_id}phone">phone</label></li>
<li>{$phone_input}</li>
<li><label for="message">message</label></li>
<li>{$message_input}</li>
<li>{$submit_contact}</li>
</ul>
{$formend_contact}
{literal}
<__script__ type='text/javascript'>
$("form").submit(function(e) {
e.preventDefault();
var name = $("#{/literal}{$form_id}{literal}name").val();
var email = $("#{/literal}{$form_id}{literal}email").val();
var phone = $("#{/literal}{$form_id}{literal}phone").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "{/literal}{$url_ajax}{literal}+'&showtemplate=false",
dataType : 'json',
data: {
name: name,
email: email,
phone: phone,
message: message
}
});// END AJAX POST
});
</__script>
{/literal}
Page 2: action.ajax_send.php (in this test I'm not retrieving form variables, setting them statically in the email):
Code: Select all
<?php
require_once(dirname(__FILE__)."/lib/Swift-4.2.2/lib/swift_required.php" );
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.mysite.com', 25);
$transport->setUsername('info@mysite.com');
$transport->setPassword('p4ssw0rd');
$email_visitor = 'usermail@domain.net';
$name_visitor = 'John';
$message_visitor='Hello!';
// Create the Mailer
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject('Message from web site')
->setFrom(array($email_visitor => $name_visitor))
->setTo(array('my@mail.com' => 'Kit'))
->setBody($message_visitor, 'text/html')
;
// Send the message
$numSent = $mailer->send($message);
?>
I saw a similar behaviour with another action page from inside a fancybox iframe: the queries were executed two times.
I guess this is due to the way cmsms parses pages and templates, that I do not completely understand, and I can't find a solution
Does anyone have faced the same issue and can point me into the right direction to solve it?
Thanks a lot