Auto-submit a form once URL is clicked

Post

Posted
Rating:
#4950 (In Topic #1106)
Joe
Avatar
Standard member
Joe is in the usergroup ‘Honoured member’
Let's say I have a button or link, that when clicked, can auto-submit a form on a different page. Is that at all possible? The URL of the link that was clicked on would contain default field values for said form. I just need a way to submit it.

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.
Online now: No Back to the top

Post

Posted
Rating:
#4957
Avatar
Site staff
enelson is in the usergroup ‘Super-moderators’
I usually use the something like this, in such a scenario, on the page needing auto-submission:

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
Online now: No Back to the top

Post

Posted
Rating:
#4958
Joe
Avatar
Standard member
Joe is in the usergroup ‘Honoured member’
This would sort of work I imagine, but not exactly how I am hoping for.

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? :lol:
Online now: No Back to the top

Post

Posted
Rating:
#4962
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
Okay, I think you're asking how to direct some kind of web request tangential to what the user is currently doing in their browser tab, without in any way leaving or disrupting that browser tab.

This would be done using either iframes or AJAX.

The easiest way is to:
  1. make a snippets hook with your PHP actualiser code, e.g. sources_custom/hooks/systems/snippets/example.php
  2. make sure you {$REQUIRE_JAVASCRIPT,ajax} in your template (so the Composr AJAX library is loaded)
  3. 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

Was I helpful?
  • 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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.

Statistics

Forum statistics:
  • 2,052 topics, 7,195 posts, 10,827 members
  • Our newest member is LowfareMart
Back to Top