10 Replies - 916 Views - Last Post: 02 July 2012 - 07:00 PM Rate Topic: -----

#1 EdMarx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 12

Email Form

Posted 23 June 2012 - 12:44 PM

Hello Everyone :-),

I am trying to make a contact page and having a hard time.

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.

SITE I AM WORKING ON

Thanks for all help.

Is This A Good Question/Topic? 0
  • +

Replies To: Email Form

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

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".
Was This Post Helpful? 0
  • +
  • -

#3 EdMarx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 12

Re: Email Form

Posted 23 June 2012 - 02:08 PM

View PostJackOfAllTrades, 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.

I will try these though thanks!
Was This Post Helpful? 0
  • +
  • -

#4 EdMarx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 12

Re: Email Form

Posted 23 June 2012 - 02:27 PM

View PostJackOfAllTrades, on 23 June 2012 - 01:15 PM, said:

There are a bazillion contact form tutorials on th....

Ok I found one. i am using protoform.

havent uploaded to server yet but I dont see a place to put the email to send the form to.

Do I just put

 $email_to = "advertisingamerica@gmail.com";

in the send.php file?

the form calls mail.php

and mail.php looks like this

<?php
	echo "<div id=\"response\">";


  $name            = $_POST['name'];
  $email           = $_POST['email'];
  $telephone       = $_POST['telephone'];
  $preference      = $_POST['preference'];
  $message         = $_POST['message'];
  
  if ( ($name=='') && ($email=='') && ($telephone=='') && ($message=='')) { 
  
  		echo "<p>Please fill all the required fields<span>*</span></em></p>";
  	
  }
  else {
  
	 	echo "<p>Submitting ok, server response:</p>";
		echo "<ul>";
		echo "<li>Name: ".$name."</li>";
	 	echo "<li>E-mail: ".$email."</li>";
		echo "<li>Telephone: ".$telephone."</li>";
		echo "<li>Preference: ".$preference."</li>";
		echo "<li>Message:".$message."</li>";
		echo "</ul>";
  }  
  echo "</div>";
?>         
            
            

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,142
  • Joined: 08-August 08

Re: Email Form

Posted 23 June 2012 - 05:59 PM

This is hideous:
  $name            = $_POST['name'];
  $email           = $_POST['email'];
  $telephone       = $_POST['telephone'];
  $preference      = $_POST['preference'];
  $message         = $_POST['message'];


Would you do this?
$C = $A;
$D = $B;
$Answer = $C + $D;


Or would you simply do this?
$Answer = $A + $B;

If you got that from a tutorial then find another tutorial. One not by the same author.

Oh, and to send mail you're actually going to want to use something that can, you know, send mail. Maybe mail.

This post has been edited by CTphpnwb: 23 June 2012 - 06:00 PM

Was This Post Helpful? 1
  • +
  • -

#6 EdMarx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 12

Re: Email Form

Posted 02 July 2012 - 11:51 AM

View PostCTphpnwb, on 23 June 2012 - 05:59 PM, said:

This is hideous:
  $name            = $_POST['name'];
  $email           = $_POST['email'];
  $telephone       = $_POST['telephone'];
  $preference      = $_POST['preference'];
  $message         = $_POST['message'];


Would you do this?
$C = $A;
$D = $B;
$Answer = $C + $D;


Or would you simply do this?
$Answer = $A + $B;

If you got that from a tutorial then find another tutorial. One not by the same author.

Oh, and to send mail you're actually going to want to use something that can, you know, send mail. Maybe mail.



Didn't get it from a tutorial. It is made by the prototype guys.

prototype

Maybe you can contact them and teach them how php.

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.
Was This Post Helpful? 0
  • +
  • -

#7 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Email Form

Posted 02 July 2012 - 01:52 PM

View PostEdMarx, on 03 July 2012 - 05:51 AM, said:

Didn't get it from a tutorial. It is made by the prototype guys.
prototype
Maybe you can contact them and teach them how php.

Hey CTphpnwb, whlie you're talking to the Prototype guys, can you tell them that this:
if ( ($name=='') && ($email=='') && ($telephone=='') && ($message=='')) { 
  
  		echo "<p>Please fill all the required fields<span>*</span></em></p>";
  	
  }
  else {


...should be this...
if ( ($name=='') || ($email=='') || ($telephone=='') || ($message=='')) { 
  
  		echo "<p>Please fill all the required fields<span>*</span></em></p>";
  	
  }
  else {


Was This Post Helpful? 3
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,142
  • Joined: 08-August 08

Re: Email Form

Posted 02 July 2012 - 02:29 PM

I think I'm pretty good at PHP, but I still don't know what a php form is. I know HTML has forms, but I've never seen one in PHP!

Here's a tutorial on sending mail but bypassing the mail() function and communicating directly with your smtp server:
http://www.dreaminco...hp-smtp-direct/
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,983
  • Joined: 08-June 10

Re: Email Form

Posted 02 July 2012 - 05:14 PM

View PostEdMarx, on 02 July 2012 - 06:51 PM, said:

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.

The first step, like I say, is always the traditional HTML form:
<form id="theForm" action="process.php" method="post">
	<input type="text" name="someValue"/><br/>
	<input type="text" name="someOtherValue"/><br/>
	<input type="submit" value="Send!"/>
</form>



Now, the PHP script to deal with that form should start of like this:
<?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:
var onPageLoad = function() {
	// Fetch a reference to the <form> element.
	var theForm = document.getElementById("theForm");
	if (theForm) {
		// Function to call when the form is submitted.
		var onsubmit = function(event) {
			if (event.preventDefault) {
				event.preventDefault();
			} 
			else {
				event.returnValue = false;
			}
			
			// Send your for data via AJAX here...
		};
		
		// Trigger the function when the form is submitted.
		if (theForm.addEventListener) {
			theForm.addEventListener("submit", onsubmit, false);
		} 
		else {
			theForm.attachEvent("onsubmit", onsubmit);
		}
	}
}

// Trigger the onPageLoad function when the page is loaded.
if (window.addEventListener) {
	window.addEventListener("DOMContentLoaded", onPageLoad, false);
} 
else {
	// Microsoft IE 8 and earlier needs this.
	window.attachEvent("onload", onPageLoad);
}


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:
window.addEventListener("DOMContentLoaded", function() {
	// Fetch a reference to the <form> element.
	var theForm = document.getElementById("theForm");
	if (theForm) {
		// Trigger a function when the form is submitted.
		theForm.addEventListener("submit", function(event) {
			event.preventDefault();
			
			// Send your for data via AJAX here...
		}, false);
	}
}, false);




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:
$(function() {
	$("#theForm").submit(function(e) {
		e.preventDefault();
		// Send your for data via AJAX here...
	});
});


Doesn't get much simpler than that.

This post has been edited by Atli: 02 July 2012 - 05:15 PM

Was This Post Helpful? 2
  • +
  • -

#10 EdMarx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 12

Re: Email Form

Posted 02 July 2012 - 05:57 PM

View PostAtli, 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.

My link

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?
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,142
  • Joined: 08-August 08

Re: Email Form

Posted 02 July 2012 - 07:00 PM

View PostEdMarx, 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:
<h1>Hello World</h1>
<?php
echo '<h2>Hello!</h2>';
?>
<h3>Goodbye</h3>

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.

This post has been edited by CTphpnwb: 02 July 2012 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1