Email Form

Does anyone know a simple tutorial for this, to uses HTML to send an email via PHP or anything else that will work.

The only thing is I cant have the contact page be a different web page. I simply need to have a form that uses CSS and Javascript for style and some function. But still uses PHP without getting in the way of the others.
In other words it cant redirect the user once the submit button is clicked.

Can I just add inline php code and call it a day?
If so how?

If you have read this far please check out what I am working on and look at my fail of a contact page.

Replies To: Email Form

Re: Email Form

Posted 23 June 2012 - 01:15 PM

There are a bazillion contact form tutorials on the net. A BAZILLION. Here's one. Of course, having a contact form can easily lead to its misuse as a spamming tool, so here's another aimed at preventing that.

Ah, but you want something via AJAX, so you're not redirected. Here's the first link in a Google search of "AJAX contact form".

Re: Email Form

Posted 23 June 2012 - 02:08 PM

JackOfAllTrades, on 23 June 2012 - 01:15 PM, said:

There are a bazillion contact form tutorials on the net. A BAZILLION. Here's one. Of course, having a contact form can easily lead to its misuse as a spamming tool, so here's another aimed at preventing that.

Ah, but you want something via AJAX, so you're not redirected. Here's the first link in a Google search of "AJAX contact form".

Ok thanks.
My only problem is the ones I have tried redirect the user to a another page. .php page.
The way my site is designed it is all on one page so I cant have a .php page for contact.

Re: Email Form

All of the examples I have been given require a .php form at some point.

I need the form to be an HTML form.

If there is a way I would simply like to call the php without redirecting the user at any point to the php page and without putting php in an iframe.

If anyone has any tutorials on how to do this it would be great.

To expand on what CTphpnwb was saying about HTML forms vs PHP forms...

There is no such thing as a "PHP form". PHP's only relationship to forms is it's ability to process HTML forms. All form processing starts with a HTML form, which you can then either have submitted to a PHP page the "old-fashioned" way, or you can create an AJAX request to send the form data to PHP. - Either way, you'll always need to have a PHP page on the receiving end to process the data.

<?php
if (isset($_POST['someValue'], $_POST['someOtherValue'])) {
// Do stuff with the form data here.
// Like, for example, send emails or save it into a database.
}
else {
die("No data found.");
}

It verifies that it has received POST data from a form and then goes on to process it: sending it as an email or just saving it to a database. Whatever you need it to do.

On top of this, you can choose to intercept the HTML form in the HTML page before it redirects to the PHP page, using Javascript. Then you can use AJAX to send the request in the background, allowing the user to stay on the same page while this happens. - Obviously this is a bit more work for you, but if done correctly this can make the user's experience a lot better.

A very simple Javascript snippet that could be used for such purposes would start like this:

This intercepts the form before it is sent, and makes it possible for you to add your AJAX code instead. (I've not added the AJAX code there, though. You'll need to study that and implement it into this.)

Also note that in that code there is a lot of IE backwards-compatibility stuff going on. Testing for the addEventListener() function, and the event.preventDefault() function. You, unfortunately, need to keep stuff like that in mind when developing Javascript code. - For comparison, without worrying about old IE compatibility, the code could be made far simpler:

Once you've gotten a hang of basic Javascript, then you may want to look into using a framework like the aforementioned Prototype framework, or jQuery. It'll take care of such concerns for you, and make most stuff like that even more simple. The same script I demonstrated above could look like this, when done in jQuery:

Re: Email Form

Posted 02 July 2012 - 05:57 PM

Atli, on 02 July 2012 - 05:14 PM, said:

To expand on what CTphpnwb was saying about HTML forms vs PHP forms...

Thanks for the reply. I meant file not form sorry about that.

I've been looking through AJAX examples and maybe I'm not looking hard enough but all the ones I see ends up with the user
looking at an .php file. As you can see I know absolutely nothing about PHP.
So your telling me there are examples out there that will use AJAX to call the .php file(or script or thingamajig)
without the user ever seeing the .php file?

If anyone has a better idea as to how to add a contact form to the contact section of this page please let me know.

Not only do I know nothing about PHP but pretty much any other script/code/programming as well.

@CTphpnwb

Thanks for the link but I don't think that will solve my problem. I could be wrong.
But it looked like the user still has to look at a file like my file.php at some point.
My problem is I have the whole site one one page and I don't want to redirect and I have the whole translucent thing going on. I've could use an iframe but I don't think I could keep the clear look.

Since you are an obvious expert at php what would you do if you had to keep the site structure the same but needed a email form?

Re: Email Form

Posted 02 July 2012 - 07:00 PM

EdMarx, on 02 July 2012 - 08:57 PM, said:

I've been looking through AJAX examples and maybe I'm not looking hard enough but all the ones I see ends up with the user
looking at an .php file. As you can see I know absolutely nothing about PHP.
So your telling me there are examples out there that will use AJAX to call the .php file(or script or thingamajig)
without the user ever seeing the .php file?

@CTphpnwb

Thanks for the link but I don't think that will solve my problem. I could be wrong.
But it looked like the user still has to look at a file like my file.php at some point.

You're misunderstanding PHP's purpose. It is a server side language. The user never sees PHP. Neither does their browser. PHP is processed only on the server, and the browser only sees what PHP ignores and/or what PHP sends. Look at this code:

The first line is ignored by the PHP interpreter and simply passed to the web server, which then passes it to the user's browser. The code between the PHP tags: <?php ... ?> is processed by the interpreter and its output is sent to the browser. The last line is treated like the first line. All of this takes place very quickly, so it may appear that it's happening in one place, but it's not. The server does some processing and then the browser does its job.

Where to place the HTML form is up to you as a designer. The PHP won't know or care where you put it. Its job is to process the information the form sends.