PHP contact form help

Soldato
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
Having sorted out the hosting issue with Go Daddy I am having problems with my contact form. I have created one using the below tutorial:

https://www.youtube.com/watch?v=5a_zy_utoFM
https://www.youtube.com/watch?v=6q7ndD0rt2s

My code is:

PHP:
<?php
$to = '[email protected]';
$subject = 'This came from XYZ';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

$message = <<<EMAIL

Hi! My name is $name.

$message

From $name
My email is $email

EMAIL;

$header = '$email';

if($_POST)
{
mail($to, $subject, $message, $header);
$feedback = 'Thanks for the email';
}
?>

			<p><span class="error">* required field.</span></p>
	
			<form action="?" method="post"> 
	
			<label for="name">Name:</label>
			<input type="text" name="name" id="name" />
			<span class="error">*</span>
			<br><br>

			<label for="email">Email:</label>
			<input type="text" name="email" id="email" />
			<span class="error">*</span>
			<br><br>

			<label for="comment">Comment:</label> 
			<textarea id="comment" name="comment" rows="9" cols="42"></textarea>
			<br><br>

			<input id="submit" input type="submit" name="submit" value="Submit">
			<br><br>
			</form>
			
			<p id="feedback"><?php echo $feedback; ?></p>

I have saved this as contact_us.php as suggested.

I have changed the link from the home page from contact_us.htm to contact_us.php so it opens the correct page, but it's blank.

When I view source code it is empty?

On the Go Daddy File Manager the file is 6.18KB, so there should at least be code there?


If I use contact_us.htm, when I submit I get:
File not found 404 etc and then in the address bar: <?php%20echo%20htmlspecialchars($_SERVER[%27PHP_SELF%27]);?>
 
Last edited:
Soldato
Joined
15 Jan 2004
Posts
10,185
The script works, so it's an issue with the hosting setup again.

Do any PHP files work? The contact page you've already got set up posts from a .htm file to a .php, does that work?
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
The .php single file approach was just blank. In the file manager of Go Daddy it shoes as .php type.

The .htm with separate .php file loaded the html fine, but clicking submit to do the .php side would just create 404 error then that PHP_SELF error. I followed another tutorial for this and it was sending emails when I tested it in XAMPP.

I guess it's Go Daddy chat tomorrow.

What is considered best practice... Save the contact form as a .php with combined code or a .htm file with link to .php?

Again, thanks for responses.
 
Soldato
Joined
15 Jan 2004
Posts
10,185
In your case, I would have both pages in the same .php since they will be sent to the PHP page anyway.

To get it working, you'll probably need that .htaccess file again.

Edit the file so it contains:

Code:
AddType application/x-httpd-php .php
 
Soldato
Joined
15 Jan 2004
Posts
10,185
The html files will be fine as long as you don't add .html .htm on the end of the line like they were previously.

I had to tell Fasthosts to enable PHP when I was with them, dunno if this may be the case with you.

I'm not a sever admin, but it doesn't seem a bizarre way to run a shared web server, whereby PHP has to be enabled by a .htaccess file or by some other means instead of having it automatically associated with the .php extension.
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
For some reason contact_us.php just doesn't load up.

I have uploaded the .htacess file:

PHP:
AddType application/x-httpd-php .php

Even if I comment out the php, it still doesn't load.
 
Last edited:
Associate
Joined
28 May 2008
Posts
346
Try creating a new page called info.php (or whatever)

Put the following code in it

Code:
<?php 

phpinfo();

?>

If you access and get a page displaying PHP version, configs etc then PHP is running fine on that server. If you should get a blank page then view the source and see if you can see that code as plain HTML - in that case PHP is not running.

Also no reason not to use .php on the file in this instance as you can use a mod rewrite to remove file extensions and completely change the names of the pages displayed in the URL.
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
php must be running fine as that test brought back a load of data about php version 5.4.43.

I also did a "Hello World!" test and it worked.

I Googled the blank screen thing and it suggested some php code may be wrong, but it isn't jumping out at me:

