/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Send form details to specific recipient dependant on list selection

Hello everyone,

This is my first post to this forum (and my first attempt at PHP), so I am hoping someone can assist me with a problem I cannot get my head around.

I am a retired old git with no real IT training, but I have created a website for our small village, which includes an online form for people to contact us.

Currently the form works fine(ish) and sends the relevant details filled out on the form to me.

Within the form is a “Please select” list box with various village functions. i.e. Village Hall, Parish Council, Marquee Hire & etc. I am trying to get it to send the form information to different people in the village, depending on which village function is selected. This is where I am coming unstuck, and I simply do not understand what or why, so I hope someone can show me the error of my ways (other than don’t mess with what you don’t understand ?)

My php code is:

[code=php]
<?php

/* Email Variables */
$emailSubject = ‘website_enquiry’;
$recipients = array(
‘recipient_1’ => ‘[email protected]’,
‘recipient_2’ => ‘[email protected]’,
‘recipient_3’ => ‘[email protected]’,
‘recipient_4’ => ‘[email protected]’,
‘recipient_5’ => ‘[email protected]’,
‘recipient_6’ => ‘[email protected]’,
‘recipient_7’ => ‘[email protected]
);
$my_email = $recipients[$_REQUEST[‘Nature’]];

/* Data Variables */
$Name = $_POST[‘Name’];
$Telephone = $_POST[‘Telephone’];
$Email = $_POST[‘Email’];
$Nature = $_POST[‘Nature’];
$Questions = $_POST[‘Questions’];

$body = <<<EOD
<br><hr><br>
Name: $Name <br>
Telephone: $Telephone <br>
Email: $Email <br>
recipient: $recipient <br>
Questions: $Questions <br>
EOD;
$headers = “From: $Emailrn”;
$headers .= “Content-type: text/htmlrn”;
$success = mail($my_email, $emailSubject, $body,
$headers);

