Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

email form

Status
Not open for further replies.

law78

Technical User
Apr 23, 2004
78
GB
Hi people,

I must be honest, I am not at all fluent in the php scripting language and was wondering if anyone can help me with a problem:

I am using the code included at the very bottom of this message to send a simple email within the flash environment and includes the fields; NAME, EMAIL AND MESSAGE.

How do add extra fields that include a drop down menu and tick boxes?

Here are the fields I am trying to add:

Name: (ALREADY SET UP)
Sex: (M/F) << tick box
D.O.B
College: (dropdown list with Birmingham, Brighton, Bristol, Cambridge, Cardiff, Coventry, Edinburgh, Glasgow, Leeds, Leicester, Liverpool, London, Manchester, Newcastle, Nottingham, Oxford, Salford, Sheffield)

Course:
Year:
NUS/Student no:
e-mail: (ALREADY SET UP)
current mobile no:

are you looking for the following (please tick)

job (part time / casual)

room/house

Here is the PHP code that I am using:

<?php
/***************************************************\
* PHP 4.1.0+ version of email script. For more
* information on the mail() function for PHP, see
* \***************************************************/


// First, set up some variables to serve you in
// getting an email. This includes the email this is
// sent to (yours) and what the subject of this email
// should be. It's a good idea to choose your own
// subject instead of allowing the user to. This will
// help prevent spam filters from snatching this email
// out from under your nose when something unusual is put.

$sendTo = "info@email.com";
$subject = "Message received from the Info Site";

// variables are sent to this PHP page through
// the POST method. $_POST is a global associative array
// of variables passed through this method. From that, we
// can get the values sent to this page from Flash and
// assign them to appropriate variables which can be used
// in the PHP mail() function.


// header information not including sendTo and Subject
// these all go in one variable. First, include From:
$headers = "From: " . $_POST["name"];
$headers .= "<" . $_POST["email"] .">\r\n";
// next include a Reply-To:
$headers .= "Reply-To: " . $_POST["email"];

// now we can add the content of the message to a body variable
$message = $_POST["message"];


// once the variables have been defined, they can be included
// in the mail function call which will send you an email
mail($sendTo, $subject, $message, $headers);
?>


Sorry about the long message but I would appreciate ANY help from you genius'

Thanks....
 
Code:
$name=trim($_POST["name"]);
$email=trim($_POST["email"]);
$sex=trim($_POST["sex"]);
$dob=trim($_POST["dob"]);
$college=trim($_POST["college"]);

$message = "Name: ".$name."\r\n"
	  ."Email: ".$email."\r\n"
	  ."Sex: ".$sex."\r\n"
	  ."DOB: ".$dob."\r\n\n"
	  ."College: ".$college."\r\n"
				etc

You may have to remove the newline and returns from this for flash, I don't know, but you can add anything you like into the message string, as long as the form feild it is comming from is named, and you can use the $_post to get the content .
hope this helps

----------------------------------------
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
Also:

This function let's you strip html/java/php from the string, which can be good, in case your user abuse this to someone who has html-mail enabled.

Just in case you where wondering about html coding:
Code:
<p title="student information">
  <b>Attending course:</b><br />
  Course: <input type="text" name="course" /><br />
  NUS/Student no: <input type="text" name="student_no" /><br />
  current mobile no: <input type="text" name="student_no" /><br />
</p>

<p title="general information">
  <b>Sex? Yes please!</b><br />
  <input type="radio" name="sex" value="Male" /> Male<br />
  <input type="radio" name="sex" value="Female" /> Female
</p>
<p>
  <b>College - Yes, where are you from?</b><br />
  <select name="College">
    <option value="college 1">college 1</option>
    <option value="college 2">college 2</option>
    <option value="college 3">college 3</option>
  </select>
</p>
<p title="looking for">
<b>What are you looking for?</b><br />
<input type="checkbox" name="job" value="1" /> Job (part time / casual)<br />
<input type="checkbox" name="house" value="1" /> House / Room
</p>

ps. I wrote this "On the fly" for you, so I cannot recommend that it is not incorrect! I strongly advise you to read thru it. I think it should be more or less o.k. but I havent had my daily dosage of caffeine yet.
 
I forgot to mention some important things:
You need to check that your users do not double-post, eg. flood your mailbox by reduant posting in the mail-script.

There are several ways to fix this.

1:
You can have an hidden field in the form, which you call "posted" and has value="0". Right after your mail() in the php-code, do a posted++;

