Jump to content

Variable not affected outside if statement


Vel

Recommended Posts

Hi guys, I have an interesting situation and I was hoping someone could explain what is going on to me.

 

I have an enquiry form on a site that sends an e-mail. All the code is on one page with the form submitting back to the same page. I have a variable, msg, which at the top of the page is set to nothing and then an if statement to see if the form was submitted. If the form was submitted then msg will have one of three messages in it by the end (regardless of anything else). However when I check the msg variable after the if statement it is empty.

 

To test if it was being affected I changed the initial value from nothing to TEST. After the IF statement the variable still held the value TEST, thus confirming it wasn't being affected by the IF statement. Now onto my code (the whole page is over 600 lines long so I'm only posting the relevant code):

 

<?php
session_start();
$msg = '';
$input = array(
24 variables from form here...
);
if(isset($_POST['form-submitted'])) {
$missing_info = FALSE;
$array = array(
	variables along with their filter validation/sanitize types...
);
$input = filter_input_array(INPUT_POST, $array);
/* PHP validation code checking appropriate fields were not empty. If they were set $missing_info = TRUE */

if(!$missing_info) {
	/* Construct e-mail, this sends successfully. */
	if(mail($to, $subject, $content, $headers)) { 
		$msg = '<div class="message"><h1>Form Sent</h1><br />Your form has been sent to us. We endevour to respond to all booking requests with 2 working days. If you have not heard from us in that time please contact us on 01462 893620. If you chose to confirm by post please allow 5 working days before contacting us.</div>';
		$_SESSION['msg'] = $msg;
	} else {
		$msg = '<div class="message"><h1>Technical Issues</h1><br />We appologise but the website is currently experiencing technical difficulties. Please try again later or print this form out and post / fax it to us. Alternatively you can call us on 01462 893620.</div>';
		$_SESSION['msg'] = $msg;
	}
} else {
	$msg = '<div class="message"><h1>Incomplete Form<h1><br />Please make sure to complete all fields of the form before submitting your booking request.</div>';
	$_SESSION['msg'] = $msg;
}
}
if(isset($_SESSION['msg'])) { 
$msg = $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>

 

If I don't load the message into session and then back out of session after the if statement then $msg will still hold whatever it was initially set to (in this case an empty string). Why isn't it set in the if statement?

Link to comment
Share on other sites

I don't get why you are setting and unsetting the SESSION['msg'] on the same page.  for debugging, can you change to this and let us know what you get back:

		if(mail($to, $subject, $content, $headers)) { 
		$msg = '<div class="message"><h1>Form Sent</h1><br />Your form has been sent to us. We endevour to respond to all booking requests with 2 working days. If you have not heard from us in that time please contact us on 01462 893620. If you chose to confirm by post please allow 5 working days before contacting us.</div>';
		die( "success message = $msg");
	} else {
		$msg = '<div class="message"><h1>Technical Issues</h1><br />We appologise but the website is currently experiencing technical difficulties. Please try again later or print this form out and post / fax it to us. Alternatively you can call us on 01462 893620.</div>';
		die("tech fault message = $msg");
	}
} else {
	$msg = '<div class="message"><h1>Incomplete Form<h1><br />Please make sure to complete all fields of the form before submitting your booking request.</div>';
	die("bad form message = $msg");
        }

Link to comment
Share on other sites

Are you sure that all of the conditions are being met in order for $msg to have a value?

100% sure, because I have them being stored in the SESSION['msg'] and that IS showing the correct value.

 

I don't get why you are setting and unsetting the SESSION['msg'] on the same page.  for debugging, can you change to this and let us know what you get back:

		if(mail($to, $subject, $content, $headers)) { 
		$msg = '<div class="message"><h1>Form Sent</h1><br />Your form has been sent to us. We endevour to respond to all booking requests with 2 working days. If you have not heard from us in that time please contact us on 01462 893620. If you chose to confirm by post please allow 5 working days before contacting us.</div>';
		die( "success message = $msg");
	} else {
		$msg = '<div class="message"><h1>Technical Issues</h1><br />We appologise but the website is currently experiencing technical difficulties. Please try again later or print this form out and post / fax it to us. Alternatively you can call us on 01462 893620.</div>';
		die("tech fault message = $msg");
	}
} else {
	$msg = '<div class="message"><h1>Incomplete Form<h1><br />Please make sure to complete all fields of the form before submitting your booking request.</div>';
	die("bad form message = $msg");
        }

 

I'm setting and unsetting the session variable on the same page right now because it was the only way I could get the message to show. It was to prove the $msg was in fact triggering properly inside the if statement but not accessible outside. I ran your test and the script never hit the die message, which is really confusing because it did send me the e-mail. Now I'm even more confused.

Link to comment
Share on other sites

A) What does a phpinfo statement show for register_globals?

 

B) How do you know the code with the die() statements didn't reach one of the die() statements? What was the output on the page and what was the 'view source' of the page in your browser? How do you know for a fact it is that specific mail() statement that is being executed? There's nothing in the posted code that indicates what any of the mail() parameters are being set to. The code you did post might be skipped over and you have another mail() statement in some code that is being executed that is actually sending you the email.

 

C) If A) or B) don't expose what is causing the problem, you will need to post ALL the code (less any database credentials) that reproduces the problem on your problematic page in question. You could be doing things like header() redirects to the same page without stopping execution on the page after the header statement or using/having output buffering turned on that is hiding what is actually occurring on the page or having assignment = operators instead of == comparison operators causing the logic to do something unexpected.

Link to comment
Share on other sites

I solved it. Thank you PFMaBiSmAd, you put me in the right frame of mind to solve it. Thinking outside of the box the issue is we use SEO friendly URLs. The form was submitting to booking-form.php?id=15 and the email was sent there, then the SEO URL kicked in redirecting the page to booking-form with no POST data, so no message was displayed. All I had to do was change the form to submit to booking-form so that the SEO URL didn't kick in and redirect the page and the message showed perfectly fine.

 

Thanks for all your help guys. Its always great to be able to bounce problems around on here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.