PHP:
<?php
$to = '[email protected]';
$subject = 'This came from XYZ';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

$message = <<<EMAIL

Hi! My name is $name.

$comment

From $name
My email is $email

EMAIL;

$header = '$email';

if($_POST)
{
mail($to, $subject, $message, $header);
$feedback = 'Thanks for the email';
}
?>

<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<head>

<title>Gary Marcks Plastering - Norfolk and Suffolk Plasterer</title>

<meta name="keywords" content="Modern Plastering, Traditional Plastering, Norfolk Plasterer, Suffolk Plasterer, Plasterer Norwich, Plastering Norwich, Plastering Suffolk" />
<meta name="description" content="do you need a plasterer in Norfolk or Suffolk? Call me today. Modern Plastering, Traditional Plastering, Norfolk Plasterer, Suffolk Plasterer, Plasterer Norwich, Plastering Norwich, Plastering Suffolk" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Gary Marcks Plastering">

<link href="garystyle.css" rel="stylesheet" type="text/css" />

<!--for the red text on the form-->
<style> 
.error {
	color:#FF0000;
}
</style>

<!--code for the size of the map canvas-->
<style> 
#map_canvas {
     	width:343px;
        height:343px;
}
</style>

<!--for the Google map-->
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() 
{
  	var myLatlng = new google.maps.LatLng(52.369398, 1.124503);
  	var mapOptions = 
	{
    		zoom: 15,
    		center: myLatlng
  	}
  	var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

  	var marker = new google.maps.Marker
	({
      		position: myLatlng,
      		map: map,
      		title: 'Gary Marcks Plastering'
  	});
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

<!--for the moving images-->
<script type="text/javascript"> 

var picPaths = ['1. all_pages_images/rotating_images/1.jpg','1. all_pages_images/rotating_images/metal_lath_over_brickwork.jpg',
'1. all_pages_images/rotating_images/resized.jpg','1. all_pages_images/rotating_images/internal_render_to_chimney_breast.jpg'];
var curPic = -1;
//preload the images for smooth animation
var imgO = new Array();
for(i = 0; i < picPaths.length; i++)
{
	imgO[i] = new Image();
	imgO[i].src = picPaths[i];
}
function swapImage()
{
	curPic = (++curPic > picPaths.length -1)? 0 : curPic;
	imgCont.src = imgO[curPic].src;
	setTimeout(swapImage, 5000);
}
window.onload=function()
{
	imgCont = document.getElementById('imgBanner');
	swapImage();
}
</script>

</head>

<body>

	<div id="header">
	</div>

	<div id="logo"><img src="1. all_pages_images/gm_plastering.jpg" alt="welcome" width="260px" height="100px"
	style="margin-left:150px">
	<div id="nav">
		<nav>
    		<ul>
        		<li><a href="home.htm">Home</a></li>
        		<li><a href="recent_work.htm">Recent Work</a>
        		<!-- First Tier Drop Down -->
					<ul>
						<li><a href="dedham.htm">Dedham, Suffolk</a></li>
						<li><a href="diss.htm">Diss, Norfolk - Apr 2015</a></li>
						<li><a href="eye.htm">Eye, Suffolk</a></li>
            			<li><a href="hinderclay.htm">Hinderclay, Suffolk</a></li>
						<li><a href="hinderclay2.htm">Hinderclay, Suffolk - Apr 2015</a></li>
						<li><a href="rickinghall.htm">Rickinghall, Suffolk</a></li>
						<li><a href="walsham_le_willows.htm">Walsham le Willows, Suffolk</a>
        			</ul>
        		</li>
        		<li><a href="testimonials.htm">Testimonials</a></li>
        		<li><a class="active" href="contact_us.htm">Contact Us</a></li>
    		</ul>
		</nav>
	</div>
	</div>

<div class="wrappercontactus">

	<div id="d1">
		<div id="d2"><img id="imgBanner" src="" alt="" width=50% height=99%"
		style="margin-left:25%;border:2px solid white"/>
		</div>
	</div>

	<div id="d3">
		<img src="product_used_images/catnic.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:5px">
		<img src="product_used_images/cscs.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
		<img src="product_used_images/k_rend_logo.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
		<img src="product_used_images/knauf.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
		<img src="product_used_images/lafarge.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
		<img src="product_used_images/marshalltown.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
		<img src="product_used_images/travis_perkins.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
		<img src="product_used_images/weber.jpg" alt="welcome" width="108px" height="90px"
		style="margin-left:20px">
	</div>

	<div id="d5">To contact me please fill in the form below. Thank you. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                
		<div id="d5a">

			<p><span class="error">* required field.</span></p>
	
			<form action="?" method="post"> 
	
			<label for="name">Name:</label>
			<input type="text" name="name" id="name" />
			<span class="error">*</span>
			<br><br>

			<label for="email">Email:</label>
			<input type="text" name="email" id="email" />
			<span class="error">*</span>
			<br><br>

			<label for="comment">Comment</label> 
			<textarea id="comment" name="comment" rows="9" cols="42"></textarea>
			<br><br>

			<input id="submit" input type="submit" name="submit" value="Submit">
			<br><br>
			</form>
			
			<p id="feedback"><?php echo $feedback; ?></p>
	
		</div>
		
			<div id="d5b">
				<div id="map_canvas">
				</div>
			</div>
	
	</div>

	<div id="d6">Contact Details
		<p style="color:#000000">Address:
		<br>6
		<br>Albert Terrace
		<br>Diss
		<br>Norfolk
		<br>IP22 4EW
		<br>Tel: 01379 308321
		<br>Mob: 07518 349787
		<br><a href="mailto:[email protected]?Subject=Hello%20again" target="_top">
		[email protected]</a></p>
	</div>

	</div>

	<footer class="footer">&copy; Gary Marcks Plastering, 2014 | Designed by ABC
	</footer>

</body>

</html>

So I have only got the php at the top before the html and a little bit in the form?

Any missing ; or use of wrong ' or "? :p

Yes, I know the coding and naming conventions aren't totes amazeballs. :cool:

Testing in XAMPP I get:

Notice: Undefined index: name in C:\xampp\htdocs\contact_us.php on line 4

Notice: Undefined index: email in C:\xampp\htdocs\contact_us.php on line 5

Notice: Undefined index: comment in C:\xampp\htdocs\contact_us.php on line 6

Notice: Undefined variable: feedback in C:\xampp\htdocs\contact_us.php on line 191

So the name, email, comment and feedback variables are broken.

And then when submitting form data:

Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\contact_us.php on line 23


I guess it's because it's trying to populate the variables when the page loads and they don't exist. I guess some kind of code is needed to only run the php when the form is submitted?
 
Last edited:
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
Fixed the name, email and comment variables by doing:

PHP:
if(isset($_POST['name'])){ $name = $_POST['name']; } 
if(isset($_POST['email'])){ $email = $_POST['email']; } 
if(isset($_POST['comment'])){ $comment = $_POST['comment']; }

Just get an error with:

PHP:
<p id="feedback"><?php echo $feedback; ?></p>

Then when I submit it I get:

Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\contact_us.php on line 14

Which is:

PHP:
mail($to, $subject, $message, $header);
 
Soldato
Joined
15 Jan 2004
Posts
10,185
It's because your header is just this:

PHP:
$header = '$email';

It should be something like this:

PHP:
$header ='From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";

Your $feedback is probably giving an error because before the page is posted, you're calling it, but it's not defined.

Use if(isset(... like you've done with the $_POST variables.

By the way, with form elements you can make them required by adding "required" just prior to closing the tag...

Code:
<input type="text" name="email" id="email" required>

This may not work with HTML4 though. Also a user could override it if they really want to, but it's a good thing to add.
 
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
Losing the will to live here.

I have changed it to an example here:
http://stackoverflow.com/questions/...from-html-form-on-submit-with-the-same-script

I tried it in XAMPP and it had no errors, was sending 2 emails and confirming mail sent etc, but then I try it for real and I get the white screen of death again?

Code is:
PHP:
<?php 
if(isset($_POST['submit'])){
    $to = "[email protected]"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<head>


</head>

<body>

	<div id="header">
	</div>

	<div id="logo"><img src="1. all_pages_images/gm_plastering.jpg" alt="welcome" width="260px" height="100px"
	style="margin-left:150px">
	<div id="nav">
		<nav>
    		<ul>
        		<li><a href="home.htm">Home</a></li>
        		<li><a href="recent_work.htm">Recent Work</a>
        		<!-- First Tier Drop Down -->
					<ul>
						<li><a href="dedham.htm">Dedham, Suffolk</a></li>
						<li><a href="diss.htm">Diss, Norfolk - Apr 2015</a></li>
						<li><a href="eye.htm">Eye, Suffolk</a></li>
            			<li><a href="hinderclay.htm">Hinderclay, Suffolk</a></li>
						<li><a href="hinderclay2.htm">Hinderclay, Suffolk - Apr 2015</a></li>
						<li><a href="rickinghall.htm">Rickinghall, Suffolk</a></li>
						<li><a href="walsham_le_willows.htm">Walsham le Willows, Suffolk</a>
        			</ul>
        		</li>
        		<li><a href="testimonials.htm">Testimonials</a></li>
        		<li><a class="active" href="contact_us.htm">Contact Us</a></li>
    		</ul>
		</nav>
	</div>
	</div>

<div class="wrappercontactus">

	<div id="d1">
		<div id="d2"><img id="imgBanner" src="" alt="" width=50% height=99%"
		style="margin-left:25%;border:2px solid white"/>
		</div>
	</div>

	<div id="d3">
		
	</div>

	<div id="d5">To contact me please fill in the form below. Thank you. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                
		<div id="d5a">

			<br>
			<form action="" method="post">
				First Name: <input type="text" name="first_name"><br><br>
				Last Name: <input type="text" name="last_name"><br><br>
				Email: <input type="text" name="email"><br><br>
				Message:<br><textarea rows="5" name="message" cols="30"></textarea><br><br>
				<input type="submit" name="submit" value="Submit">
			</form>
	
		</div>
		
			<div id="d5b">
				<div id="map_canvas">
				</div>
			</div>
	
	</div>

	<div id="d6">Contact Details
		
	</div>

	</div>

	<footer class="footer">
	</footer>

</body>

</html>

Removed some html to make it clearer. Any ideas?

Maybe I should follow the second example and have a separate .php file?
 
Last edited:
Associate
Joined
12 Feb 2003
Posts
897
Looks like a hosting issue.

Just ensure that you have PHP enabled. It may be worth checking to see if you can enable error reporting with your web host. This should help to see where exactly the issue (if any) lies.

Edit: Just seen PHP is working properly as phpinfo() worked without a problem.

Jim
 
Last edited:
Soldato
OP
Joined
18 Aug 2011
Posts
2,853
Location
Norfolk
I have now managed to get this to work (and wasted plenty of time calling Go Daddy getting no where).

- I created a new .php file name contact.php
- copied over the code from contact_us.php to contact.php one step at a time and tested it on Go Daddy's servers to make sure it worked.

The only differences I can see is:
I removed
- <?xml version ="1.0" encoding="UTF-8"?>
- <!DOCTYPE HTML PUBLIC = "//W3C//DTD HTML 4.0 Transitional//EN">

And just used
- <!DOCYPE HTML>

I also put the php code within body instead of at the top of the document but I doubt this makes any difference.
 
Associate
Joined
21 May 2013
Posts
1,991
Likely mail() is not set up on your host, or requires a configured email account to prevent spam.

Your hosting support should be able to help you with it.
 
Associate
Joined
7 May 2004
Posts
353
The hosting is probably php version 5.3 with short tags turned on, which explains why it worked on xampp, didn't work on the server but did when you removed the xml tag. PHP thinks the <?xml is actually trying to be PHP.
 
Back
Top Bottom