ThePHPthread

2 posts

Flag Post

so it was bound to happen.Discuss all php problems,ideas,plans and designs here.Ok i guess i should add some rules and examples.
RULES:
1.Spamming this thread will get you banned
2.Abusive language will get you banned
3.False script will get you banned
ok i wil start with a very simple tutorial:
Basic PHP forms:

In this tutorial, I will be showing you how to use forms with PHP, and saying some errors that you might make with basic forms.

There are 2 ways in which to use forms. The first (more common), is to have 2 separate files. This tutorial will be using that method, but I will probably do a tutorial on using only one file.
Right, enough chat. Let’s start coding!

Ok first, let us start with a very simple textbox form. Here is the code:

<form> <input /> <input> </form>

Right, so let’s take a closer look at this code.

The first line is the following.
<form>

The name of the form is what you need to get data sent from this form. If you have no name, or an invalid one (contains spaces e.t.c), your form will not work correctly.
The method can be either “post” or “get”. Get is very different from post, but this tutorial is based on the “post” method.
And finally, the action. The action is basically the file to go to after the user has clicked the submit button.

The second line is this:

<input />

This is quite simple. The type is the type of field, or box this is, and the name is an “ID tag”, which you need to grab data from this field.

The third line is:

<input>

This is just the submit button.
The value is what appears on the button, and you know what the name is.
In this case, the type is “submit” which tells the web browser that this field is a submit button.

And finally, the last line is this:

</form>

This just stops the form tag.

Right, so save the following code as form.html:

<form> <input /> <input> </form>

Now for recievefile.php:

<?php
$username = $_GET<’username’>;
echo (“Hello, $username.
Have a nice day!”);
?>

The first line is the following:

<?php

This just tells everyone that the following code is PHP.

The second line is this:

$username = $_GET<’username’>;

This sets the $username variable to the field with the name of “username”;

And the last 2 lines are this:

echo (“Hello, $username.
Have a nice day!”);
?>

This just outputs “Hello, {—WHATEVER-YOU-TYPED-INTO-THE-TEXT-BOX-EA
RIER—}. Have a nice day!”; and then ends the PHP code.

That is pretty much the basics of forms. There are many different field types, including textareas and checkboxes, but I will not go into them now.

Thanks a lot, hope you enjoyed the tutorial.

 
Flag Post

Mail
The mail function is used to send E-mail Messages through the SMTP server specified in the php.ini Configuration file. Thus, if it is wrong configured, it will not work. Here, you won’t learn how to make a client which will also accept, but it’s doable. You can help yourself on PEAR and PECL Repository (someone should make an Tutorial how to install/extend PHP libraries) .

PHP’s mail() function works thus:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

It is important for you to understand that it returns a boolean true/false on success or failure, respectively.

This example will send message “message” with the subject “subject” to email address “example@domain.com”. Also, the receiver will see that the eMail was sent from “Example2 <example2@domain.com>” and the receiver should reply to “Example3 <example3@domain.com>”

<?php
mail(
example@domain.tld”, // E-Mail address
“subject”, // Subject
“message”, // Message
“From: Example2 <example2@domain.com>\r\nReply-to: Example3 <example3@domain.com>) // Additional Headers
;
?>

These headers are optional though.
Also, there is no requirement to write E-mail addresses in format “Name <email>”, you can just write “email”.

This will send the same message as the first example but includes From: and Reply-To: headers in the message. This is required if you want the person you sent the E-mail to reply to you. Also, some E-mail providers will assume mail is spam if certain headers are missing so unless you include the correct headers you mail will end up in the junk mail folder.

Keep in mind, though, that by default mail() sends plain text message, so to make a HTML message, you should use this headers:

$headers = ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

Note: \r\n is actually \n, a newline, but you must use \r\n to keep compatibility.

And that’s it! To make something like newsletter, all you need is to run a loop and use mail() .