/    Sign up×
Community /Pin to ProfileBookmark

Guys….I need help D:

What i have to do seems extremely simple. I’ve checked many sites out (w3schools to about.com and smaller tutorial sites) and i still don’t know how to do it. I have a good idea of what i NEED to do.

I’m working on a pure CSS website. Everythings good there. All i need now is a contact page. I realize that there are many generators and premade scripts for this but i’m ignorant with php. I know the basic syntax. ( ? ). I do want to learn though. Thats why i’m asking for [B]help[/B] not someone to do it for me. [of course they could if they wanted to ?]. The contact page must contain 4 fields. Name, Email, Phone, and Question or comment. Heres what i’ve gathered so far. Tell me if i’m right.

I have an HTML form:

[CODE]<form>
<b>Full Name:</b><br>
<input name=”full name” type=”text” class=”form” id=”name”>
<br>
<br>
<br style=”line-height:8px”>
<b>E-mail Address:</b><br>
<input name=”email” type=”text” class=”form”>
<br>
<br>
<br style=”line-height:8px”>
<b>Telephone:</b><br>
<input name=”phone” type=”text” class=”form”>
<br>
<br>
<br style=”line-height:4px”>
<b>Your Message/Question:</b><br>
<textarea name=”message” cols=”75″ rows=”6″ class=”contactForm”></textarea>
<br>
<br>
<input name=”Submit” type=”image” value=”Send” style=”margin:0″ align=”top” src=”images/send.png”>
<br>
</form>[/CODE]

Thats about all i have too. (form wise).

So heres what i [B][U]NEED[/U][/B] it to do. Basically, take all those fields. And post them into a text file. I Don’t care about format as long as its there. I would prefer each one being on its own line, and then a few line breaks after “question” — Just so its neat. It doesn’t have to be like that though.

Heres what i’ve figured out with no real knowledge of php:
Its really hard.
Use: I need to use the _post
Use: fwrite using “append” to a text file.
And a redirect to a “success” page.

This needs to be done within 3 days. Its for a website that my parents are making me do for their church so the longer i take, the worse i look, and the worse i look, the worse they look.

Also, anyone willing to help, i’m willing to link back in a page on the site. (will be viewed by the people in the church. –430 active members)

Any advise is EXTREMELY appreciated. Thanks everyone.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@OctoberWindJul 30.2010 — One option would be something along these lines

[B]first[/B]

Properly set your form's name, method and action:
[code=php]
<form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
[/code]


[B]Second[/B]

Correct form input names:

name="full_name"

(no spaces...)

[B]Third[/B]

process form:
[code=php]
if (isset($_POST['Submit']) && $_POST['Submit'] == "Send") {
$file = "/path/to/file.txt";
$contact = date(n/j/Y g:i:s)."rn";
$contact .= "Name: $_POST['full_name']rn";
$contact .= "Email: $_POST['email']rn";
$contact .= "Phone: $_POST['phone']rn";
$contact .= "Message: $_POST['message']rnrn"
$handle = fopen($file, "a");
fwrite($handle, $contact);
fclose($handle);
}
[/code]


There's always room for more error handling and validation, but this should get you started.
Copy linkTweet thisAlerts:
@ZachhhhauthorJul 30.2010 — Thanks for replying!

Your answer got me a lot further. But now, i'm somewhat confused. I thought 'action' showed where the script is located on the server like <form action= "script.php">

Since this is now there, where do i put the form processing script?
[code=php]<?php echo $_SERVER['PHP_SELF'] ?>[/code]

I'm sorry D:
Copy linkTweet thisAlerts:
@OctoberWindJul 30.2010 — $_SERVER['PHP_SELF'] points back to the current page.

the form processing can go right before (or right after) the form on the same page.

Because the processing script is inside
[code=php]
if (isset($_POST['Submit']) && $_POST['Submit'] == "Send") {
// do something
}
[/code]


it will only run when the form is submitted (thus, "Submit" will be 'posted' and will be set to "Send" per the value.
Copy linkTweet thisAlerts:
@jeedenJul 30.2010 — all the code provided above should give you your solution.

My question is for octoberwind-

When would $_SERVER['PHP_SELF'] be more useful than a static/hard coded value?

Would you be placing the form as a php snippet include across multiple pages? Only then I could see the advantage of it being dynamic...but what kind of form would you use in multiple places?
Copy linkTweet thisAlerts:
@ZachhhhauthorJul 30.2010 — Thank you ?

I appreciate it.
Copy linkTweet thisAlerts:
@OctoberWindJul 30.2010 — I think it's more of a portability thing, than a 'multiple includes' thing. There's really no chance of the form breaking if you move this (or worse yet, some other) file from one place to another.
×

Success!

Help @Zachhhh 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...