Hey people! Very happy to join Daniweb :)

I'm having trouble retrieving form data via php thereby sending it to email. My code is as below. I've listed entire code though problem is only in declaring the selectlist value for POST processing. I've highlighted troubling part in red color. There something wrong there, which i couldn't figure out. I'm able to retrieve all the form fields except selectlist.

Will really appreciate it if anybody can help out on this.

Thanks in advance,
Lost Monk


//If the form is submitted
if(isset($_POST['submitted'])) {

	//Check to see if the honeypot captcha field was filled in
	if(trim($_POST['checking']) !== '') {
		$captchaError = true;
	} else {
		//Check to make sure that the name field is not empty
		if(trim($_POST['contactName']) === '') {
			$nameError = 'You forgot to enter your name.';
			$hasError = true;
		} else {
			$name = trim($_POST['contactName']);
		//Check to make sure sure that a valid email address is submitted
		if(trim($_POST['email']) === '')  {
			$emailError = 'You forgot to enter your email address.';
			$hasError = true;
		} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
			$emailError = 'You entered an invalid email address.';
			$hasError = true;
		} else {
			$email = trim($_POST['email']);
		//Check to make sure that the website field is not empty
		if(trim($_POST['website']) === '') {
			$websiteError = 'You forgot to enter your website.';
			$hasError = true;
		} else {
			$website = trim($_POST['website']);
		//Check to make sure message were entered	
		if(trim($_POST['message']) === '') {
			$commentError = 'You forgot to enter your message.';
			$hasError = true;
		} else {
			if(function_exists('stripslashes')) {
				$message = stripslashes(trim($_POST['message']));
			} else {
				$message = trim($_POST['message']);
		//If there is no error, send the email
		if(!isset($hasError)) {

			$emailTo = get_option ('mtheme_contactemail');
			$subject = 'Contact Form Submission from '.$name;
			$sendCopy = trim($_POST['sendCopy']);
			$body = "Name: $name \n\nEmail: $email \n\nWebsite: $website \n\nInterested in: $interestedin \n\nComments: $message";
			$headers = 'From: sitemail <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
			mail($emailTo, $subject, $body, $headers);

			$emailSent = true;

} ?>

wp_enqueue_script( 'contactform', get_bloginfo('template_directory') . '/js/contact-form.js', array('jquery') , '' );

<?php get_header(); ?>

	<div id="categorytitle">
	<?php the_title(); ?>

	<div class="page-contents" id="contents">

<?php if(isset($emailSent) && $emailSent == true) { ?>

	<div class="contactform-thanks">
		<h1>Thanks, <?=$name;?></h1>
		<p>Your email was successfully sent. I will be in touch soon.</p>

<?php } else { ?>

	<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>

		<?php if(isset($hasError) || isset($captchaError)) { ?>
		<p class="contactform-error">There was an error submitting the form.<p>
		<?php } ?>
		<div class="contactinfo">

			<?php the_content(); ?>

		<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
			<ol class="forms">
				<li><label for="contactName">Name</label></li>
				<li class="inputbar">
					<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
					<?php if($nameError != '') { ?>
						<span class="error"><?=$nameError;?></span> 
					<?php } ?>
				<li><label for="email">Email</label></li>
				<li class="inputbar">
					<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" />
					<?php if($emailError != '') { ?>
						<span class="error"><?=$emailError;?></span>
					<?php } ?>
                <li><label for="website">Website (if any)</label></li>
				<li class="inputbar">
					<input type="text" name="website" id="website" value="<?php if(isset($_POST['website'])) echo $_POST['website'];?>" class="requiredField" />
					<?php if($websiteError != '') { ?>
						<span class="error"><?=$websiteError;?></span> 
					<?php } ?>
                <li><label for="interestedin" class="" id="interestedin">Interested in</label></li>
                <li class="inputbar"><select name="I'm interestd in" value="<?php if(isset($_POST['interestedin'])) echo "selectedOption";?>">
  <option value="choice1">choice1</option>
  <option value="choice2">choice2</option>
  <option value="choice3'">choice3</option>
				<li class="textarea"><label for="commentsText">Message</label></li>
				<li class="inputbar">
					<textarea name="message" id="commentsText" rows="20" cols="30" class="requiredField"><?php if(isset($_POST['message'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['message']); } else { echo $_POST['message']; } } ?></textarea>
					<?php if($commentError != '') { ?>
						<span class="error"><?=$commentError;?></span> 
					<?php } ?>
				<li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">Send</button></li>

Recommended Answers

All 3 Replies

two problems, you are never pulling 'interestedin' because there is no field on your form named that:
change this line:

<li class="inputbar"><select name="I'm interestd in" value="<?php 
// to this, changing the name of your select box.
<li class="inputbar"><select name="interestedin" value="<?php 
// and then in the top code when you are processing:
//Check to make sure that the interested field is not empty
if(trim($_POST['interestedin']) === '') {
	$interestedError = 'You forgot to enter what your interested in.';
	$hasError = true;
} else {
	$interestedin = trim($_POST['interestedin']);

now $interestedin should be set and available to your mail function.
I did not find where you were calling your $emailError / $websiteError, but you can also check for $interestedError as well.

Wow! Thank you very much. How could I miss that... duh! silly me :$

And also thanks for validation part. Now my form works smooth :)

if you are error free, please close the ticket, if you are having other issues, advise of what those are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.