Currently, I am working on a WordPress site that involves a form submission process. Upon successful submission, a new post is created.
After the user submits the form, I have implemented JavaScript to prompt them to share a tweet with dynamically prepopulated content. However, I encountered an issue where the tweet is preloaded with the content from the previous form submission instead of the current one.
I am considering delaying the onClick function until the page reloads with the updated content published. However, I am unsure about how to achieve this.
Below is the markup for the form submit input:
<input onclick="tweetIt()" class="exclude btn-main stack" name="user-submitted-post" id="user-submitted-post" type="submit" value="<?php _e('Submit', 'usp'); ?>">
Once the form is successfully submitted, the content is displayed as follows:
<p id="dream"><?php echo substr(the_title('', '', FALSE), 0, 140); ?></p> // the title is populated with one of the fields from the form
Here's the relevant JavaScript code:
function tweetIt () {
var phrase = document.getElementById('dream').innerText;
var tweetUrl = 'https://twitter.com/share?text=' +
encodeURIComponent(phrase) +
'.' +
'&url=' +
'http://xxx';
window.open(tweetUrl);
}
I hope I have explained the situation clearly. Any assistance would be greatly appreciated!