/* Results */
$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv=”refresh” content=”3;URL=http://example.com/index.html”>
<style type=”text/css”>
<!–
body {
background-color: #444;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #fec001;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}
–>
</style>
</head>
<div align=”center”>Thank you for your enquiry. Your message has been sent and we will respond as soon as possible</div>
</div>
</body>
</html>
EOD;
echo “$theResults”;
?>
[/code]

and the relevant part of the html within the contact form is:

[code=html]
<td width=”50%” height=”56″><div align=”center”>Nature of your Enquiry ?</div></td>
<td width=”50%”><div align=”center”>
<select name=”Nature” size=”1″ id=”Nature”>
<option>Please Select</option>
<option value=”recipient_1″>General Enquiries</option>
<option value=”recipient_2″>Hall Hire</option>
<option value=”recipient_3″>Marquee Hire</option>
<option value=”recipient_4″>Parish Church</option>
<option value=”recipient_5″>Parish Council</option>
<option value=”recipient_6″>Recreation Area</option>
<option value=”recipient_7″>Other</option>
</select>
</div></td>
[/code]

I am pretty sure I have done something simple but stupid, so any guidance to help sort this out would be much appreciated.

to post a comment
PHP

26 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJun 24.2014 — You told us your aim, but you didn't tell us what was not happening. I'm guessing that the mail is not being sent, correct? Why don't you put in a test of your $success var since you have stored the result of the call to mail() in it? Also, echo out your values that are being used in the call to mail so you can confirm that much is correct. And most importantly, turn on php error checking until this whole thing is working 100%.

Put the following at the top of your script.
[code=php]
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
[/code]


Also - why are you commenting out your css code?

There's a few other things wrong here but let's get the thing working before you do the necessary cleanup, like validating your inputs and making sure you have the proper ones.
Copy linkTweet thisAlerts:
@kimmcmsauthorJun 24.2014 — Thanks ginerjm, my apologies. The problem is that when I complete the form in this iteration, I get an http 500 error "website cannot display the page.

In its existing, working, iteration, I complete the form & click submit. It sends me the email of whatever I have put in the form including the selected village function, the "Thank you for your enquiry etc" message pops up for 3 seconds and then it returns me to the website home page.

The only difference between the 2 iterations is that in the original I have
[code=php] $emailto = '[email protected]';[/code]
and in the second one I have changed that to
[code=php]
$recipients = array(
'recipient_1' => '[email protected]',

'recipient_2' => '[email protected]',

'recipient_3' => '[email protected]',
'recipient_4' => '[email protected]',
'recipient_5' => '[email protected]',
'recipient_6' => '[email protected]',
'recipient_7' => '[email protected]'
);
$my_email = $recipients[$_REQUEST['Nature']];
[/code]


As I amended the "mailto" from $emailto to $my_email in the 2 iterations, I have also changed that, but otherwise it is exactly the same.

I really appreciate your recommendations and will work on them and come back to this as soon as possible, but in the meanwhile, could I ask you to clarify what you mean by commenting out my CSS code ? I'm afraid I don't see it as it seems to work fine in its current basic iteration. Anyhow, many thanks for your guidance so far.

Rgds Kim.
Copy linkTweet thisAlerts:
@ginerjmJun 24.2014 — You have an html comment tag in your style tag.

Perhaps you could show us the entire form code?

What is causing these 2 iterations you refer to? I see no looping going on, so I don't understand what you mean by iterations. Clarify please?

I don't see anything here that would cause your 'thank you...' message to be overwritten by some other output after 3 seconds. Perhaps you can show us that too?
Copy linkTweet thisAlerts:
@kimmcmsauthorJun 24.2014 — The html comment at the beginning of the style tag is to bring up the 'thank you' etc as a full page html message which then closes again with a second html comment at the end.

The 'thank you ' message refreshes back to the home page by code:
[code=php] <meta http-equiv="refresh" content="3;URL=http://example.com/index.html"> [/code] which is the fourth row within "the results" section of my code above.

What I mean by iteration is that I have already created an online form and a php script which works, but was only designed to send the form content to me. I am now trying to get it to send to specific recipients which is where I am having my problems.

This is my original working full form code:
[code=html]
<html>
<head>
<title>Welcome to Example - Contact Us</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.n'; }
} if (errors) alert('The following error(s) occurred:n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF" background="goldmold.jpg">
<div id="Layer1" style="position:absolute; width:207px; height:42px; z-index:3; left: 10px; top: 19px;"><font size="6" face="Calligraph421 BT"><strong>Welcome
to <font size="7">example</font></strong></font></div>
<h1>&nbsp;</h1>
<p align="center"><font size="+4"><strong>Contact Us</strong></font></p>
<hr>
<div align="right"><font size="-1"><strong><a href="index.html" style="text-decoration = None"><font color="#000000" face="Calligraph421 BT">Return
to Home Page</font></a></strong></font></div>
<blockquote>
<blockquote>
<blockquote>
<p align="center"><font size="+1" face="Calligraph421 BT"><strong>For any
enquiries relating to the Village Hall, please either telephone the Bookings
Secretary between 08:00 and 20:00 Monday to Friday on 0845-643-0000, or
<a href="mailto:[email protected]"><font color="#0000FF">Email</font></a>
her with any questions.</strong></font></p>
</blockquote>
</blockquote>
</blockquote>
<p align="center"><strong><font size="+1" face="Calligraph421 BT">Alternatively,
or for other village matters, please complete and submit the short form below,
and we will respond as soon as possible</font></strong></p>
<form action="website_enquiry.php" method="post" enctype="multipart/form-data" name="Contact" id="Contact">
<table width="50%" border="1" align="center">
<tr>
<td width="50%" height="64"><div align="center">Full Name:</div></td>
<td width="50%"><div align="center">
<input name="Name" type="text" id="Name" size="48%">
</div></td>
</tr>
<tr>
<td width="50%" height="64"><div align="center">Telephone No.</div></td>
<td width="50%"><div align="center">
<input name="Telephone" type="text" id="Telephone" size="48%">
</div></td>
</tr>
<tr>
<td width="50%" height="67"><div align="center">Email:</div></td>
<td width="50%"><div align="center">
<input name="Email" type="text" id="Email" size="48%">
</div></td>
</tr>
<tr>
<td width="50%" height="56"><div align="center">Nature of your Enquiry ?</div></td>
<td width="50%"><div align="center">
<select name="Nature" size="1" id="Nature">
<option selected>Please Select</option>
<option>Hall Hire</option>
<option>Marquee Hire</option>
<option>Recreation Area</option>
<option>Social Club</option>
<option>Parish Church</option>
<option>Other</option>
</select>
</div></td>
</tr>
<tr>
<td width="50%" height="127"><div align="center">Enquiry Details:</div></td>
<td width="50%"><div align="center">
<textarea name="Questions" cols="40%" rows="4" id="Questions"></textarea>
</div></td>
</tr>
<tr>
<td width="50%"><div align="center">
<input name="contactSubmit" type="submit" id="contactSubmit" onClick="MM_validateForm('Name','','R','Telephone','','RisNum','Email','','NisEmail','Questions','','R');return document.MM_returnValue" value="Submit">
</div></td>
<td width="50%"><div align="center">
<input name="ContactReset" type="reset" id="ContactReset" value="Reset">
</div></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp; </p>
</form>
<p align="center">&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
[/code]

and here is the original full php:
[code=php]
<?php
/* Contact Enquiry Form PHP */

/* Email Variables */
$emailSubject = 'website_enquiry';
$emailto = '[email protected]';


/* Data Variables */
$Name = $_POST['Name'];
$Telephone = $_POST['Telephone'];
$Email = $_POST['Email'];
$Nature = $_POST['Nature'];
$Questions = $_POST['Questions'];


$body = <<<EOD
<br><hr><br>
Name: $Name <br>
Telephone: $Telephone <br>
Email: $Email <br>
Nature: $Nature <br>
Questions: $Questions <br>
EOD;
$headers = "From: $Emailrn";
$headers .= "Content-type: text/htmlrn";
$success = mail($emailto, $emailSubject, $body,
$headers);

/* Results*/
$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv="refresh" content="3;URL=http://Donyatt.com/index.html">
<style type="text/css">
<!--
body {
background-color: #444;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #fec001;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}
-->
</style>
</head>
<div align="center">Thank you for your enquiry. Your message has been sent and we will respond as soon as possible</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>
[/code]


So, having got this working, I stupidly started tinkering to try the selected recipient bit.

Putting the amended code here makes the post too long, so I will send immediately after this post. Kim
Copy linkTweet thisAlerts:
@kimmcmsauthorJun 24.2014 — Part 2 of the reply....

This is my amended contact form code:
[code=html]
<html>
<head>
<title>Welcome to Example - Contact Us</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.n'; }
} if (errors) alert('The following error(s) occurred:n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF" background="goldmold.jpg">
<div id="Layer1" style="position:absolute; width:207px; height:42px; z-index:3; left: 10px; top: 19px;"><font size="6" face="Calligraph421 BT"><strong>Welcome
to <font size="7">Example</font></strong></font></div>
<h1>&nbsp;</h1>
<p align="center"><font size="+4"><strong>Contact Us</strong></font></p>
<hr>
<div align="right"><font size="-1"><strong><a href="index.html" style="text-decoration = None"><font color="#000000" face="Calligraph421 BT">Return
to Home Page</font></a></strong></font></div>
<blockquote>
<blockquote>
<blockquote>
<p align="center"><font size="+1" face="Calligraph421 BT"><strong>For any
enquiries relating to the Village Hall, please either telephone the Bookings
Secretary between 08:00 and 20:00 Monday to Friday on 0845-643-0000, or
<a href="mailto:[email protected]"><img src="emailbutton.jpg" width="70" height="20" border="0"></a>
her with any questions.</strong></font></p>
</blockquote>
</blockquote>
</blockquote>
<p align="center"><strong><font size="+1" face="Calligraph421 BT">Alternatively,
or for other village matters, please complete and submit the short form below,
and we will respond as soon as possible</font></strong></p>
<form action="website_enquiry_process.php" method="post" enctype="multipart/form-data" name="Contactform" id="Contactform">
<table width="50%" border="1" align="center">
<tr>
<td width="50%" height="64"><div align="center">Full Name:</div></td>
<td width="50%"><div align="center">
<input name="Name" type="text" id="Name" size="48%">
</div></td>
</tr>
<tr>
<td width="50%" height="64"><div align="center">Telephone No.</div></td>
<td width="50%"><div align="center">
<input name="Telephone" type="text" id="Telephone" size="48%">
</div></td>
</tr>
<tr>
<td width="50%" height="67"><div align="center">Email:</div></td>
<td width="50%"><div align="center">
<input name="Email" type="text" id="Email" size="48%">
</div></td>
</tr>
<tr>
<td width="50%" height="56"><div align="center">Nature of your Enquiry ?</div></td>
<td width="50%"><div align="center">
<select name="Nature" size="1" id="Nature">
<option>Please Select</option>
<option value="recipient_1">General Enquiries</option>

<option value="recipient_2">Hall Hire</option>
<option value="recipient_3">Marquee Hire</option>
<option value="recipient_4">Parish Church</option>
<option value="recipient_5">Parish Council</option>
<option value="recipient_6">Recreation Area</option>
<option value="recipient_7">Other</option>
</select>
</div></td>
</tr>
<tr>
<td width="50%" height="127"><div align="center">Enquiry Details:</div></td>
<td width="50%"><div align="center">
<textarea name="Questions" cols="40%" rows="4" id="Questions"></textarea>
</div></td>
</tr>
<tr>
<td width="50%"><div align="center">
<input name="contactSubmit" type="submit" id="contactSubmit" onClick="MM_validateForm('Name','','R','Telephone','','RisNum','Email','','NisEmail','Questions','','R');return document.MM_returnValue" value="Submit">
</div></td>
<td width="50%"><div align="center">
<input name="ContactReset" type="reset" id="ContactReset" value="Reset">
</div></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp; </p>
</form>
<p align="center">&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
[/code]




As you can see, there is very little change between the two html forms, just the bit referring to recipients.

The amended php code is the code in my first post above.

Having looked at it again, could it be something to do with the
[code=php] $my_email = $recipients[$_REQUEST['Nature']];[/code]
not finding where it should be looking ?

Regards

Kim
Copy linkTweet thisAlerts:
@ginerjmJun 24.2014 — 1 - The use of a comment tag in the style section for whatever purpose you are describing is something I have never heard of. I always thought that anything inside a pair of comment tags was just that - a comment not to be interpreted by the browser, and hence, not 'code'. Therefore you have no css code in this page.

2 - echo out the pieces of your mail call as I suggested earlier and confirm that they are what you expect.

3 - is error checking turned on for php?

4 - I notice that you have two different layouts for your Nature select tag. One has value attributes, one does not. What's that?
Copy linkTweet thisAlerts:
@kimmcmsauthorJun 24.2014 — 
  • 1. Being seriously brand new to this type of thing, I'm afraid I'm not sure how to reply. I cannibalised some code I found on the web to make it fit & do what I needed. When I read through it and tested it, I assumed (probably wrongly) that the html tags were to make the 'thank you' screen a full html page, given that it is a full page message on the original, working version. However, I am more than happy to concede that I don't really know what I am talking about.


  • 2. will do - as soon as I work out how.


  • 3. No. Not yet. again, I need to work out how.


  • 4. I changed the layout to include value attributes i.e 'recipient 1' so that I did not have the email addresses in the html code, but rather have them in the php, as I read that it was bad practice to have email addresses in ones html because it is attractive to spammers.


  • I really do appreciate your help ginerjm, and will pick up on this again tomorrow evening as I am out all day.

    Regards
    Copy linkTweet thisAlerts:
    @ginerjmJun 24.2014 — 1 - the <!-- and the following --> are html comment tags wrapping (in this case) your entire css code for the body of the html page. Remove them and that css will then be functioning. Or just delete the whole style section if you are happy with the current presentation.

    2 - an echo statement simply displays whatever you provide. In this case where you were calling:

    $success = mail($my_email, $emailSubject, $body, $headers);

    I want you to add the following just prior to your call to mail() do this:

    <i>
    </i>echo "my email is $my_email&lt;br&gt;";
    echo "subject is $emailSubject&lt;br&gt;";
    echo "body is $body&lt;br&gt;";
    echo "headers are $headers&lt;br&gt;";



    3 - at the beginning of your php code add these lines (in this script and in EVERY SCRIPT YOU ARE DEVELOPING):

    <i>
    </i>error_reporting(E_ALL | E_NOTICE); // Kinds of errors to show
    ini_set('display_errors', '1'); // turn on display at the client to display the messages


    4 - My point was that in one place you have absolutely NO VALUES and in the other you DO have values, not that they weren't addresses. I don't care what you use for the value=, but if you don't use anything then what will your select tag return?
    Copy linkTweet thisAlerts:
    @Kevin2Jun 24.2014 — HTML comment tags (<!-- comment/code -->) were once required inside <script> tags to prevent old browsers from displaying the code on the page. That hasn't been the case in 15 years though. HTML comments were never part of <style> coding. They aren't necessary and are just taking up room in your code. The comment tags for CSS are [B]/* comment here */[/B], so your comment tags aren't working no matter what. Junk -- get rid of them.

    font-style: normal;
    line-height: normal;
    font-weight: normal;


    You never have to specify "normal". Normal is ... well ... normal -- the default behavior. You should only have to specify something if it is [I]not[/I] normal, e.g. [B]font-weight: bold;[/B]. Again, this is code that does nothing for you, get rid of it.

    In addition, [B]align="left|center|right"[/B] has been deprecated for many years now. Time to update that as well. You already have part of what you need in your CSS -- a specified width.

    All that being stated, here is an updated, cleaned up, and corrected (no <body> tag in your code) version of the HTML "Results" part of your code:
    [code=html]<html>
    <head>
    <title>sent message</title>
    <meta http-equiv="refresh" content="3;URL=http://example.com/index.html">
    <style type="text/css">
    body {
    background-color: #444;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 20px;
    color: #fec001;
    padding-top: 200px;
    width: 800px;
    margin: auto;
    text-align: center;
    }
    </style>
    </head>
    <body>
    <div>Thank you for your enquiry. Your message has been sent and we will respond as soon as possible.</div>
    </body>
    </html>[/code]
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJun 26.2014 — Hello Ginerjm & Kevin2.

    Thank you both for your assistance, ginerjm for explaining the echo statement and error reporting, and Kevin2 for the cleaned up 'results' code. I think I follow most if not all. Kevin2 - your identification of my html code being outdated (a bit like me) is spot on. I am using Dreamweaver MX, and it put in a lot of the tags automatically when I designed the form. Not knowing anything much about html or current conventions, I just left them there.

    I have take on board both your suggestions and have amended my code to reflect the suggested changes, but when testing, I get the same http 500 'cant display the webpage' error after I click submit, so cannot even test the errors or echos. I guess there is either something it doesn't like in the code somewhere, or I am testing it incorrectly. I have "put" the html & php code to the website but not linked to any live pages, and opened the test form page by typing in the full address in the top box.

  • 1. Is this the correct way to test ?

  • 2. Can either of you (or anyone else) see anything in either the html or php which is clearly a programming error and which would generate the page error I get, as I cannot ?


  • Regards & thanks in anticipation.

    Kim
    Copy linkTweet thisAlerts:
    @ginerjmJun 26.2014 — Can you post your revised code now?
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJun 26.2014 — Hi,

    html code is:
    [code=html]
    <html>
    <head>
    <title>Welcome to Example - Contact Us</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.01
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.n';
    } else if (test!='R') { num = parseFloat(val);
    if (isNaN(val)) errors+='- '+nm+' must contain a number.n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.n'; }
    } if (errors) alert('The following error(s) occurred:n'+errors);
    document.MM_returnValue = (errors == '');
    }
    //-->
    </script>
    </head>

    <body bgcolor="#FFFFFF" background="goldmold.jpg">
    <div id="Layer1" style="position:absolute; width:207px; height:42px; z-index:3; left: 10px; top: 19px;"><font size="6" face="Calligraph421 BT"><strong>Welcome
    to <font size="7">Example</font></strong></font></div>
    <h1>&nbsp;</h1>
    <p align="center"><font size="+4"><strong>Contact Us</strong></font></p>
    <hr>
    <div align="right"><font size="-1"><strong><a href="index.html" style="text-decoration = None"><font color="#000000" face="Calligraph421 BT">Return
    to Home Page</font></a></strong></font></div>
    <blockquote>
    <blockquote>
    <blockquote>
    <p align="center"><font size="+1" face="Calligraph421 BT"><strong>For any
    enquiries relating to the Village Hall, please either telephone the Bookings
    Secretary between 08:00 and 20:00 Monday to Friday on 0845-643-0000, or
    <a href="mailto:[email protected]"><img src="emailbutton.jpg" width="70" height="20" border="0"></a>
    her with any questions.</strong></font></p>
    </blockquote>
    </blockquote>
    </blockquote>
    <p align="center"><strong><font size="+1" face="Calligraph421 BT">Alternatively,
    or for other village matters, please complete and submit the short form below,
    and we will respond as soon as possible</font></strong></p>
    <form action="website_process.php" method="post" enctype="multipart/form-data" name="Contactform" id="Contactform">
    <table width="50%" border="1" align="center">
    <tr>
    <td width="50%" height="64"><div align="center">Full Name:</div></td>
    <td width="50%"><div align="center">
    <input name="Name" type="text" id="Name" size="48%">
    </div></td>
    </tr>
    <tr>
    <td width="50%" height="64"><div align="center">Telephone No.</div></td>
    <td width="50%"><div align="center">
    <input name="Telephone" type="text" id="Telephone" size="48%">
    </div></td>
    </tr>
    <tr>
    <td width="50%" height="67"><div align="center">Email:</div></td>
    <td width="50%"><div align="center">
    <input name="Email" type="text" id="Email" size="48%">
    </div></td>
    </tr>
    <tr>
    <td width="50%" height="56"><div align="center">Nature of your Enquiry ?</div></td>
    <td width="50%"><div align="center">
    <select name="Nature" size="1" id="Nature">
    <option>Please Select</option>
    <option value="recipient_1">General Enquiries</option>

    <option value="recipient_2">Hall Hire</option>
    <option value="recipient_3">Marquee Hire</option>
    <option value="recipient_4">Parish Church</option>
    <option value="recipient_5">Parish Council</option>
    <option value="recipient_6">Recreation Area</option>
    <option value="recipient_7">Other</option>
    </select>
    <?-- <option>Please Select</option>
    <option value =>General Enquiries</option>

    <option>Hall Hire</option>
    <option>Marquee Hire</option>
    <option>Parish Church</option>
    <option>Parish Council</option>
    <option>Recreation Area</option>
    <option>Other</option>
    </select> //-->


    </div></td>
    </tr>
    <tr>
    <td width="50%" height="127"><div align="center">Enquiry Details:</div></td>
    <td width="50%"><div align="center">
    <textarea name="Questions" cols="40%" rows="4" id="Questions"></textarea>
    </div></td>
    </tr>
    <tr>
    <td width="50%"><div align="center">
    <input name="contactSubmit" type="submit" id="contactSubmit" onClick="MM_validateForm('Name','','R','Telephone','','RisNum','Email','','NisEmail','Questions','','R');return document.MM_returnValue" value="Submit">
    </div></td>
    <td width="50%"><div align="center">
    <input name="ContactReset" type="reset" id="ContactReset" value="Reset">
    </div></td>
    </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp; </p>
    </form>
    <p align="center">&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    </body>
    </html>
    [/code]


    and php is:
    [code=php]
    <?php

    /* Error Checking */
    error_reporting(E_ALL | E_NOTICE); // Kinds of errors to show
    ini_set('display_errors', '1'); // turn on display at the client to display the messages

    /* Email Variables */
    $emailSubject = 'website_process';
    $recipients = array(

    'recipient_1' => '[email protected]',
    'recipient_2' => '[email protected]',
    'recipient_3' => '[email protected]'
    'recipient_4' => '[email protected]',
    'recipient_5' => '[email protected]',
    'recipient_6' => '[email protected]',
    'recipient_7' => '[email protected]'

    /*General Enquiries => '[email protected]',
    Hall Hire => '[email protected]',
    Marquee Hire => '[email protected]'
    Parish Church => '[email protected]',
    Parish Council => '[email protected]',
    Recreation Area => '[email protected]',
    Other => '[email protected]'*/
    );
    $my_email = $recipients[$_REQUEST['Nature']];

    /* Data Variables */
    $Name = $_POST['Name'];
    $Telephone = $_POST['Telephone'];
    $Email = $_POST['Email'];
    $Nature = $_POST['Nature'];
    $Questions = $_POST['Questions'];

    /* Email Content*/
    $body = <<<EOD
    <br><hr><br>
    Name: $Name <br>
    Telephone: $Telephone <br>
    Email: $Email <br>
    recipient: $recipient <br>
    Questions: $Questions <br>
    EOD;
    $headers = "From: $Emailrn";
    $headers .= "Content-type: text/htmlrn";
    echo "my email is $my_email<br>";
    echo "subject is $emailSubject<br>";
    echo "body is $body<br>";
    echo "headers are $headers<br>";
    $success = mail($my_email, $emailSubject, $body,
    $headers);

    /* Results HTML */
    $theResults = <<<EOD
    <html>
    <head>
    <title>sent message</title>
    <meta http-equiv="refresh" content="3;URL=http://example.com/index.html">
    <style type="text/css">
    body {
    background-color: #444;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 20px;
    color: #fec001;
    padding-top: 200px;
    width: 800px;
    margin: auto;
    text-align: center;
    }
    </style>
    </head>
    <body>
    <div>Thank you for your enquiry. Your message has been sent and we will respond as soon as possible.</div>
    </body>
    </html>
    EOD;
    echo "$theResults";
    ?>
    [/code]


    As you can see from the codes, I have been messing with the recipients part to see if that was the problem, but no joy

    Regards

    Kim
    Copy linkTweet thisAlerts:
    @ginerjmJun 26.2014 — Did you write this html? I don't believe so because it is rather comprehensive yet stupid. YOu are using a table for layout yet adding div tags inside td elements to do formatting. Ridiculous!! Really - you should re-do it. Simplify it. Make it modern.

    Looking thru this I see a) html that displays a page and b) php that reads your input from a) and sends an email.

    1 - Don't use $_REQUEST. You know for a fact that you are processing a POST so why are you using the middle-ground that can leave you open to hacking. Never use REQUEST.

    2 - your email is referencing a field $recipient which does not exist.

    3 - you have an error in recipient emails #3

    4 - take out the refresh until you get it all debugged. I did and things are a bit easier to see now.

    So - what is the relation of these two modules? You call up the html file and get a page for input? Then you hit submit and the php module is called to process it? And what exactly happens now? Are you seeing any of your debugging on the screen? YOu have to give me a picture of what you are doing and what you are seeing.
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJun 27.2014 — The html was created automatically when I designed the form in Dreamweaver MX, which although old is the only software I have for this sort of thing. From the original form, which worked in sending the form content to my email address, the only thing I tried to change was the recipient part. All of the tags elements validation etc were created by Dreamweaver.

    Yes - What you looked through is what I am trying to achieve, but the email to go to a selected recipient.

  • 1. Noted.

  • 2. Oops - my bad. I must have deleted the comma by mistake when amending the code in order to post it here. The comma is there on my development copy.

  • 3. Will do.



  • OK. Action and events.

    I load up my form page, fill in the text boxes for name, telephone, email and comments, click the please select list box and select my required recipient, and hit the submit button.

    Then I get this:

    Info icon


    The website cannot display the page

    HTTP 500

    Most likely causes:

    •The website is under maintenance.

    •The website has a programming error.

    What you can try:

    Refresh the page.


    Go back to the previous page.


    More information More information

    I don't get any debugging, or anything else, just this error screen.

    I will go away and start again with the php and amending the html, and see how far I get.
    Copy linkTweet thisAlerts:
    @ginerjmJun 27.2014 — What page is it trying to display? What is the url in the address bar? Something you expect?
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJun 27.2014 — It looks as if it trying to run the php. The URL is http:/www.example.com/website_process.php

    That is the name of my php script.
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJun 27.2014 — It looks as if it trying to run the php. The URL is http:/www.example.com/website_process.php

    That is the name of my php script.[/QUOTE]


    Obviously not example.com.
    Copy linkTweet thisAlerts:
    @ginerjmJun 27.2014 — And is that script located in the root folder of your domain?
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJun 27.2014 — No, it is in the same folder as the form page, public_html. However, my original form page and its associated php script are also in the same folder (with different names to my current test obviously) so is it a security matter to have the script in the root rather than public folder ?
    Copy linkTweet thisAlerts:
    @ginerjmJun 29.2014 — public-html is most like your root folder.
    Copy linkTweet thisAlerts:
    @FrickJun 30.2014 — this is so difficult , i don't know what are you guys talking about !
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJul 06.2014 — Hi folks,

    I have finally had the time to rework the codes, mainly thanks to ginerjm's guidance (many thanks). and the form now does what I want it to do, i.e send the form content to a specific recipient depending on which of the options in the drop down box is selected, but without having the email addresses in the html.

    So, for those who are interested, the code, very inelegant but functional, is below:

    The HTML code:
    [code=html]
    <html>
    <head>
    <title>Welcome to Example - Contact Us</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.01
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' - must contain an e-mail address.n';
    } else if (test!='R') { num = parseFloat(val);
    if (isNaN(val)) errors+='- '+nm+' - Please enter just a number. Do not enter spaces or dashes etcn';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' - Please provide details.n'; }
    } if (errors) alert('The following error(s) occurred:n'+errors);
    document.MM_returnValue = (errors == '');
    }
    //-->
    </script>
    </head>

    <body bgcolor="#FFFFFF" background="goldmold.jpg">
    <div id="Layer1" style="position:absolute; width:207px; height:42px; z-index:3; left: 10px; top: 19px;"><font size="6" face="Calligraph421 BT"><strong>Welcome
    to <font size="7">Example</font></strong></font></div>
    <h1>&nbsp;</h1>
    <p align="center"><font size="+4"><strong>Contact Us</strong></font></p>
    <hr>
    <div align="right"><font size="-1"><strong><a href="index.html" style="text-decoration = None"><font color="#000000" face="Calligraph421 BT">Return
    to Home Page</font></a></strong></font></div>
    <blockquote>
    <blockquote>
    <blockquote>
    <p align="center"><font size="+1" face="Calligraph421 BT"><strong>For any
    enquiries relating to the Village Hall, please either telephone the Bookings
    Secretary between 08:00 and 20:00 Monday to Friday on 0845-643-0000, or
    <a href="mailto:[email protected]"><img src="emailbutton.jpg" width="70" height="20" border="0"></a>
    her with any questions.</strong></font></p>
    </blockquote>
    </blockquote>
    </blockquote>
    <p align="center"><strong><font size="+1" face="Calligraph421 BT">Alternatively,
    or for other village matters, please complete and submit the short form below,
    and we will respond as soon as possible</font></strong></p>
    <form action="website_process.php" method="post" enctype="multipart/form-data" name="Contactform" id="Contactform">
    <table width="50%" border="1" align="center">
    <tr>
    <td width="50%" height="64"><div align="center">Full Name:</div></td>
    <td width="50%"><div align="center">
    <input name="Name" type="text" id="Name" size="48%">
    </div></td>
    </tr>
    <tr>
    <td width="50%" height="64"><div align="center">Telephone No.</div></td>
    <td width="50%"><div align="center">
    <input name="Telephone" type="text" id="Telephone" size="48%">
    </div></td>
    </tr>
    <tr>
    <td width="50%" height="67"><div align="center">Email:</div></td>
    <td width="50%"><div align="center">
    <input name="Email" type="text" id="Email" size="48%">
    </div></td>
    </tr>
    <tr>
    <td width="50%" height="56"><div align="center">Nature of your Enquiry ?</div></td>
    <td width="50%"><div align="center">
    <select name="Nature" size="1" id="Nature">
    <option>Please Select</option>
    <option value="General_Enquiries">General Enquiries</option>

    <option value="Hall_Hire">Hall Hire</option>
    <option value="Marquee_Hire">Marquee Hire</option>
    <option value="Parish_Church">Parish Church</option>
    <option value="Parish_Council">Parish Council</option>
    <option value="Recreation_Area">Recreation Area</option>
    <option value="Social_Club">Social Club</option>
    <option value="Other">Other</option>
    </select>

    </div></td>
    </tr>
    <tr>
    <td width="50%" height="127"><div align="center">Enquiry Details:</div></td>
    <td width="50%"><div align="center">
    <textarea name="Questions" cols="40%" rows="4" id="Questions"></textarea>
    </div></td>
    </tr>
    <tr>
    <td width="50%"><div align="center">
    <input name="contactSubmit" type="submit" id="contactSubmit" onClick="MM_validateForm('Name','','R','Telephone','','RisNum','Email','','NisEmail','Questions','','R');return document.MM_returnValue" value="Submit">
    </div></td>
    <td width="50%"><div align="center">
    <input name="ContactReset" type="reset" id="ContactReset" value="Reset">
    </div></td>
    </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp; </p>
    </form>
    <p align="center">&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    </body>
    </html>
    [/code]


    and the PHP code:
    [code=php]
    <?php

    /* Error Checking */
    error_reporting(E_ALL | E_NOTICE);

    ini_set('display_errors', '1');

    /* Email Variables */
    $emailSubject = 'Enquiry from Example Website';
    $recipients = array(

    General_Enquiries => '[email protected]',
    Hall_Hire => '[email protected]',
    Marquee_Hire => '[email protected]'
    Parish_Church => '[email protected]',
    Parish_Council => '[email protected]',
    Recreation_Area => '[email protected]',
    Social_Club => '[email protected]',
    Other => '[email protected]'
    );
    $my_email = $recipients[$_POST['Nature']];

    /* Data Variables */
    $Name = $_POST['Name'];
    $Telephone = $_POST['Telephone'];
    $Email = $_POST['Email'];
    $Nature = $_POST['Nature'];
    $Questions = $_POST['Questions'];

    /* Email Content*/
    $body = <<<EOD
    <br><hr><br>
    You have an enquiry from the Example Website <br>
    <br><hr><br>
    Name: $Name <br>
    Telephone: $Telephone <br>
    Email: $Email <br>
    Nature of Enquiry: $Nature <br>
    Enquiry Details: $Questions <br>
    EOD;
    $headers = "From: $Emailrn";
    $headers .= "Content-type: text/htmlrn";
    $success = mail($my_email, $emailSubject, $body,
    $headers);

    /* Results */
    $theResults = <<<EOD
    <html>
    <head>
    <title>sent message</title>
    <meta http-equiv="refresh" content="5;URL=http://example.com/index.html">
    <style type="text/css">
    body {
    background-color: #444;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 20px;
    color: #fec001;
    padding-top: 200px;
    width: 800px;
    margin: auto;
    text-align: center;
    }
    </style>
    </head>
    <body>
    <div>Thank you for your enquiry. Your message has been sent and we will respond as soon as possible.</div>
    </body>
    </html>
    EOD;
    echo "$theResults";
    ?>
    [/code]


    Thanks once again for the guidance.
    Copy linkTweet thisAlerts:
    @ginerjmJul 06.2014 — Jacques1 (and others) will have a field day with your output of unsanitized values coming from the user. You need to learn how to protect yourself from malfeasance by your users. One should never send something to the client that has not been scoured for possible trouble. IE, all of your post vars that you are sending back to the screen should be treated with the following:
    [code=php]
    function html_ready($value,$options=ENT_QUOTES,$charcode='UTF-8')
    {
    return htmlspecialchars($value,$options,$charcode);
    }
    [/code]


    Use this function on each of the vars that the client provided before you output them.

    Glad you got it working! But - I do wish you had re-written that horrible mess of html with divs embedded inside a table. Are you using a div tag simply to provide a deprecated type of centering? LOL!!!
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJul 06.2014 — Thanks JG.

    I agree that I need to learn an awful lot - about all of this. To start with, what you mean by sending post vars back to the screen ? The only thing that appears on the screen is the contact form, then the "Thank You etc" when the form is submitted, then it returns to the website home page.

    I have tried to follow all of your posts, but confess to losing the thread of your guidance on occasion. Could I ask you to please explain your last post in the simplest of layman terms to make sure I understand it, and where your protection code should go. If you assume, mostly correctly, that I know next to nothing about html or php or any of it, I would be most grateful.

    With regard to the divs embedded within the table, I did not (knowingly) put them there. I drew the form, inserted a table, put in the labels & text fields and centred them within each table cell - My 14 year old version of Dreamweaver MX wrote the html code based on what I had drawn.

    I fully appreciate that all this is not really something a rank amateur like me should be doing, but, as I have said before, this is for our small village. We simply don't have the budget to have it done professionally, so as a mere 60 year old younger member of our committee (and with no-one in the village with any programming knowledge) I was "volunteered" to do it, and try and make the best of it. Hence my plea for guidance.
    Copy linkTweet thisAlerts:
    @kimmcmsauthorJul 07.2014 — Hi JG.

    OK.I have been reading up on the code you kindly wrote above, and think I now understand at least in part the whats and whys thereof. If my understanding is correct, it is to convert single and double inverted commas, and less than/more than arrows to html so that these charcters will send as text rather than code which could be dangerous.

    I do, however, have a couple of question still, if you would indulge me ?

  • 1. I am still not sure what you mean by sending post vars back to the screen.

  • 2. Does the code above need to go into the php above each instance of $_POST in the /*data variables*/ individually and above the '$my_email =' ? Please would you clarify.

  • 3. You have set the character code to UTF-8, which I understand is the current standard. Will this have any effect on the iso-8859-1 character set in the html ?


  • Regards
    Copy linkTweet thisAlerts:
    @ginerjmJul 07.2014 — You should add the function to your code preferably at the end of your php block or somewhere NOT in the middle of things. Then you use it by simply calling it in a statement.

    Your code:
    [code=php]

    /* Data Variables */
    $Name = $_POST['Name'];
    $Telephone = $_POST['Telephone'];
    $Email = $_POST['Email'];
    $Nature = $_POST['Nature'];
    $Questions = $_POST['Questions'];

    /* Email Content*/
    $body = <<<EOD
    <br><hr><br>
    You have an enquiry from the Example Website <br>
    <br><hr><br>
    Name: $Name <br>
    Telephone: $Telephone <br>
    Email: $Email <br>
    Nature of Enquiry: $Nature <br>
    Enquiry Details: $Questions <br>
    EOD;
    [/code]


    The first part grabs post vars and assigns them to local vars. You then use them to develop the $body var which is being echoed back to the client to confirm what they have submitted for mailing. Well if they input something malicious as a Name or a Nature and you echo that back to the screen un-filtered, who knows what might happen? Hence the function that you should use thusly:
    [code=php]
    $Name = html_ready($_POST['Name']);
    [/code]


    Do this for each var that you get from the user before sending it back out.


    ****
    Actually - I see now that you build the $body var but you are only using it for the body of the email and I guess you can ignore what I said in this particular instance. BUT - it is good knowledge to save for a future effort. Don't echo out unpredictable data (ie, user-provided) to the client.
    ×

    Success!

    Help @kimmcms spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.26,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...