NameSilo

PHP Form help - Beginner

Spaceship Spaceship
Watch

neilfahey

Established Member
Impact
0
Hey everyone,

I've got a simple PHP mail form working on the intranet at work but now I want to customize it for our use. Basically it's going to be a form that teachers can fill in when there is a lock-down (drill or otherwise) to inform the necessary people that they are okay.

The script consists of two files - sendeail.php
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Sendemail Script</title>
</head>
<body>

<!-- Required: YOUR email ($myemail). Optional: Enter CC email address ($ccx) 
Required: Add the link for the 'next page' (at the bottom) --> 

<?php

$myemail = "[email protected]"; 
$ccx = ""; 

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n"; 
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}
echo $badinput;

$todayis = date("l, F j, Y, g:i a") ;

$attn = $attn . "(" . $ccopy . ")" ; 
$subject = $attn; 

$notes = stripcslashes($notes); 

$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n 
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $visitormail\r\n";

if (($ccopy == "ccyes") && ($visitormail != "")) 
mail($visitormail, $subject, $message, $from);

if ($myemail != "") 
mail($myemail, $subject, $message, $from);

if ($ccx != "") 
mail($ccx, $subject, $message, $from);

?>

<p align="center">
Date: <?php echo $todayis ?> 
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> ) 
<br />

Attention: <?php echo $attn ?>
<br /> 
Message:<br /> 
<?php $notesout = str_replace("\r", "<br/>", $notes); 
echo $notesout; ?> 
<br />
<?php echo $ip ?> 

<br /><br />
<a href="contact.php"> Next Page </a> 
</p> 

</body>
</html>


...and contact.php
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Form </title>
</head>
<body>

<form method="post" action="sendeail.php">

<!-- DO NOT change ANY of the php sections -->
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />


Your Name: <br />
<input type="text" name="visitor" size="35" />
<br />
Your Email:<br />
<input type="text" name="visitormail" size="35" />
<br /> <br />
All Students Accounted For ? No
<input name="ccopy" type="radio" value="ccno" />   Yes<input checked="checked" name="ccopy" type="radio" value="ccyes" /> <br /> 

<br />
Attention:<br />
<select name="attn" size="1">
<option value=" Sales n Billing ">Sales n Billing </option> 
<option value=" General Support ">General Support </option> 
<option value=" Technical Support ">Technical Support </option> 
<option value=" Webmaster ">Webmaster </option> 
</select>
<br /><br />
Mail Message:
<br />
<textarea name="notes" rows="4" cols="40"></textarea>
<br />
<input type="submit" value="Send Mail" />
<br />

<!-- Free Code at: http://www.ibdhost.com/contact/ --> 

</form>

</body>
</html>

So what I need to do is listed below:

- Rather than the user inputting their e-mail address, I need it to always send FROM a specified address (eg. [email protected])

- The 'Your Name' field needs to display in the 'Subject:' header of the resulting e-mail

- Where it says 'All Students Accounted For?' was actually a 'CC:' option but I just need it to list the YES or NO answer in the resulting e-mail

- I also want to add a couple more multi-line text boxes where they can add names of missing students and other staff in the classroom

I think that's about it... I'm sorry but I have NO IDEA where to start! Can somebody help me?

Thanks heaps in advance!
Neil

EDIT: Removed e-mail address
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
You eman you are looking for someone to hire?
 
0
•••
I didn't realise it would be that much work? I thought someone could just give me a quick tip... Sorry about that! I can give ya $74 namebucks but you'll have to tell me how (Thats what people do isn't it? I don't even know what they are!) :)

Maybe I'll look around the web for more help and come back when I've got more specific questions....

Cheers
Neil
 
0
•••
The PHP mail() command takes four parameters:

mail(<email address where it is going to>, <subject>, <body of email>, <additional headers>);


All you need to do is alter the variables that you need. For example, to alter what the subject looks like, all you need to do is adjust this lines accordingly:

$attn = $attn . "(" . $ccopy . ")" ;

The same goes for the other things.

-Bob
 
0
•••
Thanks Moondog! That's a great start... I'll have a crack at it now.
 
0
•••
Is this sort of what you are looking for?

mail.php

PHP:
<html>
<head>
<title></title>
</head>

<body>

<form method="post" action="mail.php">
Name:ย <input type="text" name="name">
<br>
All students accounted for?ย 
<select name="accounted_for">
<option name="Yes">Yes</option>
<option name="No">No</option>
</select>
<br>
<br>
Missing:
<br>
<textarea name="missing" rows="15" cols="65"></textarea>
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>

