Forum Moderators: coopster

Message Too Old, No Replies

In need of help - php form

processing form

         

cmurphy08

7:02 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



Ok, I have basically taught myself html and php forms.

I am trying to make a drop down menu for people to choose which one of our departments will receive the form.

I discovered this code to create the drop down menu.

<select name="recipient" size="1">
<option value="email@domain.com" selected>001</option>
<option value="email@domain.com">002</option>
</select>

So I tested it with the rest of the code. When I preview the form before clicking submit, it comes up displaying the email address it is going to be submitted to, but it's never sent to the specified inbox.

Do I need to put code in the 'process' file? If so, what code do I need to put in to make the form go through properly?

andrewsmd

7:44 pm on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you mean it's never sent to the specified inbox? Do you want some value sent to an email address? If so, you need to use the php mail function. Something like this.
<html>
<body>
<form name="myform" method="post">
<select name="recipient" size="1">
<option value="email@domain.com" selected>001</option>
<option value="email@domain.com">002</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
<?php
//if they click submit
if(isset($_POST['submit'])){
$to = $_POST['recipient'];
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
}//if isset
?>
</body>
</html>

cmurphy08

7:52 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



So would that go in the form php file or the process php file?

When I say it was never sent to the inbox, I mean I clicked submit and the email was never delivered. Everything worked with the form until that point.

Sorry, it's hard explaining myself when I don't exactly know what I am doing.

andrewsmd

7:59 pm on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't understand, what "process" file are you talking about. The code I provided will work as is, provided that php.ini has the proper smtp settings. You could put it in another file called "process.php" if you want, but then you have to change your structure like this. This file would be called yourFileName.html
<html>
<body>
<form name="myform" method="post" onsubmit="process.php">
<select name="recipient" size="1">
<option value="email@domain.com" selected>001</option>
<option value="email@domain.com">002</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

then in "process.php" you would have this code
<?php
//if they click submit
if(isset($_POST['submit'])){
$to = $_POST['recipient'];
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
}//if isset
?>

You will notice in the html form tag I added onsubmit="process.php" that's how you tell it to execute a file when the user clicks submit. However, you can just use the code I posted all in one file if you want. As long as the file extension is .php.

CyBerAliEn

9:46 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



Technically, this is incorrect:

<form name="myform" method="post" onsubmit="process.php">

You will notice in the html form tag I added onsubmit="process.php" that's how you tell it to execute a file when the user clicks submit.

It may work in some browsers... but the "onSubmit" attribute is a Javascript event; when the form is submitted, the browser will execute whatever Javascript code is in the HTML form's attribute "obSubmit".

To tell the form to use a separate file to "send the data to", you should specify the HTML form tag as:

<form name="myform" method="post" action="process.php">

The correct attribute is "action". You can specify any URL as the value (in other words, you can send the form data to any page or website).

Now for the original poster... I assume you have a setup where you have a HTML page with a form, and a separate PHP file which is used to process it? Assuming you have your HTML form sending the form data to the right PHP file, in order to send the email to the "selected" email address, you would need to do the following:
(1) retrieve the form field's value for where to send it [$sendTo = $_REQUEST['recipient'];];
(2) make sure the PHP function 'mail()' is being called to send the email; and be sure that the "recipient" value is passed as the proper argument to the function, such as: mail($sendTo,$subject,$message,$headers);

Assuming this processor script is already coded, it should already have this function in it. You just need to add/modify the code so that you "retrieve" the 'recipient' value from the form data, and then set it as the "to" argument in the PHP mail function.

Hopefully that makes sense.

andrewsmd

10:00 pm on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes my bad, thanks for catching that. Personally I never use those events I'm just confused as to what cmurphy08 is trying to do.

rocknbil

2:01 am on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry to derail the thread a little, but since you're new at this, better head this off before it gets to be a habit. Do not do this.

<select name="recipient" size="1">
<option value="email@domain.com" selected>001</option>
<option value="email@domain.com">002</option>
</select>

