Page 1 of 1

Undefinded variable error

Posted: Mon Aug 02, 2010 5:12 pm
by guytz72
Hi,

I have an error message being returned from a php page "undefined variable : $show_succes_msg.

You can see the page here http://www.igolfapps.com/test/contact.php

The code for the page is below:

Code: Select all

<?php
    // form processing
    if(isset($_POST['submit'])){
    	// basic init
        require_once "includes/functions.php";
	
    	// user input
    	$name = $_POST['frm_name'];
    	$mail = $_POST['frm_email'];
    	$subject = $_POST['frm_subject'];
    	$comments = $_POST['frm_comments'];
	
    	// validate input
    	$error = array();
    	if(empty($name)) $error[1] = true;
	
    	if(empty($mail)) $error[2] = true;
    	if(!check_email_address($mail)) $error[2] = true;
	
    	if(empty($subject)) $error[3] = true;
	
    	if(empty($comments)) $error[4] = true;
	
    	if(empty($error)){
    		mail("your@domain.com", "yourapp: '" . $subject . "'",
    			"Name: \t\t\t" . $name . "\n\nEmail:\t\t\t". $mail ."\n\Message: \n" . stripslashes($comments) . "\n\n",
    			"From: noreply@domain.com\r\nReply-To: $mail\r\n");
		
    		$show_succes_msg = true;
    		unset($_POST);
    	}
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="imagetoolbar" content="no" />

    <title>iPhone app › Contact</title>
    
    <link rel="stylesheet" type="text/css" media="screen" href="stylesheets/main.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="stylesheets/fancybox.css" />
        
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script src="javascripts/jquery.embedquicktime.js" type="text/javascript" charset="utf-8"></script>
    <script src="javascripts/jquery.fancybox.js" type="text/javascript" charset="utf-8"></script>
    <script src="javascripts/jquery.easing.1.3.js" type="text/javascript" charset="utf-8"></script>
    
    <script type="text/javascript">
        $(document).ready(function() {
            jQuery.embedquicktime({
                jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 
                plugin: 'javascripts/jquery.embedquicktime.js'
            });
        });
        $(document).ready(function() {
            $("a.link").fancybox({
                'zoomOpacity'           : true,
                'zoomSpeedIn'           : 300,
                'zoomSpeedOut'          : 300,
            	});
        });
    </script>
    <!--[if IE 6]>
        <link rel="stylesheet" type="text/css" media="screen" href="stylesheets/main-IE6.css" />        
        <script src="javascripts/DD_belatedPNG.js"></script>
        <script>
          /* EXAMPLE */
          DD_belatedPNG.fix('#iphone, #iphone a, h1, .link .img');

          /* string argument can be any CSS selector */
          /* change it to what suits you! */
        </script>
    <![endif]-->

</head>
<body>
    <div id="wrapper">
        <div id="header">
            <ul id="navigation">
                <li><a href="index.html"><span>Home</span></a></li>
                <li><a href="about.html"><span>About</span></a></li>
                <li class="current"><a href="contact.php"><span>Contact</span></a></li>
            </ul>
            <div id="title">
                <h1>iPhone application</h1>
                <a href="#"><span>$1.99</span></a>
            </div>
        </div>
        <div id="main">
            <div id="iphone">
                <div class="img">
                    <div class="hvlog {width: '230', height: '346', controller: 'false', loop: 'true', pluginspage: 'http://www.apple.com/quicktime/download/'}">
                        <a href="media/example.mov" rel="enclosure">click to play</a>
                        <img src="media/screenshot-1.png" alt="" />
                    </div>
                </div>
            </div>
            <form id="content" action="contact.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<?php if($show_succes_msg){ ?>
                <div class="row">
                    <div class="column six">
                        <h2>Thank you</h2>
                        <p>Thank you for your message. I'll respond as quickly as possible.</p>
                    </div>
                </div>
<?php } else {?>
                <div class="row">
                    <div class="column three">
                        <p<?php if($error[1]) echo ' class="error"><big>Can\'t be Empty</big' ?>>
                            <label for="frm_name">Name</label>
                            <input type="text" name="frm_name" value="<?php echo $_POST['frm_name'] ?>" id="frm_name" />
                        </p>
                    </div>
                    <div class="column three">
                        <p<?php if($error[2]) echo ' class="error"><big>Can\'t be Empty</big' ?>>
                            <label for="frm_email">Email</label>
                            <input type="text" name="frm_email" value="<?php echo $_POST['frm_email'] ?>" id="frm_email" />
                        </p>
                    </div>                    
                </div>
                <div class="row">
                    <div class="column three">
                        <p<?php if($error[3]) echo ' class="error"><big>Can\'t be Empty</big' ?>>
                            <label for="frm_subject">Subject</label>
                            <input type="text" name="frm_subject" value="<?php echo $_POST['frm_subject'] ?>" id="frm_subject" />
                        </p>
                    </div>
                </div>
                <div class="row">
                    <div class="column six">
                        <p<?php if($error[4]) echo ' class="error"><big>You don\'t wanna say anything?</big' ?>>
                            <label for="frm_comments">Message</label>
                            <textarea name="frm_comments" id="frm_message" rows="8" cols="40"><?php echo stripslashes($_POST['frm_comments']) ?></textarea>
                            
                            <button type="submit" name="submit">Send</button>
                        </p>
                    </div>
                </div>
            </form>
<?php } ?>            
        </div>
        <div id="footer">
            <p><strong>Copyright &copy; 2010</strong> by <a href="#">Yourcompany</a>. Design by <a href="http://jonnotie.nl">Jonnotie</a><br />
            Provided By <a href="http://www.template4all.com/" title="Free Website Templates" target="_blank" rel="nofollow">Free Website Templates</a> | <a href="http://www.freethemes4all.com/blogger-templates/" title="Free Blogger Templates" target="_blank" rel="nofollow">Free Blogger Templates</a> | <a href="http://www.seodesign.us" title="Creation Site Internet" target="_blank" rel="nofollow">SEO Design</a></p>
        </div>
    </div>