<?php

if(isset($_POST['submit']))
{
  $name = trim(stripslashes($_POST['name']));
  $all_students = $_POST['accounted_for'];
  $missing = trim(stripslashes($_POST['missing']);
  
  $date = date("m/d/Y");
  $ip = $_SERVER['REMOTE_ADDR'];
  
  $headers = "From: [email protected]";
  
  mail("[email protected]", "$name", "
  
  From: $name
  
  All students accounted for?
  $all_students
  
  Missing:
  
  $missing

  IP Address:
  $ip
  
  Date Sent:
  $date

  ", $headers);
  
  if(!mail)
  {
    echo 'Mail sent.';
  }
  else
  {
    echo 'Seems to have been a problem, mail could not be sent.';
  }
}
?>

</body>
</html>
 
0
•••
Darn it! That'll teach me to check my e-mail more often! That looks pretty good but blissfully unaware, I've been having a crack at it myself... Here's what I came up with: (THANKYOU HEAPS SecondVersion FOR YOUR POST!)

sendeail.php
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Sendemail Script</title>
</head>
<body>

<!-- $myemail is the address that lockdown reports will go to --> 

<?php

$myemail = "[email protected]"; 
$ccx = ""; 

if(empty($name) || empty($grades) || empty($missing )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}


$todayis = date("l, F j, Y, g:i a") ;

$subject = $name; 

$missing = stripcslashes($missing); 

$message = " $todayis [EST] \n
Staff Member Submitting Form: $name \n
Grade(s): $grades \n
All Students Accounted For?  $acc \n
Missing Students: $missing \n 
Names of Others In Classroom (eg. Support Staff, Visitors, Parents, Tradesmen): $other \n
";

$from = "From: $name \r\n";



if ($myemail != "") 
mail($myemail, $subject, $message, $from);

if ($ccx != "") 
mail($ccx, $subject, $message, $from);

?>

<p align="center">
<strong>Thankyou <?php echo $name ?> 
<br />
<br />
You sent the following results:</strong><br />
<br />
<strong>Grade(s):</strong> <?php echo $grades ?>
<br />
<br />
<strong>All Students Accounted For?</strong> <?php echo $acc ?>
<br />
<br /> 
<strong>Missing Students:</strong><br /> 
<?php echo $missing ?> 
<br />
<br />
<strong>Names of Others In Classroom (eg. Support Staff,<br />
Visitors, Parents, Tradesmen):</strong> <?php echo $other ?><br />
<br />
<strong>Date & Time Sent:</strong> <?php echo $todayis ?>

<br /><br />
<a href="http://10.77.81.13:81"> Home </a> 
</p> 

</body>
</html>



contact.php
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lockdown Form</title>
</head>
<body>

<form method="post" action="sendeail.php">

<!-- DO NOT change ANY of the php sections -->
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />


Your Name: <br />
<input name="name" type="text" id="name" size="35" />
<br />
<br />
Grade(s):<br />
<input name="grades" type="text" id="grades" size="35" />
<br /> <br />
All Students Accounted For ? No
<input name="acc" type="radio" value="No" />   
Yes<input checked="checked" name="acc" type="radio" value="Yes" /> 
<br />
<br /> 

Names of Missing Children:
<br />
<textarea name="missing" cols="40" rows="4" id="missing"></textarea>
<br />
<br />
Names of Others In Classroom (eg. Support

Staff,<br /> 
Visitors, Parents, Tradesmen):<br />
<textarea name="other" cols="40" rows="4" id="other"></textarea>
<br />
<br />
<input type="submit" value="Send Mail" />
<br />

<!-- Free Code at: http://www.ibdhost.com/contact/ --> 

</form>

</body>
</html>


This is actually doing EVERYTHING I want it to do now except for two things...

- I want the From: field of the resulting e-mail to display either the real address that it comes from or just 'Lockdown Report'... Currently it comes up with [email protected] (where name is what the user has typed as their name)

- I was also wondering if it was possible to tabulate the resulting e-mail or at least bold the answers to the questions, so the person who receives the e-mail isn't sifting through it all

Hope that's clear enough... Can anyone help?
Thanks
Neil

PS. Thanks again to moondog for getting me started!! Thankfully I've got a little bit of a grounding in programming (from 10 or more years ago) so I was able to figure most things out from there!

Just found another problem I'm having trouble with too...

If you leave a field blank and click 'Submit' it tells you to go back and fill in all fields, but it still sends the e-mail! :) Is there any way I can tell it not to send the e-mail in this case?

Thanks again,
Neil
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back