Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to PHP, Keep gettng undefined Variable 2

Status
Not open for further replies.

glenmac

Technical User
Jul 3, 2002
947
0
0
CA
I'm very new to PHP and when trying to pass a value from a form field I keep getting an udefined varialbe error. Here's a snippet of code.
The form;
Code:
<form action=&quot;add.php&quot; method=&quot;post&quot;>
    <table border=&quot;1&quot; bordercolor=&quot;#000000&quot;
    bordercolordark=&quot;#000000&quot; bordercolorlight=&quot;#000000&quot;>
        <tr>
            <td bgcolor=&quot;#35C0D0&quot;><p align=&quot;left&quot;><font size=&quot;5&quot;>Join
            the mailing list</font></p>
            </td>
        </tr>
        <tr>
            <td bgcolor=&quot;#008080&quot;>NAME:<font size=&quot;4&quot;><input
            type=&quot;text&quot; size=&quot;20&quot; name=&quot;name&quot;></font></td>
        </tr>
        <tr>
            <td bgcolor=&quot;#008080&quot;>EMAIL:<font size=&quot;4&quot;><input
            type=&quot;text&quot; size=&quot;20&quot; name=&quot;email&quot;></font></td>
        </tr>
        <tr>
            <td bgcolor=&quot;#008080&quot;><p align=&quot;right&quot;><INPUT TYPE=&quot;image&quot; SRC=&quot;ok.gif&quot;></p>
            </td>
        </tr>
    </table>
    <p> </p>
</form>
Here is the php page the form posts too;
Code:
<?php
$filename = &quot;members.txt&quot;;
$fd = fopen ($filename, &quot;r&quot;);
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
if(strstr($contents,$email)) { 
print &quot;Your already subscribed to this mailing list.&quot;;
}
else {
echo &quot;Thank you $email for joining the mailing list, you should recieve a confirmation email from us within 5 minutes and you will get an email from us every week, blah&quot;;
if (!$save = fopen(&quot;members.txt&quot;,&quot;a&quot;)) {
exit;
}
fwrite($save,&quot;$email\r\n&quot;);
fclose($save);
if (!$save = fopen(&quot;names.txt&quot;,&quot;a&quot;)) {
exit;
}
fwrite($save,&quot;$name\r\n&quot;);
fclose($save);
//I'm assuming that X-Mailer is a module I'll have to load
//At this point all I want to do is fget the values from 
//the form
mail(&quot;$email&quot;, &quot;Compustorm Mailing List&quot;, &quot;This email is to confirm that you have been added to the compustorm mailing list.&quot;,
     &quot;From: MailList@compustorm.com\r\n&quot;
    .&quot;Reply-To: DONTEMAILBACK@compustorm.com\r\n&quot;
    .&quot;X-Mailer: PHP/&quot; . phpversion());
}
?>
these are the errors I get;
Notice: Undefined variable: email in c:\inetpub\ on line 6

Notice: Undefined variable: email in c:\inetpub\ on line 10
Thank you for joining the mailing list, you should recieve a confirmation email from us within 5 minutes and you will get an email from us every week, blah
Notice: Undefined variable: email in c:\inetpub\ on line 14

Notice: Undefined variable: name in c:\inetpub\ on line 19

Notice: Undefined variable: email in c:\inetpub\ on line 21
//this error I'll deal with later unless someone can help
Warning: Bad Message destination in c:\inetpub\ on line 24

I got this code from and followed the instructions as closely as I could.I'm using IIS for my server and the instructions are for Apache.I'm trying to teach myself PHP by checking other peoples code. No matter what code I try when passing a value from a form to a PHP page I get the undefined variable error. I am used to coding with ASP and use request.form method for transfering values. As I've said I'm real new to php and can't seem to get a value from a form in this language. Can anyone help me with the solution of how to do this in PHP?
All help would be greatly appreciated.
 
If you want to use the names on the form as the variable name you need register_globals turned on. You can check that using phpinfo() function call.

Now, in order to access the form variables if the register_global is off, you can use $_POST and $_POST[name], etc.

Dan
 
I recommend that you use the superglobal variables ( whether you have register_globals turned on or not.

Six months from now you may not remember how the variable $foo got its value. You you will have a good idea how the value got into $_POST[&quot;foo&quot;] Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks guys I didn't know about global variables(but I'm learning) A star for both of you! I'm assuming I edit the php.ini file to turn them on and off.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top