Jump to content

Contact form issue


dmaher

Recommended Posts

I apologise for being a bit of a noob, I'm new to php. It's for a wordpress site so that doesn't always help things as trying to comprehend this and then bits of code related to wordpress make it hard to get my head around at points.

 

Basically I've been trying to add a subject field to a template contact form, I basically copied the website field code for code. However it hasn't been working, in the emails it's sending the subject comes up completely blank. What I'm not getting is that if I change:

 

$subject = $_POST['yoursubject'];

 

to

 

$subject = $_POST['website'];

 

It gets it correctly from the website field.

 

I'm not seeing where I'm going wrong...   

 

template_contact.php

 

<?php
/*
Template Name: Contact Form
*/
?>
<?php 

function checkmymail($mailadresse){
$email_flag=preg_match("!^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$!",$mailadresse);
return $email_flag;
}

if(isset($_POST['Send'])){
$error = false;
if($_POST['yourname'] != ""){$class1 = "class='ajax_valid'";}else{$class1 = "class='ajax_false'"; $error = true;}
if(checkmymail($_POST['email'])){$class2 = "class='ajax_valid'";}else{$class2 = "class='ajax_false'"; $error = true;}
if($_POST['message'] != ""){$class3 = "class='ajax_valid'";}else{$class3 = "class='ajax_false'"; $error = true;}
	$the_name = $_POST['yourname'];
	$email = $_POST['email'];
	$message = $_POST['message'];
	$website = $_POST['website'];
	$yoursubject = $_POST['yoursubject'];


if($error == false){



		$to      = $_POST['myemail'];
	$subject = $_POST['yoursubject'];
	$header  = 'MIME-Version: 1.0' . "\r\n";
	$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	$header .= 'From:'. $email . " \r\n";

	$message1 = nl2br($_POST['message']);
	$message = "New message from  $the_name <br/>
	Mail: $email<br />
	Website: $website <br />
	Subject: $yoursubject <br/>
	Message: $message1 <br />";

	mail($to,
	$subject,
	$message,
	$header); 

	$noform = true;
	}

}

get_header(); ?>
<?php get_sidebar(); ?>

<div class="content">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                 <!--post title-->
	<h2 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>

                              <!--post with more link -->
			<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>

                       <!--if you paginate pages-->
			<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>

<!--end of post and end of loop-->
  <?php endwhile; endif; ?>

         <!--edit link-->
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    

<div id="ajax_response"></div>
        <div id="ajax_form">
        <form action="<?php echo get_permalink(); ?>" method="post" class="ajax_form">
<fieldset><?php if ($noform != true){ ?><legend><span>Send us a mail</span></legend>

<p <?php echo $class1?> ><input name="yourname" class="text_input empty" type="text" id="name" size="20" value='<?php echo $the_name?>'/><label for="name">Your Name*</label>
</p>
<p <?php echo $class2?> ><input name="email" class="text_input email" type="text" id="email" size="20" value='<?php echo $email?>' /><label for="email">E-Mail*</label></p>
<p><input name="website" class="text_input" type="text" id="website" size="20" value="<?php echo $website?>"/><label for="website">Website</label></p>
<p><input name="yoursubject" class="text_input" type="text" id="yoursubject" size="20" value="<?php echo $yoursubject?>"/><label for="yoursubject">Subject</label></p>
<label for="message" class="blocklabel">Your Message*</label>
<p <?php echo $class3 ?>><textarea name="message" class="text_area empty" cols="40" rows="7" id="message" ><?php echo $message ?></textarea></p>


<p>
<input type="hidden" id="myemail" name="myemail" value="[email protected]" />
<input type="hidden" id="myblogname" name="myblogname" value="<?php echo get_option('blogname'); ?>" />

<input name="Send" type="submit" value="Send" class="button" id="send" size="16"/></p>
<?php } else { ?> 
<p><h3>Your message has been sent!</h3> Thank you!</p>

<?php } ?>
</fieldset>

</form> 
</div>


</div><!-- end content-->
<?php get_footer(); ?>

 

send.php

 

<?php 

if(isset($_POST['Send'])){

	$the_name = $_POST['yourname'];
	$email = $_POST['email'];
	$website = $_POST['website'];
	$yoursubject = $_POST['yoursubject'];
		$to      =  $_POST['myemail'];
	$yoursubject = $_POST['yoursubject'];
	$subject = $_POST['yoursubject'];
	$header  = 'MIME-Version: 1.0' . "\r\n";
	$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	$header .= 'From:'. $_POST['email']  . " \r\n";

	$message1 = nl2br($_POST['message']);
	$message = "New message from  $the_name <br/>
	Mail: $email<br />
	Website: $website <br /><br />
	Subject: $yoursubject <br/><br/>
	Message: $message1 <br />";

	mail($to,
	$subject,
	$message,
	$header); 

	echo"<p><h3>Your message has been sent!</h3> Thank you!</p>";
}
?>

 

I have a very basic understanding, but as it seems it ends at actually being able to alter anything, I know the copy and alter approach is sloppy at best. I've had it working with strings and other fields, just not this new one I've made. Which makes me think I may have not declared something or my code for the new field is wrong...

 

In playing around with $subject and trying different strings, the code in the template seems a bit redundant when it was the string for $subject in send.php that was used as opposed to the string for $subject in the template... why is that ?

Link to comment
https://forums.phpfreaks.com/topic/181964-contact-form-issue/
Share on other sites

See if this helps.  The issue I can see is you have repeated variables.  I also don't understand why you have a $message and a $message1, seems to me you could make it one variable instead of two.

 

Anyway, hope this helps.

 

if($_POST['message'] != ""){$class3 = "class='ajax_valid'";}else{$class3 = "class='ajax_false'"; $error = true;}

$the_name = $_POST['yourname'];

$email = $_POST['email'];

$message = $_POST['message'];

$website = $_POST['website'];

$subject = $_POST['yoursubject'];

 

$message = "New message from  $the_name <br/>

Mail: $email<br />

Website: $website <br />

Subject: $subject <br/>

Message: $message1 <br />";

 

 

if(isset($_POST['Send'])){

 

$the_name = $_POST['yourname'];

$email = $_POST['email'];

$website = $_POST['website'];

$subject = $_POST['yoursubject'];

$to      =  $_POST['myemail'];

$yoursubject = $_POST['yoursubject'];  //REMOVE THIS LINE

$subject = $_POST['yoursubject'];  //REMOVE THIS LINE

$header  = 'MIME-Version: 1.0' . "\r\n";

$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";

$header .= 'From:'. $_POST['email']  . " \r\n";

 

$message1 = nl2br($_POST['message']);

$message = "New message from  $the_name <br/>

Mail: $email<br />

Website: $website <br /><br />

Subject: $subject <br/><br/>

Message: $message1 <br />";

 

mail($to, $subject, $message, $header);

 

echo"<p><h3>Your message has been sent!</h3> Thank you!</p>"; } ?>

Link to comment
https://forums.phpfreaks.com/topic/181964-contact-form-issue/#findComment-959907
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.