PDA

View Full Version : HTML to PHP coding issue


stryker9603
08-19-09, 10:39 PM
Good Evening everyone. Newbee here so please be gentle. Here is what I am trying to do

I have a contact.html page that asks users for name, e-mail and a message. once I click on submit I would like the page to call my contact.php page and send an e-mail to me with said information. I have listed the code below but I cant get it to work. any help would be much appreciated.


contact.html code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>My title</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link href="flow.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
color: #28042d;
}
#apDiv1 {
position:absolute;
width:188px;
height:113px;
z-index:1;
left: 38px;
top: 48px;
}
#apDiv2 {
position:absolute;
width:200px;
height:113px;
z-index:2;
left: 880px;
top: 48px;
}
#apDiv3 {
position:absolute;
width:200px;
height:115px;
z-index:3;
left: 414px;
top: 48px;
}
#apDiv4 {
position:absolute;
width:200px;
height:46px;
z-index:4;
left: 450px;
top: 0px;
}
#apDiv5 {
position:absolute;
width:200px;
height:40px;
z-index:5;
left: 450px;
top: 489px;
}
#apDiv6 {
position:absolute;
width:618px;
height:217px;
z-index:6;
left: 237px;
top: 284px;
}
-->
</style></head>
<body class="sub">
<div id="apDiv4"> <td class="NavigationColor"><h4 align="center"><a href="index.html">Home</a>&nbsp;</a>&nbsp;<a href="contact.html">Contact</a>&nbsp;<a href="about.html"> About</a>&nbsp;<a href="privacy.html">Privacy</a></h4></td></div>
<div id="apDiv6">
<form>
<form name="contact_form" action="contact.php" method="get">
<center>
<table width="567" height="175" border="0" cellpadding="0" cellspacing="0" bgcolor="#c1c2d9">
<tr> </tr>
<tr>
<td height="28" valign="top"><table width="456" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="124" align="right" class="text_1">Your name:</td>
<td width="26"><img src="images/spacer.gif" width="1" height="1" /></td>
<td><input name="your_name" type="text" class="form_1" /></td>
</tr>
</table></td>
</tr>
<tr>
<td height="28" valign="top"><table width="456" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="124" height="33" align="right" class="text_1">E-mail address:</td>
<td width="26"><img src="images/spacer.gif" width="1" height="1" /></td>
<td><input name="your_email" type="text" class="form_1" /></td>
</tr>
</table></td>
</tr>
<tr>
<td height="91" valign="top"><table width="469" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="124" align="right" valign="top" class="text_1" style="padding-top:3px ">Message:</td>
<td width="26"><img src="images/spacer.gif" width="1" height="1" /></td>
<td><textarea name="message" class="form_2"></textarea></td>
</tr>
</table></td>
</tr>
<tr>
<td valign="top"><input type="image" src="images/submit.gif" style="margin-left:29px;" onClick="submit();">
<input type="image" src="images/reset.gif" style="margin-left:338px " /></td>
</tr>
</table>
<!-- -->
</center>
</form>
<table width="90%" border="0" cellspacing="0" cellpadding="4" style="text-align: center">
<tr style="vertical-align: top">
</tr>
</table></div>
<table width="53%" border="0" align="center" cellspacing="0" cellpadding="0" style="text-align: center">
<tr style="vertical-align: top">
<td width="20%" rowspan="2" class="StoryContentColor" style="vertical-align: top">
<div id="apDiv1"><img src="images/bach.gif" width="137" height="150" alt="bachelorette" /></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><br />
</p>
<tr style="vertical-align: top">
<td width="20%" rowspan="2" class="StoryContentColor" style="vertical-align: top">
<div id="apDiv3"><img src="images/money.gif" width="275" height="225" alt="make money" /></div>
<p><br />&nbsp;&nbsp;&nbsp;
</p>
</td>
<td width="20%"><div id="apDiv2"><img src="images/champ.gif" width="124" height="150" alt="champagne" /></div> <br /> </td>
</table>

