Jump to content

PHP Mail being sent too early?


Kathy

Recommended Posts

I really need help I am at the point of pulling out my hair!! I have a php file, it is a registration form, it captures the data, sends it to the database and it is meant to send me an email with all the details that the client has filled in and send the client a confirmation email, anyway, when the script is uploaded onto the server, and tested, an email is delivered to me, empty, as an email is being sent on opening of the page! Or on refresh of the page and not on submitting of the page!! This is soooo wrong! I cannot figure it out I have spent hours and hours and hours on it, I will appreciate any help!

Here is my code:

 

<?php
function select_db(){
mysql_connect("localhost","farmer","farmer2")or die(mysql_error());
mysql_select_db("farmer2")or die(mysql_error());
}

//check to see if the user clicked on the submit button
if(isset($_POST["submit"])){
}

$initials = $_POST['initials'];
$surname = $_POST['surname'];
$company_name = $_POST['company_name'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$type = $_POST['type'];
$method = $_POST['method'];
$owner = $_POST['owner'];
$finance = $_POST['finance'];
$hr = $_POST['hr'];
$sales = $_POST['sales'];
$fm = $_POST['fm']; 
$research = $_POST['research'];
$is = $_POST['is'];
$viticulture = $_POST['viticulture'];
$production = $_POST['production'];
$retail = $_POST['retail'];
$buying = $_POST['buying'];
$distribution = $_POST['distribution'];
$student = $_POST['student'];
$other = $_POST['other'];



$JobFunction = $_POST["ch1"] . ", " . $_POST["ch2"] . ", " . $_POST["ch3"] . ", " . $_POST["ch4"] . ", " . $_POST["ch5"] . ", " . $_POST["ch6"] . ", " . $_POST["ch7"] . ", " . $_POST["ch8"] . ", " . $_POST["ch9"] . ", " . $_POST["ch10"] . ", " . $_POST["ch11"] . ", " . $_POST["ch12"] . ", " . $_POST["ch13"];
$JobFunction = str_replace(", ,","",$JobFunction);

select_db();

mysql_query("INSERT INTO contact_details (initials, surname, company_name, address, address2, address3, telephone, email, method, type, owner, finance, hr, fm, research, `is`, viticulture, production, retail, buying, distribution, student, other) VALUES ('$initials', '$surname', '$company_name', '$address', '$address2', '$address3', '$telephone', '$email', '$method', '$type', '$owner', '$finance', '$hr', '$fm', '$research', '$is', '$viticulture', '$production', '$retail', '$buying', '$distribution', '$student', '$other')") or die(mysql_error());

mysql_query("INSERT INTO users (JobFunction, DateRegistered, Email) VALUES ('$JobFunction','$DateRegistered','$Email')") or die(mysql_error());

		//execute the query
		//echo $sqlAdd;
		$UserID = mysql_insert_id();
		$Reference = 1000 + mysql_insert_id();
		$Reference = "IVIF2009/" . $Reference;
		$UpdateReference = "UPDATE users SET RefrenceNumber = '$Reference' WHERE DownloadUserID = '$UserID'";
		$DoUpdateRefrence = mysql_query($UpdateReference);

$to      = 'k@exa.co.za';
$from	 = 'k@exa.co.za';
$subject = 'Online Registration';
$message = '<body style="font-family:tahoma; font-size:12px; line-height:18px; color:#333366">'. $initials.', '.$surname.', '.$company_name.', '.$address.', '.$address2.', '.$address3.', '.$telephone.', '.$email.', '.$method.', '.$type.', '.$JobFunction.', '. $Reference .'</body>';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'FROM: '. $from . "\r\n";
mail($to, $subject, $message, $headers);


$to  = $email; 
$from = "k@exa.co.za";
$subject = 'Online Registration at Intervitis-Interfructa Southern Africa';
$message = '<body style="font-family:tahoma; font-size:12px; line-height:18px; color:#333366"><table border=0> <tr> <td><p>Hi There!</p>
  <p>Thank you for registering online. Your online registration has been confirmed. </p>
  <p>Your reference is ' . $Reference . '</p>
  <p>Please note that the exhibition takes place from 28th to 30th July 2009.To assist our staff and speed up your entry ticket retrieval, <strong> please bring this e-mail with you when you visit the show.</strong>Pre-Registration tickets may be collected at the Pre-Registration and Press Ticket Desk.</p>
  <p>We look forward to your visit!Have a Great Day!</p>
  <p><img src="http://www.intervitis-interfructa.co.za/images/logo.jpg"></p></td>
</tr> </table>
</body>';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'FROM: '. $from . "\r\n";
mail($to, $subject, $message, $headers);
}



