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.