Jump to content

Undefine Variable Error


sunny-

Recommended Posts

Greetings everyone, I am a really new php coder and I got stuck in a problem while writting a practice code. This is about a Feedback page that sends an email after getting the Feedback.

 

The error Recieved basically states that the variables are undefined. so That means there is some problem while passing the values of the variables. I really cannot Figure it out. Please help me.

 

 

The Feedback Form Page is---

 

<HTML>
<HEAD>
<TITLE>The Feedback Form</TITLE>
</HEAD>
<BODY>
<FORM method="POST" action="sf.php">
<p>Your Name: <INPUT type="text" name="sender_name" size=20 maxlength=50> </p>
<p>Your Email: <INPUT type="text" name="sender_email" size=20 maxlength=50> </p>
<p>Message: <textarea name="message" cols=30 rows=5></textarea></p>
<p>Do you like this Website:
<INPUT type="radio" name="like_site" value="Yes" checked> yes
<INPUT type="radio" name="like_site" value="No"> no
</p>
<INPUT type="submit" value="SUBMIT">

</FORM>
</BODY>
</HTML>

 

The Page which is responsible for sending the email is

 

<HTML>
<HEAD>
<TITLE>The Send Feedback</TITLE>
</HEAD>
<BODY>
<?php_track_vars?>
<?php

   $msg="Sender's Full Name:\t $sender_name \n";
   $msg.="Sender's Email:\\t$sender_email\n";
   $msg.="Did you like the Website?\t$like_site\n";
   $msg.="Additional Message:\t$message\n\n";
   $mailheaders="From: Lord Sunny's Website";
   $mailheaders.="Reply-To: $sender_email\n\n";
   mail("sunnymay1993@hotmail.com", "Feedback Form From--$sender_name",$msg,$mailheaders);
   echo"<H1 align=centre> Thank you, $sender_name";
   if($sender_name=='yes')
       echo"<P align=centre> Thank you for Liking our Website.</p>";
   else
       echo"<P align=centre> We will try to improve our website as per your feedback.</p>";
?>
</BODY>
</HTML>

 

 

Sorry if I am posting this in an inappropiate forum.

Link to comment
Share on other sites

Try this:

<?php
$message = $_POST["message"];
$sender_name = $_POST["sender_name"];
$sender_email = $_POST["sender_email"];
$like_site = $_POST["like_site"];


$msg="Sender's Full Name:\t $sender_name \n";
$msg.="Sender's Email:\\t$sender_email\n";
$msg.="Did you like the Website?\t$like_site\n";
$msg.="Additional Message:\t$message\n\n";
$mailheaders="From: Lord Sunny's Website";
$mailheaders.="Reply-To: $sender_email\n\n";
mail("sunnymay1993@hotmail.com", "Feedback Form From--$sender_name",$msg,$mailheaders);
echo"<H1 align=centre> Thank you, $sender_name";
if($like_site == 'Yes') {
echo "<p align='centre'> Thank you for Liking our Website.</p>";
} else {
echo "<p align='centre'> We will try to improve our website as per your feedback.</p>";
}
?>

 

Corrected this:

- I've defined the values of the form with $_POST[];

- The "if($like_site == 'Yes')" was set to "if($sender_name == 'Yes')"

Edited by Silvar
Link to comment
Share on other sites

Silvar's Solution Seems to be working.Thank you very much. But why do I have to use another set of varibales just to hold whatever I actaully passed? Isent that a bit of waste of Space? because I am storing the same thing which I already have direct access to. Or is this the way it is done in php?

 

 

 

As for the isset() function. I tired it and it seems like the $_POST is actaully set.

 

The new sf.php with the isset() ====

 

<HTML>
<HEAD>
<TITLE>The Send Feedback</TITLE>
</HEAD>
<BODY>
<?php_track_vars?>
<?php

if(isset($_POST))
echo "IT is set.";

$msg="Sender's Full Name:\t $sender_name \n";
$msg.="Sender's Email:\\t$sender_email\n";
$msg.="Did you like the Website?\t$like_site\n";
$msg.="Additional Message:\t$message\n\n";
$mailheaders="From: Lord Sunny's Website";
$mailheaders.="Reply-To: $sender_email\n\n";
mail("sunnymay1993@hotmail.com", "Feedback Form From--$sender_name",$msg,$mailheaders);
echo"<H1 align=centre> Thank you, $sender_name";
if($sender_name=='yes')
echo"<P align=centre> Thank you for Liking our Website.</p>";
else
echo"<P align=centre> We will try to improve our website as per your feedback.</p>";
?>
</BODY>
</HTML>

 

So it is suppose to print "IT is Set" if isset($_POST) returns a 1;

 

So It is printing, so that means that the variables are passed properly? If so then why does it still say that the Variable is undefined.

Edited by sunny-
Link to comment
Share on other sites

See my reply. As

Silvar's Solution Seems to be working.Thank you very much. But why do I have to use another set of varibales just to hold whatever I actaully passed? Isent that a bit of waste of Space? because I am storing the same thing which I already have direct access to. Or is this the way it is done in php?

 

 

 

As for the isset() function. I tired it and it seems like the $_POST is actaully set.

 

The new sf.php with the isset() ====

 

<HTML>
<HEAD>
<TITLE>The Send Feedback</TITLE>
</HEAD>
<BODY>
<?php_track_vars?>
<?php

if(isset($_POST))
echo "IT is set.";

$msg="Sender's Full Name:\t $sender_name \n";
$msg.="Sender's Email:\\t$sender_email\n";
$msg.="Did you like the Website?\t$like_site\n";
$msg.="Additional Message:\t$message\n\n";
$mailheaders="From: Lord Sunny's Website";
$mailheaders.="Reply-To: $sender_email\n\n";
mail("sunnymay1993@hotmail.com", "Feedback Form From--$sender_name",$msg,$mailheaders);
echo"<H1 align=centre> Thank you, $sender_name";
if($sender_name=='yes')
echo"<P align=centre> Thank you for Liking our Website.</p>";
else
echo"<P align=centre> We will try to improve our website as per your feedback.</p>";
?>
</BODY>
</HTML>

 

So it is suppose to print "IT is Set" if isset($_POST) returns a 1;

 

So It is printing, so that means that the variables are passed properly? If so then why does it still say that the Variable is undefined.

See my first reply. As en example $sender_name doesn't give you the output of the form, $_POST['sender_name']; does, so you need to define it.

Link to comment
Share on other sites

Whatever resource your learning php from is severely outdated. The functionality you are expecting has been disabled for like 10 years. The functionality would magically register globals into local scope. You now have to get the data you are looking for from the $_POST array.

 

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.