//clear all the values
    $initials = "";
$surname = "";
$company_name = "";
$address = "";
$address2 = "";
$address3 = "";
$telephone = "";
$email = "";
$type = "";
$method = "";
$owner = "";
$finance = "";
$hr = "";
$sales = "";
$fm = "";
$research = "";
$is = "";
$viticulture = "";
$production = "";
$retail = "";
$buying = "";
$distribution = "";
$student = "";
$other = "";                                                        
    
?>
<html>
<head>


<title>Intervitis Interfructa</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=7"/>
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style7 {font-size: x-small}
.style8 {font-size: xx-small}
.style14 {font-size: x-small; font-weight: bold; }
.style15 {
font-size: 10px;
font-weight: bold;
}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function validate(form){
var errors = [];

if ( !checkRadioArray(form.method) ) {
  errors[errors.length] = "You must choose between production or supplier.";
}
if ( !checkRadioArray(form.type) ) {
  errors[errors.length] = "You must choose between wine, fruit, both or other.";
}

if (errors.length > 0) {
  reportErrors(errors);
  return false;
}

return true;
}

function checkRadioArray(radioButtons){
for (var i=0; i < radioButtons.length; i++) {
  if (radioButtons[i].checked) {
   return true;
  }
}
return false;
}

function reportErrors(errors){
var msg = "There were some problems...\n";
var numError;
for (var i = 0; i<errors.length; i++) {
  numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
}
alert(msg);
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (Template.psd) -->
<form method="post" action="reg_suc.php" onSubmit="return validate(this);">
<table id="Table_01" align="center" width="850" height="883" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td colspan="4" background="images/Template_01.gif" height="194"></td>
</tr>
<tr>
	<td colspan="2" background="images/Template_02.gif" height="45"></td>
	<td width="426" height="45" ><div align="left" class="heading">
	  <div style="color:#f26732;">Online Registration </div>
  </div></td>
	<td rowspan="8" class="ragthPanel" width="137" height="424"> </td>
</tr>
<tr>
	<td background="images/Template_05.gif" style="background-repeat:no-repeat;" width="218" height="36"> </td>
  <td colspan="2" rowspan="7" height="379" class="contentCell" valign="top" align="center">

    <div id="tabMenu" align="center"><a href="visitor_info.html">Background</a> | <a href="vi_cat.html">Categories</a> | <a href="vi_highlights.html" >Highlights</a><br/>
        <a href="vi_online_reg.php" style="border-bottom:2px solid #9ccb3d;">Online Registration</a> | <a href="vi_ex_cat.html">Exhibitor Catalogue</a>|  <a href="vi_down.php">Downloads</a> |</div>
    <br>
    <table width="100%" border="0" cellspacing="3" cellpadding="2">
          <tr>
            <td colspan="4"><div align="center" class="style15">PLEASE FILL IN THE REQUIRED FIELDS * </div></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">INITIALS/VOORLETTERS * </span></td>
            <td colspan="2">
		<input name="initials" type="text" value="<?php echo $initials; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">SURNAME/VAN * </span></td>
            <td colspan="2"><input name="surname" type="text" value="<?php echo $surname; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">COMPANY/MAATSKAPPY * </span></td>
            <td colspan="2"><input name="company_name" type="text" value="<?php echo $compnay_name; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">POSTAL ADDRESS/POSADRES * </span></td>
            <td colspan="2"><input name="address" type="text" id="address" value="<?php echo $address; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7"> CITY/STAD * </span></td>
            <td colspan="2"><input name="address2" type="text" id="address2" value="<?php echo $address2; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">CODE/KODE * </span></td>
            <td colspan="2"><input name="address3" type="text" id="address3" value="<?php echo $address3; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">(CODE) TEL NO/ * <br>
            (KODE) TEL NR. </span></td>
            <td colspan="2"><input name="telephone" type="text" id="telephone" value="<?php echo $telephone; ?>"></td>
          </tr>
          <tr>
            <td colspan="2"><span class="style7">E-MAIL/E-POS * </span></td>
            <td colspan="2"><input name="email" type="text" value="<?php echo $email; ?>"></td>
          </tr>
          <tr>
            <td colspan="4"><hr></td>
          </tr>
          <tr>
            <td colspan="4"><div align="center"><span class="style15">TICK AT LEAST ONE FIELD * </span></div></td>
          </tr>
          <tr>
            <td><span class="style7">PRODUCTION OF/PRODUKSIE VAN  </span></td>

            <td><input name="method" type="radio" value="production"></td>

            <td><span class="style7">WINE/WYN </span></td>
            <td><input name="type" type="radio" value="wine"></td>
          </tr>
          <tr>
            <td><span class="style7">SUPPLIER TO/VERSKAFFER AAN </span></td>

            <td><input name="method" type="radio" value="supplier"></td>
            <td><span class="style7">FRUIT/VRUGTE</span></td>

            <td><input name="type" type="radio" value="fruit"></td>
          </tr>
          <tr>
            <td> </td>
            <td> </td>
            <td><span class="style7">BOTH/BYDE</span></td>

            <td><input name="type" type="radio" value="both"></td>
          </tr>
          <tr>
            <td> </td>
            <td> </td>
            <td><span class="style7">OTHER/ANDER</span></td>

            <td><input name="type" type="radio" value="ander"></td>
          </tr>
          <tr>
            <td colspan="4"><hr></td>
          </tr>
          <tr>
            <td><span class="style14">JOB FUNCTION/WERKS FUNKSIE </span></td>
            <td><div align="center"></div></td>
            <td><div align="center"><span class="style15">TICK AT LEAST ONE FIELD * </span></div></td>
            <td> </td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">OWNER/MD/CHAIRMAN</span></div></td>
            <td><div align="center">

              <input name="ch1" type="checkbox" id="owner" value="owner">
            </div></td>
            <td colspan="2"><span class="style7">EIENAAR/BD/VOORSITTER</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">FINANCE/ADMINISTRATION</span></div></td>
            <td><div align="center">
              
		  <input name="ch2" type="checkbox" id="finance" value="finance">
            </div></td>
            <td colspan="2"><span class="style7">FINANSIES/ADMINISTRASIE</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">HUMAN RESOURCES </span></div></td>
            <td><div align="center">
              <input name="ch3" type="checkbox" id="hr" value="hr">
            </div></td>
            <td colspan="2"><span class="style7">MENSLIKE HULPBRONNE </span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">FARM MANAGEMENT </span></div></td>
            <td><div align="center">
              <input name="ch4" type="checkbox" id="fm" value="fm">
            </div></td>
            <td colspan="2"><span class="style7">BOERDERY BESTUUR </span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">RESEARCH/LABORATORY</span></div></td>
            <td><div align="center">
              <input name="ch5" type="checkbox" id="research" value="research">
            </div></td>
            <td colspan="2"><span class="style7">NAVORSING/LABORATORIUM</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">INFORMATION SYSTEMS </span></div></td>
            <td><div align="center">
              <input name="ch6" type="checkbox" id="is" value="is">
            </div></td>
            <td colspan="2"><span class="style7">INLIGTING STELSELS </span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">VITICULTURE</span></div></td>
            <td><div align="center">
              <input name="ch7" type="checkbox" id="viticulture" value="viticulture">
            </div></td>
            <td colspan="2"><span class="style7">WINGERDBOU</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">WINEMAKER/PRODUCTION</span></div></td>
            <td><div align="center">
              <input name="ch8" type="checkbox" id="production" value="production">
            </div></td>
            <td colspan="2"><span class="style7">WYNMAKER/PRODUKSIE</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">HOSPITALITY/RETAIL</span></div></td>
            <td><div align="center">
              <input name="ch9" type="checkbox" id="retail" value="retail">
            </div></td>
            <td colspan="2"><span class="style7">GASVRYHEIDSBEDRYF/KLEINHANDEL</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">BUYING</span></div></td>
            <td><div align="center">
              <input name="ch10" type="checkbox" id="buying" value="buying">
            </div></td>
            <td colspan="2"><span class="style7">AANKOPE</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">DISTRIBUTION/WAREHOUSING</span></div></td>
            <td><div align="center">
              <input name="ch11" type="checkbox" id="distribution" value="distribution">
            </div></td>
            <td colspan="2"><span class="style7">VERSPREIDING/BERGING</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">STUDENT</span></div></td>
            <td><div align="center">
              <input name="ch12" type="checkbox" id="student" value="student">
            </div></td>
            <td colspan="2"><span class="style7">STUDENT</span></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style7">OTHER</span></div></td>
            <td><div align="center">
              <input name="ch13" type="checkbox" id="other" value="other">
            </div></td>
            <td colspan="2"><span class="style7">ANDER</span></td>
          </tr>
         
          
          <tr>
            <td colspan="4"><center>
              <hr>
              <br>
              <input name="submit" type="submit" id="submit" onClick="MM_validateForm('initials','','R','surname','','R','company_name','','R','address','','R','address2','','R','address3','','RisNum','telephone','','R','email','','RisEmail');return document.MM_returnValue" value="Submit">
               
              <br>
              <hr>
            </center></td>
          </tr>



<tr>
            <td colspan="4"><div align="center"><span class="style8">Disclaimer: Right of admission reserved. By completing this ticket and entering the show, I agree to be contacted in a business capacity via INTERVITIS INTERFRUCTA Southern Africa database </span></div></td>
          </tr>
        </table>
        </td>
</tr>
<tr><td background="images/Template_07.gif" width="218" height="27" style="background-repeat:no-repeat;"><div class="homeLink"><a href="background.html">Home</a></div></td></tr>
<tr><td background="images/Template_08.gif" width="218" height="27" style="background-repeat:no-repeat;"><div class="menuLinks"><a href="background.html">Exhibition Details</a></div></td></tr>
<tr><td class="menuCell" width="218" height="27" style="background-repeat:no-repeat;"><div class="menuLinks"><a href="why_exhib.html">Exhibition Information</a></div></td></tr>
<tr>
	<td class="menuCell" width="218" height="27">
	     <div class="menuLinks"><a href="contact.php">Contact Us </a></div></td>
</tr>
<tr valign="top">
  <td class="menuCell" width="218" height="27">
      <div class="menuLinks"><a href="visitor_info.html" style="color:#f26732; font-weight:bold;">Visitor Information </a></div></td>
</tr>
<tr>
	<td width="218" height="100" class="laftCell"> <img src="images/spacer.gif" width="1" height="240" alt=""></td>
</tr>
<tr>
	<td colspan="4" align="center">
	 <div style="font-family:'Trebuchet MS', tahoma, Verdana, Arial; font-size:12px; padding:20px 0px 20px 0px;">© 2008 Exalon Dynamic Systems</div>
	</td>
</tr>
<tr>
	<td>
		<img src="images/spacer.gif" width="218" height="1" alt=""></td>
	<td width="128">
		<img src="images/spacer.gif" width="127" height="1" alt=""></td>
	<td>
		<img src="images/spacer.gif" width="368" height="1" alt=""></td>
	<td>
		<img src="images/spacer.gif" width="137" height="1" alt=""></td>
</tr>
</table>
</form>
<!-- End ImageReady Slices -->
</body>
</html>

 

Link to comment
Share on other sites

I also thought that it might have something to do with it, I moved the } around the code a lot and that seemed to change whether or not the mail would send at all and I had no luck with it. It just wouldn't send out any mail. Any idea what could be wrong there?

