1

I'm using a WordPress sidebar widget to capture email addresses. The thing is, it redirects after form submission. I want the visitor to stay on the page they were on after form submission with just a hidden div giving a successful signup message.

I've tried something with javascript like this --

<script type="text/javascript">
function showHide() {    
var div = document.getElementById("hidden_div");
if (div.style.display == 'none') {
div.style.display = '';
}
else {      
div.style.display = 'none';
}
} 
</script>

And that works perfectly for showing the hidden div on submit, but the actual form then doesn't work :(

The form (with what I was trying to do) is like this --

<div id="wp_email_capture"><form name="wp_email_capture" method="post" onsubmit="showHide(); return false;" action="<?php echo $url; ?>">
<label class="wp-email-capture-name">Name:</label> <input name="wp-email-capture-name" type="text" class="wp-email-capture-name"><br/>
<label class="wp-email-capture-email">Email:</label> <input name="wp-email-capture-email" type="text" class="wp-email-capture-email"><br/>
<input type="hidden" name="wp_capture_action" value="1">
<input name="submit" type="submit" value="Submit" class="wp-email-capture-submit">
</form>

<div id="hidden_div" style="display:none"><p>Form successfully submitted.</p> 
</div>

The problem is coming in somewhere between 'return false' and the form action (which is where the plugin's coder has made it redirect I think). If I remove 'return false', it redirects. With 'return false' there, the form doesn't work. I can't figure out a way to get the form to work but not redirect, ie. just show the hidden div, work, and that's it! No redirect :) Would appreciate your help.

waterprism
  • 23
  • 2
  • 6
  • 1
    `return false;` prevents the form from submitting. But, without it, your page will post back and it will appear as if your hidden div never showed. Do you want to actually submit the form for server-side processing *and* show the hidden div, without the page reloading? If so, you need to look into AJAX. – Cᴏʀʏ Sep 24 '11 at 22:20
  • 1
    Yeah, I'd like the form to be processed and show the hidden div without a page redirect. I've never even touched AJAX besides the dishes :( How would it work with this? Thanks. – waterprism Sep 24 '11 at 22:34
  • As Cory explained, you will definitely need AJAX for this. AJAX is a way to send data programmatically (using javascript) without re-loading pages. What type of Javascript libraries/frameworks are available to you, if any? For example, does your site use jQuery, Prototype, Mootools, etc? If you don't know, try looking in the `` section of one of your pages and see what kind of ` – ampersand Sep 25 '11 at 01:33
  • what value the var $url contains if your form action is other page the control goes over to that page and the code on the this page will not execute. – Punit Sep 25 '11 at 09:20
  • @ampersand. Hi. Thanks. There is jquery. The theme's author has a jquery file linked. – waterprism Sep 25 '11 at 13:29
  • I added a possible solution using jQuery. It should work with all of the latest jQuery versions. – ampersand Sep 25 '11 at 16:25

1 Answers1

1

I will show how to submit the form with jQuery, as this is what you have available to you:

First of all, you should make one small change to the form HTML. Namely, change showHide() to showHide(this), which will give showHide() access to the form element. The HTML should be:

<div id="wp_email_capture"><form name="wp_email_capture" method="post" onsubmit="showHide(this); return false;" action="<?php echo $url; ?>">
<label class="wp-email-capture-name">Name:</label> <input name="wp-email-capture-name" type="text" class="wp-email-capture-name"><br/>
<label class="wp-email-capture-email">Email:</label> <input name="wp-email-capture-email" type="text" class="wp-email-capture-email"><br/>
<input type="hidden" name="wp_capture_action" value="1">
<input name="submit" type="submit" value="Submit" class="wp-email-capture-submit">
</form>

<div id="hidden_div" style="display:none"><p>Form successfully submitted.</p> 
</div> 

The javascript to submit the form and display the div on successful submit is:

function showHide(form) { 
    var serial = $(form).serialize();
    $.post(form.action, serial, function(){
        $('#hidden_div').show();
    });
}; 

What this does:

  1. Serializes the form data, i.e. converts it to one long string such as wp-email-capture-name=&wp-email-capture-email=&wp_capture_action=1 that is stored in serial.
  2. Submits the serialized data to the the form's action url (form.action)
  3. If the form submit was successful, it runs the success handler, which is the third parameter to $.post(). This handler takes care of displaying the hidden div. I changed the code to use jQuery's .show() function, which takes care of browser inconsistencies.

Hope this is helpful.

ampersand
  • 4,264
  • 2
  • 24
  • 32
  • Sorry ampersand! I thought this thread was dead, and with a ton of stuff on my head put this problem on the backburner. Thank you so much. I will test this code soonest. I really had to launch the site and just went with a javascript history -1 solution temporarily to redirect back to the page the visitor was on. I will definitely try this soon and do appreciate your time. – waterprism Sep 27 '11 at 18:02