then you wrap the mail() inside an:
Code:
if ($posted == "0")
  {
    // the mail() code here
  }

This will make the script increase the $posted to 1, after the mail() has run once.
You can also add more code if you wish:

Code:
else
  {
    echo "Your information has already been sent!";
  }

I would, if I where you, make a function for sending the mail, so you simply call the function from the first if sentence I gave you.

The function can then return the result, which makes it not necessary for you to have loads of if/else, substr, trim, nl2br, etc. in the logic code.

Code:
// example function
function blah($variable = "default value", $variable2 = "default value")
  {
    // do something
    if ($something == "something")
      {
        $result = somefunction($somevar, $anothervar, $onemore, $thelastone);
      }

    // make result more readable
    if ($result == something)
      {
        $result = "something more readable";
      }
    // return the result
    return $result
  } // end function blah
 
Dont suppose anyone could develop the above for me? I am happy using a standard HTML page for the front end ( I will just add a link from flash to a html pop up box).

Just really struggling
 
function for sending mail
Code:
// example function
function sendmail($sendto = "404", $subject = "default subject", $message = "404", $headers="404", $result="404")
  {
    // if nothing is missing
    if ($sendto != 404 && $message != 404 && $headers != 404 && $result = 404)
      {
        mail($sendTo, $subject, $message, $headers); // send mail
        $result = "yeah!"; // updt result to ok
      }

    // return the result
    return $result;

  } // end function blah

I think this should work, but I wrote it "On the fly", so I havent tested it.
 
Anyone looking to build this form, I will pay. There is one more criteria for the form though:

I want an automated email sent to the recipient depending on what college they chose, eg. the user selects Manchester, the automated email gives them the date to which we will be visiting Manchester.

Hope this makes sense, if some one can put this together before the end of tomorrow I will pay them $60 via paypal, not uch I know but its all I can afford unfortunatly.

Let me know.
 
The rest is very easy!

make the form:
Code:
<?php
if (!isset($submit))
  {
echo "<form name=\"sendmail\" method=\"post\" action=\"?\">
<!--# here you enter all the form things, like I gave examples for above-->
<input type=\"submit\" name=\"submit\" value=\"submit\" />
</form>";
  } // end if not submitted
else // if user has submitted
  {
    // here enter code, like [b]darncat[/b] gave example for
    $name=trim($_POST["name"]);
    $email=trim($_POST["email"]);
    $sex=trim($_POST["sex"]);
    $dob=trim($_POST["dob"]);
    $college=trim($_POST["college"]);

    $message = "Name: ".$name."\r\n"
          ."Email: ".$email."\r\n"
          ."Sex: ".$sex."\r\n"
          ."DOB: ".$dob."\r\n\n"
          ."College: ".$college."\r\n"
                etc

    // then we mail! (using my function above)
    $result = sendmail($email, $subject, $message, $headers);
    if ($result == 404)
      {echo "error, some information might be missing!"}
    else
      {
        echo "your submission went great!";
      }
    
  } // end if user has submitted

// here you paste the function I pasted above.. (sendmail)
?>

this too is not 100% working code!! you need to fix all the code which darncat gave example of!
the rest should be 99% done already now!
you also have to generate the html input thingies, but I gave sample of all you need above! checkbox, radio-box, dropdown list, etc.
if you need textarea, it's <textarea></textarea"
it also needs a name!

the name in the input fields, will be the variable-names, when it's been submitted!

eg.. if you have an input:
<input type="hidden" name="boo" value="yes" />

after submitting, $boo == "yes"

I think now you should be able to struggle your way thru the rest.. just patch up the missing html and the missing from the example the cat gave you.
 
law78: I can make it for you, but then you need to send me an e-mail.

I will soon leave work, but I can program from home.

olav-x AT volvo-power.net

since you want automated functions, I need to know how to retrieve such information, if from database, flatfile or whatever.

sincerely,
Olav Alexander Mjelde
 
There is no database in place, can the response not be added to the php file, if there is another way of 'retrieving' the reply message let me know, obviously there is 18 different replies (18 colleges) and I would like it to be developed so that I can change the response.

I am quite happy going into the php script and editing the reply message, obviously this type of into will change so it just needs to be editable... whichever way you choose to do it.

Does this make sense?

p.s. just sent you an email, you received it yet?

Thanks
 
yes! I got the mail, and also I can make a .php script with responses.. long-term project, I would recommend mysql database! with gui for updating information/dates.

I will get back to you in approx 2 hrs.

sincerely,
Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top