Link to comment
Share on other sites

if(isset($_POST["submit"])){
}

 

1) When you now press submit it executes nothing

2) If submit never has a value (<input type="submit">) isset() always returns false which in turn will never execute the if body therefor i recommend using:

 

if (!strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') && !empty($_POST))

 

which will first verify that their is a POST request header (and which implies something was send as the default is GET), then we verify if $_POST contains any data (which should if they send something). This latter makes sure we have something to process.

 

Note: This can be shortened to if (!empty($_POST)) I just like to double-check ;)

Link to comment
Share on other sites

Thanks for that, I inserted it and tried it out,

<?php
function select_db(){
mysql_connect("dedi536.nur4.host-h.net","winefarmer02","expofarmers2")or die(mysql_error());
mysql_select_db("winafarmers2")or die(mysql_error());
}

//check to see if the user clicked on the submit button
if (!strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') && !empty($_POST)){
}

$initials = $_POST['initials'];
$surname = $_POST['surname'];
$company_name = $_POST['company_name'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$type = $_POST['type'];
$method = $_POST['method'];
$owner = $_POST['owner'];
$finance = $_POST['finance'];
$hr = $_POST['hr'];
$sales = $_POST['sales'];
$fm = $_POST['fm']; 
$research = $_POST['research'];
$is = $_POST['is'];
$viticulture = $_POST['viticulture'];
$production = $_POST['production'];
$retail = $_POST['retail'];
$buying = $_POST['buying'];
$distribution = $_POST['distribution'];
$student = $_POST['student'];
$other = $_POST['other'];

Unfortunately, it sends the mail on refresh and on the opening of the page.. I cannot believe this..  ???

 

Link to comment
Share on other sites

Try:

 

if (!strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') && !empty($_POST)){

 

Instead of:

 

if (!strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') && !empty($_POST)){
} // this shouldn't be here but rather after your last mail() statement

 

P.S. clear your mysql_connect() data not all people who browse these forums are friendly ;)

Link to comment
Share on other sites

Short answer - the if() conditional test you are playing with is closed on the very next line by the } that is there. The remainder of the code is not contained within the if(){} structure and it is being executed every time the page is requested, not when the page is requested due to the form being submitted. Move the closing } to be after the code you want executed when the from is submitted.

Link to comment
Share on other sites

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.