</body>
</html>
The functions.php code is below:

Code: Select all

<?php

	function redirect($destination, $delay = null){
		if(empty($delay)){
			header('Location: '. $destination);
			exit();
		}else{
			header('Refresh: '. $delay .'; url='. $destination);
		}
	}
	
	function check_email_address($email) {
	    // First, we check that there's one @ symbol, and that the lengths are right
	    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
	        // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
	        return false;
	    }
	    // Split it into sections to make life easier
	    $email_array = explode("@", $email);
	    $local_array = explode(".", $email_array[0]);
	    for ($i = 0; $i < sizeof($local_array); $i++) {
	        if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
	            return false;
	        }
	    } 
	    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
	        $domain_array = explode(".", $email_array[1]);
	        if (sizeof($domain_array) < 2) {
	            return false; // Not enough parts to domain
	        }
	        for ($i = 0; $i < sizeof($domain_array); $i++) {
	            if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
	                return false;
	            }
	        }
	    }
	    return true;
	}

?>
I am a php newbe and any help with this is greatly appriciated!

Thanks!

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 5:31 pm
by d8p
Just a quick glance maybe change your code from:

if(empty($error))

to

if(count($error)==0)

I'm thinking because you defined the array maybe there is something there, even if it is techinically empty? Hope this helps...not sure if it will!

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 5:49 pm
by jraede
Does it say what type of error? I'm guessing it's NOTICE, in which case it doesn't really matter, it's just letting you know that there could be an issue in the future. You can turn off reporting of notice errors in your php.ini file.

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 6:01 pm
by guytz72
Hi,

Thanks for the quick reply, tried that and still get the same error message. if I change the variable to the word "true" I get the message as epected, but even declaring it at the start and setting it to false I still get this message.

I am not sure if it is just a NOTICE, but the form won't display, all I get is the error messages.

Any other ideas?

Thanks again.

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 6:03 pm
by jraede
Is there another error message that displays after that one? Can you post the full content of the page when it displays those errors? (assuming the PHP is at the top, otherwise if you could just post the full errors)

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 6:06 pm
by guytz72
Here are the errors, and you can see yourself here: http://www.igolfapps.com/test/contact.php
Thanks!


Notice: Undefined variable: show_succes_msg in E:\domains\i\igolfapps.com\user\htdocs\Test\contact.php on line 105

Notice: Undefined variable: error in E:\domains\i\igolfapps.com\user\htdocs\Test\contact.php on line 115
> Name
Notice: Undefined variable: error in E:\domains\i\igolfapps.com\user\htdocs\Test\contact.php on line 121
> Email
Notice: Undefined variable: error in E:\domains\i\igolfapps.com\user\htdocs\Test\contact.php on line 129
> Subject
Notice: Undefined variable: error in E:\domains\i\igolfapps.com\user\htdocs\Test\contact.php on line 137
> Message <br />
<b>Notice</b>: Undefined index: frm_comments in <b>E:\domains\i\igolfapps.com\user\htdocs\Test\contact.php</b> on line <b>139</b><br />

Send

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 6:12 pm
by jraede
They're all notices, so really they shouldn't be preventing your page from being displayed. You're getting these because you're trying to access variables without them being set (odds are they're set in an if/else statement that isn't being run).

However, it seems like they are showing up in the middle of your form fields? If that's the case then displaying those errors would indeed break the HTML. Just change the ERROR_REPORTING line in php.ini to E_ALL & ~E_NOTICE

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 6:18 pm
by guytz72
Thanks, I will contact my ISP but dont hold much hope....they dont like changing anything in case it breaks someone elses site!!

WIll look through the HTML again as it might be something I am missing and you have given me somewhere to start.

Thanks again.

Re: Undefinded variable error

Posted: Mon Aug 02, 2010 6:20 pm
by jraede
If you can't turn off error reporting for notices, just add $variable = null;, for each variable you're using, at the top of the page. For the array in line 139 you also have to define its index, like $array['index'] = null;

Re: Undefinded variable error

Posted: Wed Aug 04, 2010 12:29 am
by guytz72

Code: Select all

$show_succes_msg = null;
	
    	// validate input
    	$error = array();
    	
    	$error[1] = null;
    	$error[2] = null;
    	$error[3] = null;
    	$error[4] = null;
That did not work either, I put the above code at the top and still get the messages in the middile of the page.

in the html I have the beow syntax, is the php code supposed to be in the middle of the <p> tag?

Code: Select all

                <div class="row">
                    <div class="column three">
                        <p<?php if($error[3]) echo ' class="error"><big>Can\'t be Empty</big' ?>>
                            <label for="frm_subject">Subject</label>
                            <input type="text" name="frm_subject" value="<?php echo $_POST['frm_subject'] ?>" id="frm_subject" />
                        </p>
                    </div>
                </div>

Re: Undefinded variable error

Posted: Wed Aug 04, 2010 4:17 am
by jraede
Is $_POST['frm_subject'] being set?