/    Sign up×
Community /Pin to ProfileBookmark

collecting form data and sending email

Hi everyone. I’m brand new to PHP and I’m having difficulties getting this simple script to work. Basically what I’m trying to do is build a form in HTML which asks for the user’s name and email address, and then sends that information to a php script that will send an email to that user. Here’s my attempt at it.

Here’s the code in the HTML form. This file is named formPractice.html:

[code]
<!DOCTYPE public HTML “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en”>
<head>
<title>PHP & Form practice</title>
<meta http-equiv=”content-type” content=”text/html; charset=”ISO-8895-1″/>
</head>
<body>

<form action=”formPractice.php” method=”post”>
<p>Please input your name:<br/>
<input type=”text” name=”name” id=”name”/>
</p>
<p>
Now input your email address:<br/>
<input type=”text” name=”email” id=”email”/>
</p>
<input type=”submit” name=”submit” id=”submit” value=”submit”/>
</form>
</body>
</html>
[/code]

and here’s the code for the php file that will handle the form data. This file is named formPractice.php:

[code]
<?php
$email = $_POST[“email”];
$name = $_POST[“name”];
$emailBody = “Thank you for placing your order with us “.$name.”. You will be receiving your

package in a day or two. If you have any questions please feel free to contact me (415)378-

9068.”;
mail($email, “confirmation”, $emailBody);
?>
[/code]

When I open the file called formPractice.html and fill in the form entries, and click the submit button, I get a dialog box that asks me which program I want to use to open up the formPractice.php file and instead of executing that file, it’ll just open and display the contents. Is there something I’m missing here?

Also, how come on my laptop, I can’t save the document as a php file. I create the script in notepad and when I go to save it I name it something like phpDocument.php, but when I look at the file type it’s still a text file with the name phpDocument.php. So basically it’s phpDocument.php.txt. But when I do the same thing on my desktop, I get the desired file type.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@Little_GoatJun 16.2006 — you have to have your scripts on a server. you can download something such as XAMPP (but then it's kind of hard to set up emailing) or you can put it on a webhost. php scripts are server-side, and that is why it only works on a server. as for the laptop, the file type PHP isn't on your computer. you will have to do that manually.
Copy linkTweet thisAlerts:
@mark_yiehauthorJun 16.2006 — but I've tested it on my server also and it doesn't work either. www.mark-yieh.com/formPractice.html.
Copy linkTweet thisAlerts:
@chazzyJun 16.2006 — you might have to put up some headers as well into your mail function. try speaking to the support at your host - they would know best.
Copy linkTweet thisAlerts:
@mark_yiehauthorJun 17.2006 — ok I got it to work, but I ran into another problem. When I type in my yahoo email address, the php script responds and sends an email to that account, but when I type in my hotmail address, the php script doesn't work. It seems that this has something to do with the email accounts but that doesn't make sense though. If it works for one, shouldn't it work for all? Unless not all email accounts are the same.

And in regards to this answer:

[I]as for the laptop, the file type PHP isn't on your computer. you will have to do that manually.[/I]

How do I go about doing that?

Thanks everyone.
Copy linkTweet thisAlerts:
@Little_GoatJun 17.2006 — well, what operating system do you have? I know in 98, after it asks what to open it with, you should set it to notepad so you can edit it easily (or any other editor) and check the box that says "always use this to open this type of file" or something like that.

I hope I'm understandable. sorry if I'm not. :o

LG
Copy linkTweet thisAlerts:
@chazzyJun 18.2006 — ok I got it to work, but I ran into another problem. When I type in my yahoo email address, the php script responds and sends an email to that account, but when I type in my hotmail address, the php script doesn't work. It seems that this has something to do with the email accounts but that doesn't make sense though. If it works for one, shouldn't it work for all? Unless not all email accounts are the same.[/QUOTE]


What does the mail server say? It seems like your sendmail is rejecting it based on where it's going or coming from, based on possible spam.
×

Success!

Help @mark_yieh 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.18,
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,
)...