This exposes these email addresses to spammers, and you'll have more offers for Rolex watches, diplomas, and v***ra than you'll know what to do with.

Do this instead. In your form.php,

<select name="recipient" size="1">
<option value="1" selected>001</option>
<option value="2">002</option>
</select>

Then in your process.php, right at or near the top of the script, create an associative array connecting the numbers with the addresses:

$emails = Array (
1 => 'address1@domain.com',
2 => 'address2@domain.com'
);

The rest is as andrew describes, with one small change. You need to reference the submitted "recipient" as the key of the $emails array:

$to = $emails[$_POST['recipient']];
//$to is now "address1@domain.com" if 1 is selected

This keeps your email addresses out of public view.

andrewsmd

1:21 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice suggestion rocknbil, post back when you have more questions cmurphy08.

cmurphy08

2:12 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Ok, so I read what everyone said to do.

I used this in my form:
<select name="recipient" size="1">
<option value="1" selected>001</option>
<option value="2">002</option>
</select>

and then...
$to = $emails[$_POST['recipient']];
//$to is now "address1@domain.com" if 1 is selected

I tested it on my website and everything worked fine. I went back to test it again and then when I clicked submit it said I had an error and wouldn't let me proceed? I don't get why it would have worked and then stop working?

I then put this in the process file:
$emails = Array (
1 => 'address1@domain.com',
2 => 'address2@domain.com'
);

andrewsmd

2:34 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you post the error message you receive? Just an FYI, whenever you get an error posting the error message can be very helpful.

cmurphy08

2:37 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



So I fill out the form, click submit for review, (review it) and then click submit and then its has an HTTP 500 Internal Server Error.

andrewsmd

4:11 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That means its a generic server error. How is your php hosted. Are you using a shared system or something. I'm guessing there is probably something wrong with your smtp settings but I wouldn't know for sure.

cmurphy08

4:14 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Umm.. haha yea what do you mean how my php is hosted? We have a our own server and everything is on that. My other forms are working fine though?

rocknbil

5:59 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I then put this in the process file:
$emails = Array (
1 => 'address1@domain.com',
2 => 'address2@domain.com'
);

Sorry if this is a stupid question, but did you change the email address values to your real email addresses?

A 500 on a PHP script generally means some other function within the script is kicking the 500 - and I'm guessing that might be the mail routines.

Do you know if PHP error reporting is in for your server? It's off by default as a security issue, hackers can glean a lot about a server's configuration by causing a script to error.

First stop is your error logs. If you have command line /SSH access,

tail /path/to/domain/error_log

Will give you the latest logs. Otherwise you can get to it via any control panel you have.

Next put this at the top of your script - but DO NOT LEAVE IT THERE when it's deployed live:

error_reporting(E_ALL);
ini_set("display_errors", 1);

andrewsmd

6:01 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have your own server, then that's where you php is hosted. You will need to check your smtp settings in the php.ini folder. If you have no idea what that is, then check with the guy who installed your php. If it is set to the default then your smtp server will be local host so you need to make sure your server has smtp forwarding enabled. If not, check out how to use ini_set("SMTP", "yoursmtpserver"); to set the smtp server for your application.

cmurphy08

6:16 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



In response to rocknbil, what do you mean did I change the email address values to your real email address? Where would that be. Sorry for being annoying.

rocknbil

9:55 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$emails = Array (
1 => 'Enter REAL email address 1 here',
2 => 'Enter REAL email address 2 here'
);

cmurphy08

10:08 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Yes I did that.

rocknbil

2:44 am on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay. Enough guesswork. :-) Yeah that's why I thought it might be a stupid question.

I took andrew's code and whipped something up. Copy the entire code below, paste in "mailer.php." It has been tested with real email addresses. Upload and test.

Note how the entire deal - form and all - is in one file. Study the comments, and do not use it as is for a live application. Once you get a handle on what's happening, explore the term email injection php on the search engine of your choice.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype on one line -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Mailer Example</title>
</head>
<body>
<!-- CAUTION: This is for a simple mailer example only
and has no input filtering, no error checking, and is likely
subject to email injection! -->


