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
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
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:
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
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:
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.
 
Back
Top Bottom