Log in

View Full Version : html form to php email form



weiner769
01-31-2010, 12:11 PM
I need help changing my html form entries to php mail. Their seems to be some problem with getting the submitted form into the php file and then emailing it to me. I repeatedly have gotten emails from the php file, but their is no body to the email.


<form action="sendmail.php" method="post" enctype="application/x-www-form-urlencoded" name="Application" target="_blank" id="Application">
<table width="625" height="691" border="0">
<tr>
<td width="207"><label>
<div align="right">Name:</div></td>
<td width="408"><label>
<input type="text" name="name" id="name" />
<br />
</label></td>
</tr>
<tr>
<td><div align="right">Address:</div></td>
<td><label>
<input type="text" name="address" id="address" />
</label></td>
</tr>
<tr>
<td><div align="right">Address 2:</div></td>
<td><label>
<input type="text" name="address2" id="address2" />
</label></td>
</tr>
<tr>
<td><div align="right">City:</div></td>
<td><label>
<input type="text" name="city" id="city" />
</label></td>
</tr>
<tr>
<td><div align="right">State:</div></td>
<td><label>
<select name="state" size="1" id="state">
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DC">DC</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="IA">IA</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="MA">MA</option>
<option value="MD">MD</option>
<option value="ME">ME</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MO">MO</option>
<option value="MS">MS</option>
<option value="MT">MT</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="NE">NE</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NV">NV</option>
<option value="NY">NY</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VA">VA</option>
<option value="VT">VT</option>
<option value="WA">WA</option>
<option value="WI">WI</option>
<option value="WV">WV</option>
<option value="WY">WY</option>
</select>
</label></td>
</tr>
<tr>
<td><div align="right">Zip Code:
</div></td>
<td><label>
<input type="text" name="zipcode" id="zipcode" />
</label></td>
</tr>
<tr>
<td><div align="right">*Cell Phone:</div></td>
<td><label>
<input type="text" name="cellphone" id="cellphone" />
</label></td>
</tr>
<tr>
<td><div align="right">*Email:</div></td>
<td><label>
<input type="text" name="email" id="email" />
</label></td>
</tr>
<tr>
<td><div align="right">Birth Date:</div></td>
<td><label>
<input type="text" name="birthdate" id="birthdate" />
</label></td>
</tr>
<tr>
<td><div align="right">Ethnicity:</div></td>
<td><label>
<input type="text" name="ethnicity" id="ethnicity" />
</label></td>
</tr>
<tr>
<td><div align="right">Height:</div></td>
<td><label>
<input type="text" name="height" id="height" />
</label></td>
</tr>
<tr>
<td><div align="right">Hair Color:</div></td>
<td><label>
<input type="text" name="haircolor" id="haircolor" />
</label></td>
</tr>
<tr>
<td><div align="right">Body Type:</div></td>
<td><label>
<input type="text" name="bodytype" id="bodytype" />
</label></td>
</tr>
<tr>
<td><div align="right">Weight:</div></td>
<td><label>
<input type="text" name="weight" id="weight" />
</label></td>
</tr>
<tr>
<td><div align="right">Previous Modeling Experience (if any):</div></td>
<td><label>
<textarea name="experience" cols="50" id="experience"></textarea>
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="right"></div></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="right"></div></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="right">*Required fields</div></td>
<td><input type="submit" name="Submit" id="Submit" value="Apply" /></td>
</tr>
</table>
<label><br />
</label>
</form>


<?php
$to= '_@gmail.com';

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

$subject= 'Application';
$from= 'Website';
$name = $_POST["name"] ;
$address = $_POST["address"] ;
$address2 = $_POST["address2"] ;
$city = $_POST["city"] ;
$state = $_POST["state"] ;
$zipcode = $_POST["zipcode"] ;
$cellphone = $_POST["cellphone"] ;
$email = $_POST["email"] ;
$birthdate = $_POST["birthdate"] ;
$ethnicity = $_POST["ethnicity"] ;
$height = $_POST["height"] ;
$haircolor = $_POST["haircolor"] ;
$bodytype = $_POST["bodytype"] ;
$weight = $_POST["weight"] ;
$experience = $_POST["experience"] ;
$body= stripslashes ("name" . $name . "address" . $address . "address2" . $address2 . "city" . $city . "state" . $state . "zipcode" . $zipcode . "cellphone" . $cellphone . "email" . $email . "birthdate" . $birthdate . "ethnicity" . $ethnicity . "height" . $height . "haircolor" . $haircolor . "bodytype" . $bodytype . "weight" . $weight . "experience" . $experience) ;

mail( $to, $subject, $body, $from );
?><title>sendmail.php</title>

Thank you for contacting us! We will be in touch with you very soon.

Thank you in advance for your assistance, I have been at this for 8 hours and have given up lol.

