Auto-submit a form once URL is clicked
Posted
#4950
(In Topic #1106)

Standard member

I'm trying to make it easier for users to add bookmarks. I've made it so when the user clicks "add bookmark", it opens a new window that auto-completes all required form data (bookmark title, page-link, folder). At this point all the user has to do is click the "add" button, but my intention is to hopefully get the bookmark to add in the background (via JS or something), without the user needing to leave the current page and click on anything additional.
Posted

Site staff

Code
<form id='form' method='POST' action='/'>
...
</form>
Then somewhere before the closing body tag, you submit the form with JS
Code
document.getElementById("form").submit();
Also see javascript - Auto submit form on page load - Stack Overflow
Posted

Standard member

1). There would be a link on a page that a user must click (i.e. 'add bookmark').
2). Using your method, and once the user clicks this link, I'd have to make it so a new window would still open linking to the page that form resides within. Even though the form would be submitted automatically and I could code it to close the window afterwards, I'd still have to direct the user to this page first, which is what I'm trying to avoid.
I know we're not supposed to make Tempcode executable, but it crossed my mind – I could create a Tempcode symbol that injects SQL into the bookmark table to automatically create the bookmark, but maybe that's a security issue? On the flip-side though, the symbol would be coded to only inject a specific row with specific values into a specific table for a specific member – so is it still a security threat in that case?

Posted

Site director

This would be done using either iframes or AJAX.
The easiest way is to:
- make a snippets hook with your PHP actualiser code, e.g. sources_custom/hooks/systems/snippets/example.php
- make sure you {$REQUIRE_JAVASCRIPT,ajax} in your template (so the Composr AJAX library is loaded)
- do something like <form action="#" onsubmit="load_snippet('example', null, function() { alert('Success'); } ); return false;">
If you know how to use your browser's developer tools, particularly the network activity tab, this will help a lot in debugging as you can watch the AJAX call happen, seeing the exact call made and the response to it.
Become a fan of Composr on Facebook or add me as a friend. Add me on on Mastodon. Follow me on Minds (where I am most active). Support me on Patreon
- If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
- If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
- If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
- If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
1 guest and 0 members have just viewed this.