Question about IF statement

Ive got a problem with code executing in an IF statement when I dont think it should be doing. Basically, I want the PHP code to execute ONLY when the button is clicked which I have achieved with this:

if (isset($_POST[‘Submit’])) { }

All seems to be OK (i.e. the contents of the form variables are successfully emailed to me) apart from the following problem. I want to run some Jquery script which allters the DOM and prints a confirmation receipt on the same page. The Jquery I’ve written works fine, HOWEVER, it is getting executed even before the Submit button has been pressed. I don’t understand how this is happening as the Jquery script is contained with the PHP IF statement. If you load the page and view the souce code you can see it has been rendered BEFORE the submit button has been pressed - how can this be???

I’ve pasted my PHP code below and here is the link to the page http://fluxcreative.net/contact/email.php

<?php
echo $_POST['Submit'];
//test to see if the button has been pressed i.e. the post form var has something in it.  If true execute below:
if (isset($_POST['Submit']))

echo "THIS SHOULDNT BE PRINTED!!!";

{   
// get data from the form and stuff into vars
	$name = $_POST['name'];
	$from = $_POST['email'];
	$telephone = $_POST['telephone'];
	$message = $_POST['message'];
	
//setup mail vars
	$to = "xxx@gmail.com";
	$subject = "FluxCreative.net contact form message!";
	$headers = "From: $from";
	$body = "This is an email from: $name\
\
$message";

// send email with mail object and run Jquery thanks
//	mail($to,$subject,$message,$headers);
echo "<script type='text/javascript'>

 $(document).ready(function(){
   $('a').click(function(event){
	    alert('ITs working!');
        $('h2').replaceWith('<h2>Email Received!</h2>');	
        $('.dots').replaceWith('<span class=dots>Howdy</h2>');			
		$('#form-container').fadeOut(2000); 
		$('.welcome').replaceWith('<p class=welcome>Hi there <strong> $name </strong>, thanks for sending us an email.</p><p>We will respond to your message shortly, in the meantime, why not check out our portfolio, find out what services we offer and about who we are or read our digital musing via our Blog. ');		
   });
 });

</script>";

}

?>

Doh! Thanks guys, you are correct, it worked when I removed my test echo to the correct place.

Thanks again!

As far as I can tell, you are breaking out of the if right as you

echo 'This should....';

thereby causing the code that follows to always be sent to the browser.

Hope that helps!


Dan Bernardic

Yes I also suspect thereby if it is also not just for testing:


//test to see if the button has been pressed i.e. the post form var has something in it.  If true execute below:
if (isset($_POST['Submit']))

echo "THIS SHOULDNT BE PRINTED!!!";

{  

Should be:


if (isset($_POST['Submit']))
{   
echo "THIS SHOULDNT BE PRINTED!!!";
................................
}