Schmoopy
01-31-2010, 07:50 PM
First thing you might want to try in the debugging process is to see if those variables contain any values. For instance, using a die() statement right after the body variable is assigned, to see if there is any data in it.

Try using
die(print_r($_POST)); in the PHP file, and see what values are in there.

The code looks fine, so perhaps doing this will shed some light on what's happening.

weiner769
02-01-2010, 06:20 PM
Thanks so much for your help! That glitch has now been fixed and it is working great now! In my search for trying to find a cure for my form issue I came across another concern. To my understanding my email address can be harvested and used by spammers. How would I go about avoiding this?

Heres my updated script for those in need lol (and for assistance if those that are well-endowed would be so kind!

<?php
$to= '_@gmail.com';

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

$subject= 'Application';
$from= 'Website';
$name = $_REQUEST['name'] ;
$address = $_REQUEST['address'] ;
$address2 = $_REQUEST['address2'] ;
$city = $_REQUEST['city'] ;
$state = $_REQUEST['state'] ;
$zipcode = $_REQUEST['zipcode'] ;
$cellphone = $_REQUEST['cellphone'] ;
$email = $_REQUEST['email'] ;
$birthdate = $_REQUEST['birthdate'] ;
$ethnicity = $_REQUEST['ethnicity'] ;
$height = $_REQUEST['height'] ;
$haircolor = $_REQUEST['haircolor'] ;
$bodytype = $_REQUEST['bodytype'] ;
$weight = $_REQUEST['weight'] ;
$experience = $_REQUEST['experience'] ;
$body= stripslashes ('name ' . $name . ' address ' . $address . ' address2 ' . $address2 . ' city ' . $city . ' state ' . $state . ' zipcode ' . $zipcode . ' cellphone ' . $cellphone . ' email ' . $email . ' birthdate ' . $birthdate . ' ethnicity ' . $ethnicity . ' height ' . $height . ' haircolor ' . $haircolor . ' bodytype ' . $bodytype . ' weight ' . $weight . ' experience ' . $experience) ;

mail( $to, $subject, $body, $from );
?><title>sendmail.php</title>

Thank you for submitting your application! We will be contacting you shortly!

traq
02-02-2010, 01:09 AM
If you have the email listed on your html page (i.e., action="mailto:myemail@domain.com" ) then this is a concern. However, if you only have the email on your server-side code, it will never be revealed to the typical user or bot (unless they've already hacked your server anyway, and then it's the least of your worries).

weiner769
02-02-2010, 04:45 PM
I am not using a server because that seemed to be a little over my head at this point in time. I cam across a php tutorial that suggested that I ensure that my form is not sent to other emails.


if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {

[error.html]

}

Though I plugged it into my code and it did not work. I instead received this error message: Parse error: syntax error, unexpected '[' in /home/p153j78n/public_html/sendmail.php on line 29

I kind of know how to fix this but would like some assurance, because the r and n and / confuse me lol. Thanks!

Schmoopy
02-02-2010, 05:28 PM
[error.html] Is causing that parse error.

You need to do something with it. Do you want it to redirect to an error page called error.html?


header("Location: error.html");

If not, what do you want the script to do?

traq
02-03-2010, 01:23 AM
I am not using a server because that seemed to be a little over my head at this point in time...

php runs on a server. it may not be your server, but it is on a server.

under normal circumstances, php code (such as your $to= '_@gmail.com'; ) that is not echoed or printed or otherwise sent to the user's browser is inaccessable to everyday visitors and email harvesters.

I believe the script you have above is used to prevent email header injection, so a bot/spammer can't use your form to send tons of email to everyone on the planet. By rejecting email submissions with line breaks, you make sure there's only one email address and not a big list.

If (as seems to be the case) you are sending the email to a predetermined address (yours) and not to a user-submitted email address, this kind of script is not necessary.

weiner769
02-03-2010, 06:36 PM
That is exactly what I am doing! That clears it up. Thanks for your time.

CrystalSC
02-04-2010, 02:55 PM
I am having a php email form issue as well. I hope it's okay that I piggy-back this thread.
This is my first time trying to use a php email form and I am a novice so please bear with me.

Every time I try to send a test email, I'm just getting a blank page. :(

HTML FORM ON PAGE:


<form method="post" action="contact.php">
<div>

<p>
<label for="input-1">Your Name</label><br/>
<input type="text" name="input-1" id="input-1" class="text" />
</p>
<p>
<label for="input-2">Your Email</label><br/>
<input type="text" name="input-2" id="input-2" class="text" />
</p>
<p>
<label for="input-3">Your Website</label><br/>
<input type="text" name="input-3" id="input-3" class="text" />
</p>
<p>
<label>Want to Affiliate?</label><br/>
<input type="radio" name="input-4" id="input-4" /> <label for="input-4" class="radio">Yes</label> <input type="radio" name="input-5" id="input-5" /> <label for="input-5" class="radio">No</label>
</p>
<p>
<label for="input-6">Comment</label><br/>
<textarea name="input-6" id="input-6" rows="10" cols="90"></textarea>
</p>
<input type="submit" value="Send" class="button"/>
</div>
</form>



CONTACT.PHP FORM ON SERVER


<?php
$to = "_@_____.com";
$subject = "Contact Me";
$input-1 = $_REQUEST['input-1'];
$input-2 = $_REQUEST['input-2'];
$input-3 = $_REQUEST['input-3'];
$input-4 = $_REQUEST['input-4'];
$input-5 = $_REQUEST['input-5'];
$input-6 = $_REQUEST['input-6'];
$headers = "From: $input-2";
$sent = mail($to, $subject, $input-1, $input-2, $input-3, $input-4, $input-5, $input-6);
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

SCREENSHOT OF FORM:

http://img.photobucket.com/albums/v13/CrystalPistol/tobedeletedlater/formshot.jpg

SCREENSHOT OF SUBMIT RESULTS:

http://img.photobucket.com/albums/v13/CrystalPistol/tobedeletedlater/th_submitshot.jpg (http://smg.photobucket.com/albums/v13/CrystalPistol/tobedeletedlater/?action=view&current=submitshot.jpg)

As you can see it is calling the php form but not sending the email or displaying results to end user.
I've been using this site (http://php.about.com/od/advancedphp/ss/mail.htm) and this site (http://www.w3schools.com/PHP/php_mail.asp) as my references and after two days, cannot figure out what I'm doing wrong. Any guidance is very much appreciated. Thank you!

bluewalrus
02-04-2010, 04:06 PM
The comma designates other attributes of the mail function try this.

http://php.net/manual/en/function.mail.php


<?php
$to = "_@_____.com";
$subject = "Contact Me";
$input-1 = $_REQUEST['input-1'];
$input-2 = $_REQUEST['input-2'];
$input-3 = $_REQUEST['input-3'];
$input-4 = $_REQUEST['input-4'];
$input-5 = $_REQUEST['input-5'];
$input-6 = $_REQUEST['input-6'];
$headers = "From: $input-2";
$body = $input-1 . $input-2 . $input-3 . $input-4 . $input-5 . $input-6;
$sent = mail($to, $subject, $body);
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

Schmoopy
02-04-2010, 06:07 PM
You forgot about the variables though. $input-1 is not a valid variable name, so it won't work.
Also forgot to include the headers.
This should do it:



<?php
$to = "_@_____.com";
$subject = "Contact Me";
$input1 = $_REQUEST['input-1'];
$input2 = $_REQUEST['input-2'];
$input3 = $_REQUEST['input-3'];
$input4 = $_REQUEST['input-4'];
$input5 = $_REQUEST['input-5'];
$input6 = $_REQUEST['input-6'];
$headers = "From: $input2";
$body = $input1 . $input2 . $input3 . $input4 . $input5 . $input6;
$sent = mail($to, $subject, $body, $headers);
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

CrystalSC
02-04-2010, 06:32 PM
Thank you both so much! I will try again with your suggestions and let you know how it goes. :)

