Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Another PHP mailer form question

Posted on 2011-02-18
10
Medium Priority
?
317 Views
Last Modified: 2012-05-11

The snippet of code I need help with is attached. "Suite Size" is not a string, but a choice from a dropdown SELECT menu. Not sure what the proper syntax should be.




$email_message .= "First Name: ".clean_string($firstName)."\n";
	$email_message .= "Last Name: ".clean_string($lastName)."\n";
	$email_message .= "Phone: ".clean_string($phone)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Suite Size: ".clean_string($size)."\n";

Open in new window

0
Comment
Question by:tekgrl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 34927092
html select means it should have been submitted as a form

$email_message .= "Suite Size: ". clean_string($_GET['size']."\n";
0
 

Author Comment

by:tekgrl
ID: 34927213
Hi. The email went through okay, but the option I chose from the dropdown did not appear in the email body. Code for the HTML form is attached.
<form class="cmxform" id="commentForm" name="contactform" method="post" action="send-form.php" target="_parent">
	<div>
		<label for="firstName" class="blockit">First Name*</label>
		<input  type="text" name="firstName" class="required" maxlength="50" size="20">
	</div>
	<div>
		<label for="lastName" class="blockit">Last Name*</label>
		<input  type="text" name="lastName" class="required" maxlength="50" size="20">
	</div>
	<div>
		<label for="phone"  class="blockit">Phone</label>
		<input  type="text" name="phone" class="required" class="phone" maxlength="12" size="20">
	</div>
	<div>
		<label for="email" class="blockit">Email Address*</label>
		<input  type="text" name="email" class="required email" maxlength="80" size="20">
	</div>
	<div>
		<label for="size" class="blockit">Suite Size</label>
		<select name="size">
		<option value="studio">Studio</option>
		<option value="1bed">1 Bedroom</option>
		<option value="2bed">2 Bedroom</option>
		<option value="3bed">3 Bedroom</option>
		<option value="penthouse">Penthouse Suites</option>
		</select>
	</div>
	  	<input type="image" src="images/submit.gif" class="btn" value="Submit" alt="Submit">
</form>

Open in new window

0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34930032
I'm not sure as you said your form went through okay.
As this is from your PHP code:
$email_message .= "Email: ".clean_string($email_from)."\n";
And from the HTML :
<input  type="text" name="email" class="required email" maxlength="80" size="20">

>> $email_from NOT MATCH with name="email"

It should be not a problem you use clean_string($size) for the selectbox though.

Let know your PHP code for submitting the form so we can check where the problem is...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34930297
Here is a little trick that will help you understand how HTML forms work with PHP action scripts.

echo "<pre>";
var_dump($_POST);

I changed the <form> tag to cause this HTML form to post to the embedded PHP action script.  Install this and run it to see how it works.
<?php // RAY_temp_tekgrl.php
error_reporting(E_ALL);

if (!empty($_POST))
{
    echo "<pre>";
    var_dump($_POST);
    echo "</pre>";
}
?>
<form class="cmxform" id="commentForm" name="contactform" method="post">
	<div>
		<label for="firstName" class="blockit">First Name*</label>
		<input  type="text" name="firstName" class="required" maxlength="50" size="20">
	</div>
	<div>
		<label for="lastName" class="blockit">Last Name*</label>
		<input  type="text" name="lastName" class="required" maxlength="50" size="20">
	</div>
	<div>
		<label for="phone"  class="blockit">Phone</label>
		<input  type="text" name="phone" class="required" class="phone" maxlength="12" size="20">
	</div>
	<div>
		<label for="email" class="blockit">Email Address*</label>
		<input  type="text" name="email" class="required email" maxlength="80" size="20">
	</div>
	<div>
		<label for="size" class="blockit">Suite Size</label>
		<select name="size">
		<option value="studio">Studio</option>
		<option value="1bed">1 Bedroom</option>
		<option value="2bed">2 Bedroom</option>
		<option value="3bed">3 Bedroom</option>
		<option value="penthouse">Penthouse Suites</option>
		</select>
	</div>
	  	<input type="image" src="images/submit.gif" class="btn" value="Submit" alt="Submit">
</form>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34930302
This link may also be useful.
http://us.php.net/manual/en/tutorial.forms.php

Best regards, ~Ray
0
 

Author Comment

by:tekgrl
ID: 34930392
Here is my full PHP script. All the other fields print out on the email EXCEPT the Suite Size.
<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "me@me.com";
	$email_subject = "Test";
		
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
if(!isset($_POST['firstName']) || !isset($_POST['lastName']) || !isset($_POST['phone']) ||
   !isset($_POST['email'])) {
   died('We are sorry, but there appears to be a problem with the form your submitted.');            
      }
	
	$firstName = $_POST['firstName']; // required
	$lastName = $_POST['lastName']; // required
	$phone = $_POST['phone']; // required
	$email_from = $_POST['email']; // required

		
	
	$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 .= "Phone: ".clean_string($phone)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Suite Size: ".clean_string($_GET['$size']."\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);  
      header("Location: thanks.html");
}
?>

Open in new window

0
 
LVL 5

Accepted Solution

by:
onemadeye earned 2000 total points
ID: 34930429
I think simply because you dont defining the Suite Size on the POST variables.
Here is your codes modified:
<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "me@me.com";
	$email_subject = "Test";
		
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
if(!isset($_POST['firstName']) || !isset($_POST['lastName']) || !isset($_POST['phone']) ||
   !isset($_POST['email'])) {
   died('We are sorry, but there appears to be a problem with the form your submitted.');            
      }
	
	$firstName = $_POST['firstName']; // required
	$lastName = $_POST['lastName']; // required
	$phone = $_POST['phone']; // required
	$email_from = $_POST['email']; // required
	$suiteSize = $_POST['size']; 

	$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 .= "Phone: ".clean_string($phone)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Suite Size: ".clean_string($suiteSize)."\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);  
      header("Location: thanks.html");
}
?>

Open in new window

0
 

Author Comment

by:tekgrl
ID: 34930453
You are smart. That's it! Thanks.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34930521
If you add error_reporting(E_ALL)  to the top of your script, you will be able to see the notices about use of undefined variables and indexes.  The default installation of PHP suppresses the notices.  Had you had that in place here, you would have found that the code at your line 41 was wrong.  It was looking at $_GET instead of $_POST.

But that said, this script might still cause you some trouble.  Better to learn from the experts.  This book is a good place to get started.
http://www.sitepoint.com/books/phpmysql4/

Good luck with your project, ~Ray
0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 34931641
Hai,

Try to go for "PHPMAILER" which has the following things

1) Can send bulk mails
2) Can have bulk attachments
3) Can avoid spams
4) Can send mail to any type of mail ids's
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question