Auto-submit a form once URL is clicked

Post

Posted
Rating:
#4641 (In Topic #939)
Joe
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.

Post

Posted
Rating:
#4646
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

View

Post

Posted
Rating:
#4647
Joe
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:

Post

Posted
Rating:
#4651
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.
1 guest and 0 members have recently viewed this.