CrystalSC
02-05-2010, 03:18 PM
Yay! I received an email test successfully! Thank you both very much. Your solutions were exactly what was needed. And I renamed the values. ;)

One thing more, rather than the text display after being submitted (i.e.Your mail was sent successfully) I want a page to be displayed. I tried inserting it as such but all it does is display the link as text. Can you please tell me what I'm doing wrong here?

if($sent)
{print "Location: /successful.html"; }
else
{print "Location: /error.html"; }
?>

Also, instead of trying just the partial path, I did try the full http path but that didn't work either.

Again, thank you for your help!!!

bluewalrus
02-05-2010, 04:21 PM
The easiest would be:



if($sent){
include("/successful.html");
} else {
include("/error.html");
}
?>

traq
02-05-2010, 04:25 PM
Have you already printed anything to the page? If you have output anything before sending the header() (http://us3.php.net/manual/en/function.header.php), it won't work. You could try something like this to make sure nothing is output before your headers (this goes at the very top of your page):


<?php
if($sent == 'sent'){ header("Location: www.yoursite.com/successful.html"); exit; }
elseif($sent == 'notsent'){ header("Location: www.yoursite.com/error.html"); exit; }
else{ /*code to process email goes here*/ }


but, as bluewalrus suggests, include() would be just fine too

CrystalSC
02-05-2010, 06:26 PM
Thank you both!! The suggestion bluewalrus gave works just fine for my needs so I'm just going to stick with that.

I really appreciate all the help!! :)

(My issue is resolved)