<div id="apDiv5"><td class="NavigationColor"><h4 align="center"><a href="index.html">Home</a>&nbsp;</a>&nbsp;<a href="contact.html">Contact</a>&nbsp;<a href="about.html"> About</a>&nbsp;<a href="privacy.html">Privacy</a></h4></td></div>
</table> </td>
</tr>
</body>
</html>




















Contact.php code

<?
$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";
$headers.='Content-type: text/html; charset=iso-8859-1';
mail("sales@mydomain.com", $subject, "
<html>
<head>
<title>Contact letter</title>
</head>
<body>

<br>
".$_GET['message']."
</body>
</html>" , $headers);
echo ("Your message was successfully sent!");
?>
<script>
resizeTo(300, 300)
//window.close()
</script>

Joe
08-20-09, 12:35 AM
If you dont understand the code enough to solve the issue this site has a very easy to use 'wizard' that will create the form for you.

http://mycontactform.com/

Enjoy!

Mad_Haggis
08-20-09, 01:13 AM
"$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";"

Apple?

I am very allegic to this tuff, amd has carried the reaction.

Wants and understanding 2 T code will start[in your case] with a "number sign followed backwards by a periode?

TonyT
08-20-09, 08:14 AM
Don't use GET for an email form, use POST.
<form name="contact_form" action="contact.php" method="post">
<input name="your_name" type="text" class="form_1" />
<input name="your_email" type="text" class="form_1" />
<textarea name="message" class="form_2"></textarea>

Change inputs for better control & need a value:
<form name="contact_form" action="contact.php" method="post">
<input name="your_name" type="text" class="form_1" value="" size="30" maxlength="48" />
<input name="your_email" type="text" class="form_1" value="" size="30" maxlength="48" />
<textarea name="message" class="form_2" rows="6" cols="48"></textarea>

You should use error correction because if the user enters an invalid email address the mail won't get sent. Search google for javacript form validation.

Your PHP is incorrect, you MUST close PHP tags when shifting to HTML. And the message won't get sent because your PHP code doesn't have code for the message! And what if someone accesses the php page directly? You'd get blank messages. And you should include code that prevents header injection by spammers using your script as a mail relay. And the subject is not defined in the form or in your PHP. And don't use an image for the submit, it doesn't work well in all browsers, just use a submit button.
<?
$subject="from ".$_GET['your_name'];
$headers= "From: ".$_GET['your_email']."\n";
$headers.='Content-type: text/html; charset=iso-8859-1';
mail("sales@mydomain.com", $subject, "
<html>
<head>
<title>Contact letter</title>
</head>
<body>

<br>
".$_GET['message']."
</body>
</html>" , $headers);
echo ("Your message was successfully sent!");
?>
<script>
resizeTo(300, 300)
//window.close()
</script>

Change to:
<?php
// ensure this script is never cached
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');

if ($_POST['submit'] != TRUE) { //if submit not pressed bounce to home page
echo "<meta http-equiv='refresh' content='0;url=./'>";
exit;
}

elseif ($_POST['submit'] == TRUE) { //if submit button pressed process form
$recipient = "sales@mydomain.com";
$subject = "Type The Subject Here";
$your_name = stripslashes(strip_tags($_POST['your_name']));
$your_email = stripslashes(strip_tags($_POST['your_email']));
$message = stripslashes(strip_tags($_POST['message']));
$ip = $_SERVER['REMOTE_ADDR']; // grab sender's ip address
$msgformat = "Sender IP Address: $ip\nFrom: $sender_name\nEmail: $sender_email\nPhone: $sender_phone\n\n$email_message"; // format message for sending

//prevent header injection, bounce to google if fields contain carriage returns
if (ereg("[\r\n]", $your_name) || ereg("[\r\n]", $your_email)) {
echo "<meta http-equiv='refresh' content='0;url=http://www.google.com'>";
exit;
}

// send the message
mail($recipient, $subject, $msgformat, "From: $your_name <$your_email>");

// close PHP and swithch to HTML, display the message to the viewer
?>

<html>
<head>
<title>Contact letter</title>
</head>
<body>
<h3>Your message was successfully sent!</h3>
<-- display the message to the viewer -->
<p><pre><?php echo $msgformat; ?></pre></p>
</body>
</html>

Don't use window.close, it won't work with modern browser's security settings.