Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Multi-Step Form submits itself in background unbeknown to user WordPress

  • SOLVED

Hi,

I have created a simple multi-step form for use in Wordpress.

There are two pages to fill out.

The issue I am having is that when the user clicks "Next" on the first page of the form to go to the second page, the form somehow <strong>submits itself in the background unbeknown to the user</strong>. Therefore when the user clicks "Submit" I end up getting the form mailed to me twice.

However the first time when it submits in the background it only processes the form values on page one and of course when the user finally Submits the form I get all of the form values.

This problem only seems to occur when the Wordpress header.php file is being called so something in there is causing this issue. I know my form is a very very basic form with no validation etc but that doesn't matter a great deal at this stage unless that is what is causing the problem?

If I remove the header.php file entirely then this problem does not occur.

If I test my multi-step form in a html setup outside of Wordpress then this problem does not occur.

Here is the code for each page:

1) On the home page I have created a form with the following values;


<form method="post" action="<?php bloginfo('url');?>/step-2/">
<input type="text" name="firstname">
<input type="text" name="emailaddress">
<input type="submit" value="Next">
</form>


2) On the page titled Step 2 (/step-2/) I have the following values;

session_start();

session_register('firstname');
session_register('emailaddress');

$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['emailaddress'] = $_POST['emailaddress'];



<form method="post" action="<?php bloginfo('url');?>/step-3/">
<input type="text" name="value1">
<input type="text" name="value2">
<input type="submit" value="Submit Form">
</form>



3) On the page Step 3 (/step-3/) I have the following values;


session_start();

session_register('value1');
session_register('value2');

$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];

<?php

session_start();

$ToEmail = '[email protected]';
$EmailSubject = 'Site contact form ';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_SESSION['firstname']."<br>";
$MESSAGE_BODY .= "Email: ".$_SESSION["emailaddress"]."<br>";
$MESSAGE_BODY .= "Value 1: ".$_SESSION["value1"]."<br>";
$MESSAGE_BODY .= "Value 2: ".$_SESSION["value2"]."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die
("Failure");

?>

<div> Thank you for submitting your details </div>

Answers (3)

2011-10-19

Julio Potier answers:

Hello

Delete all your "<strong>session_start()</strong>" and add only one on top of <em>header.php</em> before
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

And try again ;)


Wordpressing comments:

Hi Julio,

I thought that might work but it didn't.

I removed all session_start() from both form2.php and form3.php and added it to the header.php above the DOCTYPE and still the same problem occurs.

I even then decided to remove it from the header.php and even with no session_start() declared the form still posts itself!

Has to be something in that damn header.php file already causing it... hmm


Wordpressing comments:

Seems like its the <?php wp_head();?> that is doing it... if I remove that then the problem goes away... hmm


Julio Potier comments:

Ok, can you add a line before wp_head() ?
This one :
remove_all_actions( 'wp_head' );
We will see if this mess comes from an action or not.
I guess yes, in this case :
1) Delete the remove_all_actions() line.
2) deactivate a plugin
3) retry to send your form
4) go to 2) ;)

We will find which plugin causes this.


Wordpressing comments:

It is definitely coming from within the wp_head();

I tried removing the entire wp_head(); and the problem went away...

I also tried remove_all_actions( 'wp_head' ); which I assume is also the same... and the problem went away...

I've settled on a final solution of;

<?php
if ( is_page('step-3') || is_page('step-2') ) {

}
else {
wp_head();
}
?>


It's not the most ideal solution but in this instance I don't have time to find out precisely which hook is causing the problem.

Thanks Julio for your help... and to EVERYONE else who decided to reply always appreciated!

This answer from Julio was the most helpful in this instance and as such I will award the votes to you.


Julio Potier comments:

Thank you !

2011-10-19

Luis Abarca answers:

Can you please paste the header.php code to see whats happening ?

You can also check add a hidden value in the second form and check before sending the email




if ( isset( $_POST['step'] ) && $_POST['step'] == 2 ) {
// ok send the email :D
}


Wordpressing comments:

Hi Luis,

The contents of the header.php is as follows;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">

<title><?php woo_title(); ?></title>
<?php woo_meta(); ?>
<?php global $woo_options; ?>

<link rel="stylesheet" type="text/css" href="<?php bloginfo( 'stylesheet_url' ); ?>" media="screen" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php if ( $woo_options[ 'woo_feed_url' ] ) { echo $woo_options[ 'woo_feed_url' ]; } else { echo get_bloginfo_rss( 'rss2_url' ); } ?>" />
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />

<?php wp_head(); ?>
<?php woo_head(); ?>

</head>

<body <?php body_class(); ?>>
<?php woo_top(); ?>

