PHP undefined variable question

17 views
Skip to first unread message

Matthew T.

unread,
Aug 6, 2011, 7:58:26 PM8/6/11
to Northwest Arkansas Linux Users Group
Any suggestions as to why the code below gives me undefined variable
errors in xampp 1.7.4 but ran fine on Hostmonster's servers? Maybe
xampp has stricter error reporting?

<?php
if(isset($_POST['submit'])) {
$question = $_POST['question'];
$name = $_POST['name'];
$email = $_POST['email'];
$subject1 = $_POST['subject1'];
$comments = $_POST['comments'];
$output_form = false;

if($question != 4 && $question != 'four') {
echo 'Try the security question again.<br />';
$output_form = true;
}

if($question == 4 || $question == 'four')
{
$to = 'in...@caneycemetery.com';
$subject = $subject1;
$msg = "From $name \n\n$comments";
$bcc = 'm...@google.com';
mail ($to, $subject, $msg, "From:" . $email . "\r\nBCC:" . $bcc);

echo "Thank you for your comments, $name.<br />";
echo 'We appreciate your interest in the history of Caney.';

?>
<h2>Click <a href="../index.html">here </a>to go back to the home
page</h2>
</div>
<?php
}
}
else {$output_form = true;}
if ($output_form) {
?>

<h3>Contact Us</h3>
<form action="email.php" method="post" action="<?php echo
$_SERVER['PHP_SELF']; ?>">
<p>
<label>Security Question: What is 2+2?</label>
<input id="question" type="text" name ="question" maxlength=4
value="" size="30" />
<label>Name</label>
<input id="name" type="text" name="name" value="<?php echo
$name; ?>" size="30" />
<label>Email</label>
<input id="email" type="text" name="email" value="<?php echo
$email; ?>" size="30" />
<label>Subject</label>
<input id="subject1" type="text" name="subject1" value="<?php
echo $subject1; ?>" size="30" />
<label>Your Comments</label>
<textarea id="comments" name="comments" rows="5" cols="5"><?php
echo $comments; ?></textarea>
<br />
<input class="button" type="submit" name="submit"/>
</p>
</form>

<?php
}

Boyd Stephen Smith Jr.

unread,
Aug 7, 2011, 1:34:48 AM8/7/11
to nwa...@googlegroups.com
In <0fc7dafe-5c1e-4778...@f7g2000vba.googlegroups.com>, Matthew
T. wrote:
>Any suggestions as to why the code below gives me undefined variable
>errors in xampp 1.7.4 but ran fine on Hostmonster's servers? Maybe
>xampp has stricter error reporting?

Most likely.

><?php
> if(isset($_POST['submit'])) {
> $question = $_POST['question'];
> $name = $_POST['name'];
> $email = $_POST['email'];
> $subject1 = $_POST['subject1'];
> $comments = $_POST['comments'];

These 5 variables my still show up as unset, if the POST data is either non-
existent or lacks these keys. Also, since this is in a conditional the will
not be set is the POST data does not contain the "submit" key.

> $output_form = false;
>
> if($question != 4 && $question != 'four') {
> echo 'Try the security question again.<br />';
> $output_form = true;
> }
>
> if($question == 4 || $question == 'four')
> {
> $to = 'in...@caneycemetery.com';
> $subject = $subject1;
> $msg = "From $name \n\n$comments";
> $bcc = 'm...@google.com';
> mail ($to, $subject, $msg, "From:" . $email . "\r\nBCC:" .
$bcc);
>
> echo "Thank you for your comments, $name.<br />";

Warning: Possible XSS attack. Try a name of
"<script>alert("0wned!");</script>" for kicks.

> echo 'We appreciate your interest in the history of Caney.';
>
> ?>
> <h2>Click <a href="../index.html">here </a>to go back to the home
>page</h2>
> </div>
> <?php
> }
> }
> else {$output_form = true;}
> if ($output_form) {
> ?>
>
> <h3>Contact Us</h3>
> <form action="email.php" method="post" action="<?php echo
>$_SERVER['PHP_SELF']; ?>">
> <p>
> <label>Security Question: What is 2+2?</label>
> <input id="question" type="text" name ="question"
maxlength=4
>value="" size="30" />
> <label>Name</label>
> <input id="name" type="text" name="name" value="<?php
echo
>$name; ?>" size="30" />

Based on my read of the above, this is only executed when $output_form is
true-ish. That only happens when $name is unset.

> <label>Email</label>
> <input id="email" type="text" name="email" value="<?php
echo
>$email; ?>" size="30" />

Ditto, but s/name/email/.

> <label>Subject</label>
> <input id="subject1" type="text" name="subject1"
value="<?php
>echo $subject1; ?>" size="30" />

Ditto, but for $subject1.

> <label>Your Comments</label>
> <textarea id="comments" name="comments" rows="5"
cols="5"><?php
>echo $comments; ?></textarea>

Ditto, but for $comments.

> <br />
> <input class="button" type="submit" name="submit"/>
> </p>
> </form>
>
> <?php
> }

Don't forget the last "?>".

Also, please make sure of PHP's lint mode "-l" to check your scripts.
--
Boyd Stephen Smith Jr. ,= ,-_-. =.
b...@iguanasuicide.net ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/

signature.asc

Matthew T.

unread,
Aug 7, 2011, 9:54:25 PM8/7/11
to Northwest Arkansas Linux Users Group
Thank you for the input.

On Aug 7, 12:34 am, "Boyd Stephen Smith Jr." <b...@iguanasuicide.net>
wrote:
> In <0fc7dafe-5c1e-4778-83bf-7a789a053...@f7g2000vba.googlegroups.com>, Matthew
>
> T. wrote:
> >Any suggestions as to why the code below gives me undefined variable
> >errors in xampp 1.7.4 but ran fine on Hostmonster's servers? Maybe
> >xampp has stricter error reporting?
>
> Most likely.
>
> ><?php
> >                            if(isset($_POST['submit'])) {
> >                            $question = $_POST['question'];
> >                            $name = $_POST['name'];
> >                            $email = $_POST['email'];
> >                            $subject1 = $_POST['subject1'];
> >                            $comments = $_POST['comments'];
>
> These 5 variables my still show up as unset, if the POST data is either non-
> existent or lacks these keys.  Also, since this is in a conditional the will
> not be set is the POST data does not contain the "submit" key.
>
>
>
>
>
>
>
> >                            $output_form = false;
>
> >                            if($question != 4 && $question != 'four') {
> >                            echo 'Try the security question again.<br />';
> >                            $output_form = true;
> >                            }
>
> >                            if($question == 4 || $question == 'four')
> >                            {
> >                            $to = 'i...@caneycemetery.com';
> >                            $subject = $subject1;
> >                            $msg = "From $name \n\n$comments";
> >                            $bcc = '...@google.com';
>  signature.asc
> < 1KViewDownload- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages