/    Sign up×
Community /Pin to ProfileBookmark

mail form in PHP for a newbie

hi everybody.

I absolutely don’t know nothing about PHP, I was reading different post here but anything easy enough for me to be understood.

I have a this site
[url]www.cattaneoalessandro.com[/url]

What I would like to have is

a form to send a mail to a mail address
once u fill in some fields u can click “submit” or “cancel”

“submit” – a page appears and sayd that ur mail has been sent
“cancel” – i don’t really care, it may happen that on the page u see something like “ur mail has been deleted” or just that the fields turn to blank again.

How can I do it with PHP? Do I need to install somehting?
I’ve checked and my hosting allows PHP.

just…explain it like if u were talking to a person tht turn on a PC for the first tiem.

It’s pretty urgent…thanks in advance.
Ale

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 20.2006 — Ciao,

Is it the form that you have on this page: http://www.cattaneoalessandro.com/pages/contattaci.htm ?

If yes, you will need to create a file, for example "send_mail.php" (in the same directory as contattaci.htm if you want it simple)

In contattaci.htm, change the action attribute of the form tag to that file (send_mail.php) and the methode attribute to "post" (instead of "get")

now you would have to validate each required field (Let's say all of them are needed), so you would have to check if they are all not empty.

And make other sorts of validation (for example, validate the e-mail address)

functions that you will need are the following:

[url=http://php.net/isset]isset()[/url]

[url=http://php.net/empty]empty()[/url]

[url=http://php.net/mail]mail()[/url]

Your cancel (reset) button will automatically reset the fields to their initial values.

and example for send_mail.php
[code=php]
<?php
# checks if submit button has been pressed
if(isset($_POST['Invia'])){
# put the POST data in more confortable variable names
$cognome = $_POST['Cognome'];
$nome = $_POST['Nome'];
# etc...

# do all the validation that you need
# check if all fields are filled, and if the e-mail address is valid

# sending e-mail
# email, oggetto and testo set above

# additional header (from address)
$headers = "From: [email protected]";

$mail = mail($email, $oggetto, $testo, $headers);

if($mail){
# mail was sent, display message
echo "Mail sent!";
}else{
# mail was not sent, display form and error message
echo "Error";
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 20.2006 — mmm I'm pretty lost. OK...what I've done is

I've create a send_mail.php file in the same directory of contattaci.html;

In contattaci.html, for the form, I've change action and method like this [code=php]<form action="send_mail.php" method="post"[/code]

but then? I've was getting info from the links u've mentioned...but I do not understand.

I was trying with empty() for the variable cognome....but i think i'm not doing right.

Here I enclose the php file.

Thanks for ur help.

Ale

PS

are u italian?

[upl-file uuid=6341d2a5-ce32-4ba4-a00a-6dbed4d7392e size=873B]send_mail.zip[/upl-file]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 20.2006 — ?

No i am not italian

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
# checks if submit button has been pressed
if(isset($_POST['Invia'])){
# put the POST data in more confortable variable names
$cognome = $_POST['Cognome'];
$nome = $_POST['Nome'];
$email = $_POST['mail'];

# optional infos
$via = $_POST['Via'];
$paese = $_POST['paese'];
$provincia = $_POST['Provincia'];

# etc...
$oggetto = $_POST['Oggetto'];
$testo = $_POST['testo'];

# do all the validation that you need
# check if all fields are filled, and if the e-mail address is valid
if(!empty($cognome) && !empty($nome) && !empty($email) && !empty($oggetto) && !empty($testo)){
$email_pattern = "/^[-^!#$%&'*+/=?`{|}~.w]+@[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*(.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/";
if(preg_match($email_pattern, $email)){
# e-mail address should be valid
# sending e-mail
# email, oggetto and testo set above

# you might want to add the optional infos to testo, or to send an html e-mail

# additional header (from address)
$headers = "From: $email";

$mail = mail("[email protected]", $oggetto, $testo, $headers);

if($mail){
# mail was sent, display message
echo "e-mail inviata. Grazie per averci scritto.";
}else{
# mail was not sent, display form and error message
echo "e-mail non inviata.";
}
}else{
# e-mail address will most probably not be valid
}
}else{
# one or more required fields were left empty (cognome, nome, email, oggetto or testo)
}


}
?>
</body>
</html>

[/code]


This should work,

one thing i did wrong in the previous code was that i was sending the e-mail to the guy who sent the mail, haha, i fixed that.

ok, i have now set up a field validation for the required fields, the "Dati facoltativi" fields can stay blank, for now they aren't even sent with the e-mail.
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 21.2006 — ehhh, super thank u for u devotion and help ?

well I have the contattaci page as u can check in the file I enclose to this post.

and in the same directory I have the send_mail.php exactely as u wrote me (anyway I enclose this file too).

BUT: in contattati.html, I fill the mandatory field, then click OK and what I got is the browser saying if I want to open or save a php script, like if I'm downloading or saving a file.

What do I do wrong?

thanks in advance

Ale

[upl-file uuid=1d9f3a52-3176-4339-953e-697d0864bf3a size=2kB]pages.zip[/upl-file]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 21.2006 — This would usually mean that PHP is not installed on the server.

Try this, create a new file, anywhere on the server, name it whatever you want, info.php or phpinfo.php, whatever, and write this inside:
[code=php]
<?php
phpinfo();
?>


[/code]

then type the url to the file in your browser, and tell me what you see, or place a link here to the file
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 21.2006 — ah OK! i didn't get this! So maybe everything is right cos I was testing the file just locally, on my PC...from the office I can not upload files on the server that I do think has PHP installed.

I will try later from home.

thanks a lot!
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 21.2006 — Yeah, you would need a webserver and PHP installed on your local machine in order to test it
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 21.2006 — and a mail server
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 21.2006 — ehm sorry but I'm lost again...

I've downloaded the [URL=http://it.php.net/downloads.php]PHP 5.1.2[/URL]

and now?

Do I have to upload the entire directory on the server?

How can I install it on my PC?

And what abt the mailserver?

thanks again
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 26.2006 — Please please help!

I've installed everything: apache server and PHP. they are both correctly installer...but when use the submit button in my contattaci page i get the window that says if I wanna save or dowload the file...please help!

thanks in advance
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 26.2006 — try what i said in post 6 (http://www.webdeveloper.com/forum/showpost.php?p=536381&postcount=6) and tell me what you see
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 26.2006 — this is the link

http://www.cattaneoalessandro.com/pages/info.php

and in the browser I see a long long list of data abt PHP version 4.4.1 that it's the on i've installed.

I enclose a screen shot of what i see...but the scroll is much longer.

Maybe the error is in the send_mail.php?

I don't know...I don't understand anything :mad:

thanks a lot

[upl-file uuid=d27f2dfa-c951-47e6-b23e-0d967f089710 size=88kB]screenPHP.jpg[/upl-file]
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 26.2006 — mmm I think something is working

If u go to

http://www.cattaneoalessandro.com/pages/contattaci.htm

and u fill in the required fields adn then click OK, the message that says the e-mail has been succesfully sent appears...but I would like to see it

in the page with the same look & feel of the rest of the site. At the moment it apperas in a blank page.

Another question...maybe i just have to wait some time...but for the moment the mail I've sent doesn't go anywhere.

Any suggestion?

thanks in advance.

Ale

PS

I enclose the send_mail.php page

[upl-file uuid=689cc500-7146-4598-bf60-57868f3db8b5 size=1kB]send_mail.zip[/upl-file]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 26.2006 — you mean that you don't get the e-mails?
Copy linkTweet thisAlerts:
@alexthecattaauthorMar 26.2006 — mmm yeah for the moment i don't get anything.

but I also mean that if the required fields are correctly full and i click submit, I would like the message "your email has been sent" woudl appear in a page with header, nav bar and so on, not in completly blank page.
×

Success!

Help @alexthecatta spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...