<div id="wrapper">

<?php if ( function_exists( 'has_nav_menu') && has_nav_menu( 'top-menu' ) ) { ?>

<div id="top">
<div class="col-full">
<?php wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'top-nav', 'menu_class' => 'nav fl', 'theme_location' => 'top-menu' ) ); ?>
</div>
</div><!-- /#top -->

<?php } ?>

<div id="header">

<div class="col-full">

<div id="logo">
<img src="<?php bloginfo('template_directory'); ?>/images/fastproperlogo.png">
<!--
-->
</div><!-- /#logo -->

<?php if ( isset($woo_options[ 'woo_ad_top' ]) && $woo_options[ 'woo_ad_top' ] == 'true' ) { ?>
<div id="topad">
<?php if ( $woo_options['woo_ad_top_adsense'] != '' ) { echo stripslashes($woo_options['woo_ad_top_adsense']); } else { ?>
<a href="<?php echo $woo_options[ 'woo_ad_top_url' ]; ?>"><img src="<?php echo $woo_options['woo_ad_top_image']; ?>" width="468" height="60" alt="advert" /></a>
<?php } ?>
</div><!-- /#topad -->
<?php } ?>

<?php if ( isset($woo_options[ 'woo_header_right' ]) && $woo_options[ 'woo_header_right' ] == 'true' && $woo_options[ 'woo_ad_top' ] != 'true' ) { ?>
<div id="header-right">

<?php if ( $woo_options[ 'woo_header_right_content' ] == 'true' ) { ?>
<div class="fl">
<?php if ( $woo_options[ 'woo_header_right_text' ] != '' ) { ?><p><?php echo $woo_options[ 'woo_header_right_text' ]; ?></p><?php } ?>
</div><!-- /.fl -->
<?php } ?>

<?php if ( $woo_options[ 'woo_header_right_btn' ] == 'true' ) { ?>
<div class="fr">
<a href="<?php echo $woo_options[ 'woo_header_right_btn_link' ]; ?>"><span><?php echo $woo_options[ 'woo_header_right_btn_text' ]; ?></span></a>
</div><!-- /.fr -->
<?php } ?>

</div><!-- /#header-right -->
<?php } ?>

</div><!-- /.col-full -->

</div><!-- /#header -->

<div id="navigation-out">

<div id="navigation">

<div class="col-full">

<?php
if ( function_exists( 'has_nav_menu') && has_nav_menu( 'primary-menu') ) {
wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'main-nav', 'menu_class' => 'nav fl', 'theme_location' => 'primary-menu' ) );
} else {
?>
<ul id="main-nav" class="nav fl">
<?php
if ( isset($woo_options[ 'woo_custom_nav_menu' ]) && $woo_options[ 'woo_custom_nav_menu' ] == 'true' ) {
if ( function_exists( 'woo_custom_navigation_output') )
woo_custom_navigation_output();
} else { ?>
<?php if ( is_page() ) $highlight = "page_item"; else $highlight = "page_item current_page_item"; ?>
<li class="<?php echo $highlight; ?>"><a href="<?php echo home_url( '/' ); ?>"><?php _e( 'Home', 'woothemes' ) ?></a></li>
<?php
wp_list_pages( 'sort_column=menu_order&depth=6&title_li=&exclude=' );
}
?>
</ul><!-- /#nav -->
<?php } ?>

<div id="search-top" class="fr">
<?php get_template_part( 'search-form' ); ?>
</div>

</div><!-- /.col-full -->

</div><!-- /#navigation -->

</div><!-- /#navigation-out -->


Luis Abarca comments:

I can't see any problem in your header file.

Did you try to add a hidden field on your second form and check for that field before sending the email ?

2011-10-19

Kannan C answers:

before calling the mail function do some validations like
page2:

if(isset($_SESSION['firstname']) && !empty($_SESSION['firstname']) && isset($_SESSION['emailaddress']) && !empty($_SESSION['emailaddress'])) {
//form 2
} else {
//redirect to page 1
}


page3:

if(isset($_SESSION['value1']) && !empty($_SESSION['value1']) && isset($_SESSION['value2']) && !empty($_SESSION['value2'])) {
mail();
}
else {
//redirect to page 2
}


Wordpressing comments:

I replace your values with session values that match mine and nothing happens. The form still posts itself in the background when going from Page 1 to Page 2 even though the user hasn't pressed Submit.

Naturally I need to place your code after session_start(); right?


Kannan C comments:

5 dollar is so small for this question, if you increase the offer i will post a new code which can be written in a single page or will check your existing page for errors. And your right, the contents should be after session_start(), and session_start() can go on to the top of the header.php