Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2012, 03:54 AM   PM User | #1
mjocon
New to the CF scene

 
Join Date: Jul 2012
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
mjocon is an unknown quantity at this point
New to php, need help with contact form please

New to php, im trying to send this form to my email, i pieced together the form using a couple of online tutorials. when i hit submit it just returns with " (0) "

here is the html
**********************************************************


<div id="form_container">

<h2><a>Untitled Form</a></h2>
<form action="test.php" method="post">
<div class="form_description">
<h3>Untitled Form</h3 >
<p>This is your form description. Click here to edit.</p>
</div>
<ul >

<li id="li_1" >
<label class="description" for="element_1">Name *</label>
<span>
<input id="element_1_1" name= "element_1_1" class="element text" maxlength="255" size="8" value=""/>
<label>First *</label>
</span>
<span>
<input id="element_1_2" name= "element_1_2" class="element text" maxlength="255" size="14" value=""/>
<label>Last *</label>
</span>
</li>
<li id="li_2" >
<label class="description" for="element_2">Phone *</label>
<span>
<input id="element_2_1" name="element_2_1" class="element text" size="3" maxlength="3" value="" type="text"> -
<label for="element_2_1">(###)</label>
</span>
<span>
<input id="element_2_2" name="element_2_2" class="element text" size="3" maxlength="3" value="" type="text"> -
<label for="element_2_2">###</label>
</span>
<span>
<input id="element_2_3" name="element_2_3" class="element text" size="4" maxlength="4" value="" type="text">
<label for="element_2_3">####</label>
</span>
</li>
<li id="li_3" >
<label class="description" for="element_3">Email *</label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_4" >
<label class="description" for="element_4">Address <sub>City/ State/ and Zip Code are the only required fields *</sub></label>

<div>
<input id="element_4_1" name="element_4_1" class="element text large" value="" type="text">
<label for="element_4_1">Street Address</label>
</div>

<div>
<input id="element_4_2" name="element_4_2" class="element text large" value="" type="text">
<label for="element_4_2">Address Line 2</label>
</div>

<div class="left">
<input id="element_4_3" name="element_4_3" class="element text medium" value="" type="text">
<label for="element_4_3">City *</label>
</div>

<div class="right">
<select class="element select medium" id="element_4_4" name="element_4_4">
<option value="" selected="selected"></option>
<option value="Hawaii" >Hawaii</option>
</select>
<label for="element_4_4">State *</label>
</div>

<div class="left">
<input id="element_4_5" name="element_4_5" class="element text medium" maxlength="15" value="" type="text">
<label for="element_4_5">Zip Code *</label>
</div>

<div class="right">
<select class="element select medium" id="element_4_6" name="element_4_6">
<option value="" selected="selected"></option>
<option value="United States" >United States</option>
</select>
<label for="element_4_6">Country</label>
</div>

</li>
<li id="li_6" >
<label class="description" for="element_6">What style shoot(s) are you interseted in? </label>
<span>
<input id="element_6_1" name="element_6_1" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_6_1">Portraits</label>
<input id="element_6_2" name="element_6_2" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_6_2">Group</label>
<input id="element_6_3" name="element_6_3" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_6_3">Engagement</label>
<input id="element_6_4" name="element_6_4" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_6_4">Wedding</label>
<input id="element_6_5" name="element_6_5" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_6_5">Event</label>
<input id="element_6_6" name="element_6_6" class="element checkbox" type="checkbox" value="1" />
<label class="choice" for="element_6_6">Other</label>
</span>
</li>
<li id="li_5" >
<label class="description" for="element_5">Message </label>
<div>
<textarea id="element_5" name="element_5" class="element textarea medium"></textarea>
</div>
</li>

<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>


********************************************************

and here is my php


********************************************************
<?php
if(isset($_POST['email'])) {

// CHANGE THE TWO LINES BELOW
$email_to = "myemailaddress";

$email_subject = "Contact";


function died($error) {
// your error code can go here
echo "I am very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please hit the back button on your browser and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['element_1_1']) ||
!isset($_POST['element_1_2']) ||
!isset($_POST['element_2_1']) ||
!isset($_POST['element_2_2']) ||
!isset($_POST['element_2_3']) ||
!isset($_POST['element_3']) ||
!isset($_POST['element_4_1']) ||
!isset($_POST['element_4_2']) ||
!isset($_POST['element_4_3']) ||
!isset($_POST['element_4_4']) ||
!isset($_POST['element_4_5']) ||
!isset($_POST['element_4_6']) ||
!isset($_POST['element_6_1']) ||
!isset($_POST['element_6_2']) ||
!isset($_POST['element_6_3']) ||
!isset($_POST['element_6_4']) ||
!isset($_POST['element_6_5']) ||
!isset($_POST['element_6_6']) ||
!isset($_POST['element_5'])) {
died('I am sorry, but there appears to be a problem with the form you submitted.');
}

$firstname = $_POST['element_1_1']; // required
$lastname = $_POST['element_1_2']; // required
$area = $_POST['element_2_1']; // required
$fst3 = $_POST['element_2_2']; // required
$lst4 = $_POST['element_2_3']; // required
$email_from = $_POST['element_3']; // required
$streetaddress = $_POST['element_4_1']; // not required
$addressline2 = $_POST['element_4_2']; // not required
$city = $_POST['element_4_3']; // required
$state = $_POST['element_4_4']; // required
$zipcode = $_POST['element_4_5']; // required
$country = $_POST['element_4_6']; // not required
$portraits = $_POST['element_6_1']; // not required
$group = $_POST['element_6_2']; // not required
$engagement = $_POST['element_6_3']; // not required
$wedding = $_POST['element_6_4']; // not required
$event = $_POST['element_6_5']; // not required
$other = $_POST['element_6_6']; // not required
$msg = $_POST['element_5']; // not required


$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$lastname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($firstname)."\n";
$email_message .= "Last Name: ".clean_string($lastname)."\n";
$email_message .= "Area Code: ".clean_string($area)."\n";
$email_message .= "First 3: ".clean_string($fst3)."\n";
$email_message .= "Last 4: ".clean_string($lst4)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Street Address: ".clean_string($streetaddress)."\n";
$email_message .= "Street Address Line 2: ".clean_string($addressline2)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip Code: ".clean_string($zipcode)."\n";
$email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Portraits: ".clean_string($portraits)."\n";
$email_message .= "Group: ".clean_string($group)."\n";
$email_message .= "Engagement: ".clean_string($engagement)."\n";
$email_message .= "Wedding: ".clean_string($wedding)."\n";
$email_message .= "Event: ".clean_string($event)."\n";
$email_message .= "Other: ".clean_string($other)."\n";
$email_message .= "Message: ".clean_string($msg)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- place your own success html below -->

Thank you for contacting me. I will be in touch with you soon!
<br />
<ul>
<li id="hb"><a href="index.html">Return Home</li>
</ul>
<style media="screen" type="text/css">
li#hb {

list-style: none;
color: #ffffff;
height: 25pt;
width: 100pt;
text-align: center;
font-size: 12pt;
background: -moz-linear-gradient(#40e0d0, #2aa296);
background: -webkit-gradient(linear,left bottom,left top,color-stop(0, #2aa296),color-stop(1, #40e0d0));
background: -webkit-linear-gradient(#40e0d0, #2aa296);
background: -o-linear-gradient(#40e0d0, #2aa296);
background: -ms-linear-gradient(#40e0d0, #2aa296);
background: linear-gradient(#40e0d0, #2aa296);
border-radius: 2px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.65);
}
li#hb:hover > a{
color: #ffd39b;
}
</style>
<?php
}
die();
?>

********************************************************


thanks for the help
mjocon is offline   Reply With Quote
Old 07-23-2012, 06:00 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
In the future please wrap all of your code in [php][/php] tags.

I'm not sure how you are ending up with the string " (0) " as your results (although. . . maybe a cgi directive on die), but I do see how you are not processing anything:
PHP Code:
if(isset($_POST['email'])) { 
There is no input type named email, so therefore 'email' is not a valid associative index in the $_POST superglobal.

You can actually do a lot to simply this and clean up. The first is the issets. There is no reason to use multiple; the isset() function accepts a variable number of parameters: if (!isset($var1, $var2, $var3, ...)). Every one has to be valid in order for the isset() to pass.

You can also use arrays to group things if it makes sense to do so. In HTML you can use the syntax with the names as an array and retrieve them as such:
Code:
<input type="text" name="address[street]" />
<input type="text" name="address[city]" />
<input type="text" name="address[state]" />
<input type="text" name="address[postalcode]" />
<!-- . . . -->
Which is retrieve in PHP under $_POST or $_GET as:
PHP Code:
$street $_POST['address']['street']; 
And so forth. To me I'd choose the above over in-descriptive names such as element_6_4, but that is a preference since the name has 0 weight in what it actually does.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mjocon (07-23-2012)
Old 07-23-2012, 09:37 PM   PM User | #3
mjocon
New to the CF scene

 
Join Date: Jul 2012
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
mjocon is an unknown quantity at this point
Smile

thanks for the response, ill clean it up and remove the $issest values not used. I'm still trying to figure php out and your response helped. Thanks
mjocon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:48 AM.


Advertisement
Log in to turn off these ads.