Pages parsed twice?

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Pages parsed twice?

Post by nervino »

Hi All,
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
it correctly sends only one mail.



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');
?>
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
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Pages parsed twice?

Post by calguy1000 »

I bet you have more than one 'form' on the rendered page.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: Pages parsed twice?

Post by nervino »

And you lose the bet :P
I've also tried to fire the form with its cmsms generated name:

Code: Select all

$("#cntnt01moduleform_1").submit(function(e) {
  	e.preventDefault();............
Same result: two emails received.

thanks Robert
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: Pages parsed twice?

Post by nervino »

Well, I made several other tests:
1) using a static url in the ajax, instead of the cmsms generated one:
$.ajax({
type: "POST",
url: "http://www.site.com/modules/MyModule/ac ... x_send.php",

sends 1 mail


2) By Removing "showtemplate=false" from the ajax url:
$.ajax({
type: "POST",
url: "{/literal}{$url_ajax}{literal}",

output "SyntaxError: JSON.parse: unexpected character" and so sends 1 mail

I also made a fresh install (cmsms 1.11.3), deleting everything from the template but the form that sends the emails: same result, 2 emails sent.

I really have no idea how to fix this unwanted behaviour...
caciavar
Forum Members
Forum Members
Posts: 64
Joined: Wed Feb 11, 2009 6:30 pm
Location: Toronto, Canada

Re: Pages parsed twice?

Post by caciavar »

I have the same issue ever since 1.11.

I'm using ajax with a simple 'contact-me' form.

In 1.10 I would receive one e-mail. Ever since 1.10 (up to current version of 1.13) I am getting 2 e-mails. I think it has something to do with the "&showtemplate=false" as part of the form action. I definitely concur with you nervino, there is a bug somewhere.

I don't think it has anything to do with form builder either... something in the cmsms core but I'm not sure what.

-Chris
Post Reply

Return to “Developers Discussion”