PDA

View Full Version : Need Help with php...



b52_00
03-14-2007, 03:54 PM
I'm a newbie here please go light on me.. I got this code here that I'm trying to figure out but I can't get it to work... can someone please take a look at the code and tell what I'm doing wrong it's driving crazy..Thanks

//------------------------------------------------
// File: 'phpmail.php'
// Func: using mail();
//------------------------------------------------

$Subject = "Test E-mail";
$toEmail = "MyEmail@somewhere.com";

if($submit)
{
mail($fromEmail, $Subject, $nMessage."\nFrom: ".$fromName."<".$fromEmail.">");
}
?>

<html>
<head>
<title>Mail Tutorial</title>
</head>
<body bgcolor="#FFFFFF">
<form method="post" action="<? echo($PHP_SELF) ?>">
Your E-mail:

<input type="text" name="fromEmail" size="25"> <br>

Your Name: <input type="text" name="fromName" size="25"> <br>

Your Message: <br>

<textarea cols="50" rows="5" name="nMessage"> Message ...</text> <br>

<input type="submit" value="Submit">

</body>
</head>

Ezekiel
03-14-2007, 05:14 PM
I'm a newbie here please go light on me.. I got this code here that I'm trying to figure out but I can't get it to work... can someone please take a look at the code and tell what I'm doing wrong it's driving crazy..Thanks


Where did you find this tutorial (and code)? The writer clearly doesn't even have a beginner's knowledge of PHP -- there are many errors that exist due to the writer just putting in what they think should go there, the same as pseudocode. In fact, I'd say they don't even have a knowledge of HTML, looking at the code. Tell the author that they should only write tutorials on things they have knowledge of.

If it's your own code, that's acceptable because you're a beginner. I'm writing the rest of my comments under the assumption that a tutorial-writer wrote that code, so don't take offense if it's in fact your own.

Where to begin...


//------------------------------------------------
// File: 'phpmail.php'
// Func: using mail();
//------------------------------------------------

No opening PHP tag = no PHP, as far as the interpreter is concerned.


if($submit)

There are ways to check if form post data has been submitted, but inventing a variable with the name of 'submit' is not one of them. The variable named 'submit' does not exist. Form data submitted using the post method logically is stored in the array $_POST["variablenamehere"], as you'll find in all HTML/PHP form tutorials.


mail($fromEmail, $Subject, $nMessage."\nFrom: ".$fromName."<".$fromEmail.">");

The arguments to the function are all in the wrong order and the naming scheme is broken because nMessage doesn't contain numerical data (n prefix = number). Suffice to say, it wouldn't work.

Also -- and this is a major problem -- none of those variables exist. When form post data is sent to PHP, it is put into the $_POST[] array, so if you sent a variable named fromEmail, you could access it with $_POST["fromEmail"].


<textarea cols="50" rows="5" name="nMessage">

Again, the 'n' prefix in a variable name should be a variable containing a number.


</text>

There's no such HTML tag as 'text'. 'textarea' should be the one to be closed.


</head>

Don't you mean </html>?

This may seem complicated to you, but it shouldn't be to someone confident enough in the language to write tutorials for others. His tutorials just seem to be disinformation. I can explain individual errors more if you want.

Here's a quick re-write of the code I did; there may be errors but you get the idea. I've unnecessarily added XHTML-style tag-closers and labels instead of normal text:


<?php

if(isset($_POST["fromEmail"]) && isset($_POST["fromName"]) && isset($_POST["toEmail"]) && isset($_POST["subject"]) && isset($_POST["message"]))
{
mail($_POST["toEmail"], $_POST["subject"], $_POST["message"], "From: " . $_POST["fromName"] . " <" . $_POST["fromEmail"] . ">\r\nTo: " . $_POST["toEmail"]);
}
else
{
exit("Go back and fill in all the details properly.");
}

?>
<html>
<head>
<title>
Mail Tutorial
</title>
</head>
<body>
<form method="post" action="<?php echo($PHP_SELF); ?>">
<input type="text" name="fromEmail" id="fromEmail" size="25" />
<label for="fromEmail">
Your e-mail:
</label>
<br />
<input type="text" name="fromName" id="fromName" size="25" />
<label for="fromEmail">
Your name:
</label>
<br />
<input type="text" name="toEmail" id="toEmail" size="25" />
<label for="toEmail">
To:
</label>
<br />
<input type="text" name="subject" id="subject" size="25" />
<label for="subject">
Subject:
</label>
<br />
<br />
<textarea cols="50" rows="5" name="message" id="message">
Enter e-mail message here.
</textarea>
<label for="message">
Your message:
</label>
<br />
<input type="submit" value="Send!" />
</form>
</body>
</html>

b52_00
03-14-2007, 09:35 PM
Hey Mike*0*,
Number one..Thanks for taking a look at this code.. and yes I got it from a site called kirupa.com..well anyway I was doing alot of researching trying to figure out how to have a submit type of box were people can put there name, blog and submitted to my email address ( I hope I'm making sense here) thinking it was going to be easy (wrong) I guess I was a little way over my head trying to figure it out..but at least I'm trying and learning how to program,php or html (please correct me if I'm wrong)..lol...Yes I would like it if you can go more into details with the errors if you don't mind..and one more thing.... now that you have changed it around a bit were do I stick my email address so when somebody hit's the send button it will be sent to my email?

nozf3r4tu
03-14-2007, 10:02 PM
if what you are trying is to have a form in your site,take a look at :
mycontactform.com.
It will allow you to create a form,then just copy and paste te html code in your page.It will send you the info to a specified email adress.
just my 2 cents

Ezekiel
03-15-2007, 12:57 PM
now that you have changed it around a bit were do I stick my email address so when somebody hit's the send button it will be sent to my email?

It should do that in theory, but I haven't hosted it anywhere to test so there could be small errors. Best way is to try it for yourself.

I'll add some more detail about those errors:


No opening PHP tag = no PHP, as far as the interpreter is concerned.

PHP code is placed in files with the .php extension. Inside these files, both PHP code and normal HTML or XHTML (or other browser language) are mixed together, so we need to tell the PHP interpreter which parts are PHP code and which parts are standard HTML. This is done by enclosing all PHP code in a special tag, as shown below:


<?php

echo("This is PHP code.");

?>

Everything between the opening PHP tag and the closing tag is treated as PHP and handled accordingly. Everything else is left unaltered and sent back to the browser.


if($submit)

I think I explained this one; the $submit variable does not exist. To check if a form is submitted you would check using the $_POST array.

In your code, the top block of PHP was not enclosed in the proper tag so would be treated as plain HTML and sent to the browser as-is.


mail($fromEmail, $Subject, $nMessage."\nFrom: ".$fromName."<".$fromEmail.">");

Nothing more to add on this one.


<textarea cols="50" rows="5" name="nMessage">

Although not strictly necessary, it's common practice in programming to give variables meaningful names. Many people use standardized 'naming schemes' to ensure good readability of code. This means giving variables names depending on what they contain, and commonly in languages such as C++ people name number-containing variables 'nVariable', null-terminated strings 'szVariable' and so on. I was remarking that, although this sort of naming is not really done at all in HTML, naming that textarea and data nMessage was wrong because it contained a string; not a number.


</text>

In this case, the tag you had previously opened was <textarea>, so here you should have written </textarea>.


</head>

HTML documents have the basic structure shown below:


<html>
<head>
</head>
<body>
</body>
</html>

At that point in the code, you should have written </html>