Jump to content

Recommended Posts

Sorry if this is php or ajax not sure but here I go

 

I hve a contact form and I can get it to do everything I want but actually SEND the email to me

 

Heres my form

 

<div id="form" style="padding-left:10px;">
<form action="pform.php">
<div>
<p class="c3"></p>
<h2>Please fill out this contact form for EncodeCSS.com</h2>
<br />
<div><p><label class="c4">Full Name:</label> <input type="text" id="name" size="30" style="margin-left:15px;" /></p>
<p><label class="c4">Your Email:</label> <input type="text" id="email" size="30" style="margin-left:10px;" /></p>
<p><label class="c4">Your Subject:</label> <input type="text" id="subject" size="30" /></p>
<div><label class="c4">Your Message</label></div>
<textarea class="c5" onblur="this.style.borderWidth='1px'; this.style.margin='1px'; this.style.borderColor='#0099cc';" onfocus=" this.rows='13'; this.style.borderWidth='2px'; this.style.margin='0px'; this.style.borderColor='#0099cc';" id="body" cols="70" rows="8" name="body">
</textarea>
<br /><br />
  <input type="button" value="Submit" id="submit" onclick="return check_values();" /></div>
</div>
</form>
<br />
<br />
<div id="confirmation" style="display:none"></div>

<br />
<br /></div>

 

Heres my pform.php

 

<?php

  error_reporting(0);
  
include 'cform_config.php';

if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body)) {
echo $error_message;
    die();
}

$email_from = $email;
$email_subject = "Contact Form: ".stripslashes($subject);
$email_message = "Please find below a message submitted by '".stripslashes($name);
$email_message .="' on ".date("d/m/Y")." at ".date("H:i")."\n\n";
$email_message .= stripslashes($body);

$headers = 'From: '.$email_from."\r\n" .
   'Reply-To: '.$email_from."\r\n" .
   'X-Mailer: PHP/' . phpversion();

// mail($email_it_to, $email_subject, $email_message, $headers);

echo "<b>$confirmation</b>";
die();
?>

 

Heres my config

<?php

error_reporting(0);
  
  // configuration
  $page_title = "Contact Us Form";
  $email_it_to = "devwebsites@gmail.com";
  $error_message = "Please complete the form first";
  $confirmation = "Thank you, your message has been successfully sent.";
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/133779-contact-form-not-emailing/
Share on other sites

posted data (through regular form submission or through ajax) are passed to the target script either by POST or GET method.  Unless you have register globals set to ON (which you shouldn't, and if you do, turn them off), the data in your target script will take the form of $_POST['varnamehere'] or $_GET['varnamehere'], depending on what method you used (post or get). 

<div id="form" style="padding-left:10px;" method="POST">
<form action="pform.php">
<div>
<p class="c3"></p>
<h2>Please fill out this contact form for EncodeCSS.com</h2>
<br />
<div><p><label class="c4">Full Name:</label> <input name="name" type="text" id="name" size="30" style="margin-left:15px;" /></p>
<p><label class="c4">Your Email:</label> <input name="email" type="text" id="email" size="30" style="margin-left:10px;" /></p>
<p><label class="c4">Your Subject:</label> <input name="subject" type="text" id="subject" size="30" /></p>
<div><label class="c4">Your Message</label></div>
<textarea class="c5" onblur="this.style.borderWidth='1px'; this.style.margin='1px'; this.style.borderColor='#0099cc';" onfocus=" this.rows='13'; this.style.borderWidth='2px'; this.style.margin='0px'; this.style.borderColor='#0099cc';" id="body" cols="70" rows="8" name="body">
</textarea>
<br /><br />
  <input type="button" value="Submit" id="submit" onclick="return check_values();" /></div>
</div>
</form>
<br />
<br />
<div id="confirmation" style="display:none"></div>
         
<br />
<br /></div>

 

changes to your html ^^^

 

<?php

  error_reporting(0);
  
include 'cform_config.php';

if(!isset($POST['name']) || !isset($POST['email']) || !isset($POST['subject']) || !isset($POST['body'])) {
   echo $error_message;
    die();
}

   $email_from = $POST['email'];
   $email_subject = "Contact Form: ".stripslashes($POST['subject']);
   $email_message = "Please find below a message submitted by '".stripslashes($POST['name']);
   $email_message .="' on ".date("d/m/Y")." at ".date("H:i")."  ";
   $email_message .= stripslashes($POST['body']);

   $headers = 'From: '.$email_from." " .
   'Reply-To: '.$email_from." " .
   'X-Mailer: PHP/' . phpversion();

   $result = mail($email_it_to, $email_subject, $email_message, $headers);

   if($result) echo "<b>$confirmation</b>";
   else echo "didn't sent";
die();
?>

I would assume no since its giving me an error when i click the button of "Please fill out the form first"

 

Heres my js

 

var http = createRequestObject();
var areal = Math.random() + "";
var real = areal.substring(2,6);

function createRequestObject() {
var xmlhttp;
try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
  	xmlhttp=new XMLHttpRequest();
  }
return  xmlhttp;
}

function sendRequest() {
var rnd = Math.random();
var name = escape(document.getElementById("name").value);
var email = escape(document.getElementById("email").value);
var subject = escape(document.getElementById("subject").value);
var body = escape(document.getElementById("body").value);

try{
    http.open('POST',  'pform.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = handleResponse;
	http.send('name='+name+'&email='+email+'&subject='+subject+'&body='+body+'&rnd='+rnd);
}
catch(e){}
finally{}
}

function check_values() {
var valid = '';

var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var subject = document.getElementById("subject").value;
var body = document.getElementById("body").value;
if(trim(name) == "" ||
	trim(email) == "" ||
	trim(subject) == "" ||
	trim(body) == "") {
		alert("Please complete all fields");
} else {
	if(isEmail(email)) {
		document.getElementById("submit").disabled=true;
		document.getElementById("submit").value='Please Wait..';
		sendRequest();
	} else {
		alert("Email appears to be invalid.nPlease check.");
		document.getElementById("email").focus();
		document.getElementById("email").select();
	}
}
}

function handleResponse() {
try{
    if((http.readyState == 4)&&(http.status == 200)){
    	var response = http.responseText;
      document.getElementById("confirmation").innerHTML = response;
      document.getElementById("confirmation").style.display ="";
	}
  }
catch(e){}
finally{}
}

function isUndefined(a) {
   return typeof a == 'undefined';
}

function trim(a) {
return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function isEmail(a) {
   return (a.indexOf(".") > 0) && (a.indexOf("@") > 0);
}

<?php
if(!isset($POST['name']) || !isset($POST['email']) || !isset($POST['subject']) || !isset($POST['body']))
?>

Should really be:

<?php
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['body']))
?>

 

Isset is great for checking to make sure a variable was set, but will still return true in all of those cases. However empty will check to make sure something was actually filled.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.