<?php
// It's standard to put all configurable variables right
// at the top of your script, where you can find them.
// Define these first. Note how the form is output.
$emails = Array (
1 => 'tech@example.com',
2 => 'assist@example.com'
);
// Don't put the email addresses in the response
// or the form. Use this array.
$persons = Array (
1 => 'John Doe',
2 => 'Mary Doe'
);
// Who it's to
$to = $emails[$_POST['recipient']];
// If there's no email entered, you need a from.
$default_from = 'no-reply@example.com';
// Who it's from
$from = (isset($_POST['from']))?$_POST['from']:$default_email;
$subject = "Test email to " . $persons[$_POST['recipient']];

// If it's been submitted, process
if (isset($_POST['submitButton'])) {
$txt = ($_POST['msg'] != '')?$_POST['msg']:'No message was entered';
// This overwrites "$txt" but puts the
// message stored in $txt at the end.
// Note how it's got $txt after "Message." This is
// the beauty of how variables are interpolated first,
// then stored in the variable on the left, opposite of how we
// read them.
$txt = "
From: $from
subject: $subject
Message: $txt
";
$headers = "From: $from\r\n";
mail($to,$subject,$txt,$headers);
$response = '
<h1>Email Successfully Sent</h1>
<p>The email was successfully sent to ' .
$persons[$_POST['recipient']] . '.</p>';
print $response; // or echo
}

// If it's not been submitted, output the form
else {
$form = '
<form name="myform" id="myform" method="post" action="mailer.php">
<p><label for="recipient">Select Recipient:</label>
<select name="recipient" id="recipient">
<!-- don\'t need selected in first list item -->
';
foreach ($emails as $key=>$value) {
$form .= '<option value="' . $key . '">' . $persons[$key] . "</option>\n";
}
$form .= '
</select></p>
<p><label for="from">Your Email:</label>
<input type="text" name="from" id="from" size="25" value=""></p>
<p><label for="msg">Message:</label>
<textarea name="msg" id="msg" rows="3" cols="25">
This is a test email. This is a test email.
This is a test email. This is a test email.
</textarea></p>
<!-- note change of submit name, never use possible reserved words -->
<input type="submit" name="submitButton" id="submitButton" value="submit">
</form>
</body>
</html>
'; //End form
print $form;
}
?>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

cmurphy08

12:36 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



I read through the code and your notes. So, let's see if I understand all of this. This code is the form and then the process file all in one? So, I can take this code, put it in dreamweaver and then just add the form components I need and then obviously add the needed e-mail addresses and such in the code you gave and add then add email injection php and it should work? Because that would be awesome!

rocknbil

6:51 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, I can take this code, put it in dreamweaver and then just add the form components I need

[IMO]
For the love of [whatever deity you follow] do not edit this in Dreamweaver. DW is a WYSIWYG html editor, it is not really designed to modify scripts. Use a powerful text editor - I still use HomeSite, if you have the original disks for DW before Adobe took over Dreamweaver, you will find it on the disks. If you don't have anything else, copy and paste the script into notepad.
[/IMO]

then add email injection php

I was suggesting you search google for these terms, learn what email injection is, then learn how to prevent it, and implement it in your script.

For now, just copy and paste the example program into notepad, save it as "mailer.php" (note the action of the form.) Change the email addresses to your emails. Upload and test.

Yes, it's an all-in-one-file, form and script. Test it out, study it, see cause and effect when you run it. Baby steps to coding. :-)

cmurphy08

7:06 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



Oh ok about the email injection. I guess I need to transition to a script editor. My problem is i am design person and am not familiar enough with script to just write stuff, wish I was, but that is why i use dreamweaver. The web designer that made our site also said that would be the best option for me. Oh well.

andrewsmd

8:56 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use ConText or Eclipse as both an html/php editor they are both free.