1

I have a cakephp controller with action and associated view. The action in the controller saves some data to the database (it creates a new record) when called. I was noticing that for some reason 2 records are created. The only difference between the 2 records is the date_created field which is different by a few seconds so I know that the second (duplicate) record is being created a few seconds after the first. I have been able to narrow the problem down to some javascript that is included in the view inside tags. I include a .js file at the top of the view using -

echo $this->Html->script('https://link.to.externalSite.com/script.js', array(
  'inline' => true
));

When I change 'inline' => true to false then the problem disappears, only 1 record is added to the database as I expect. However I need the script set inline => true for my purposes.

The tag in the view page is triggered when a submit button on a form is clicked as follows -

<script type="text/javascript" charset="utf-8">
    var myVar = new .......... ({trigger: 'submitBtn'});
</script>

I am wondering if this is caused because the script is loaded from an external url? If i include the script in my webroot will I avoid this problem? I have come across questions on stackoverflow about similar issues with JS scripts causing controller actions to be called twice and they talk about issues of deeplinking, however I don't understand what to do about that.

1 Answer 1

2

With inline set to true, when you view your in-browser source code how many times is the script file https://link.to.externalSite.com/script.js included? Perhaps its being added by multiple view elements?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks .. When I 'view source' in safari I only see the script file included once. Incase this helps - the submit button image used in the form is also included from an external source. thanks for your response.
Is there a live page you can share with us?
Hi - no sorry, Im developing locally right now and not able to share a live page.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.