Jump to content

Need to exclude blank forms into mail


tommhans

Recommended Posts

Hi there. I'm making a simple shopping cart that sends information to a mail, and also sends a confirmation mail back to the user submitting the form(a part which i also need help with). I use dropdown menues for the quantity of the products, so that the user would not order over the quantity that we have in store. So if there are any blank forms i want them to be excluded from the mail. I have over 70 products(they're called varer in this code) so it is a huge file, if you want to see how the page looks right now take a look here (yes its not that pretty yet, im working on that aswell)

Heres the phpcode:

 

<?php
$page = $_GET['page'];
function spamcheck($field)
 {
 //filter_var() sanitizes the e-mail
 //address using FILTER_SANITIZE_EMAIL
 $field=filter_var($field, FILTER_SANITIZE_EMAIL);
 //filter_var() validates the e-mail
 //address using FILTER_VALIDATE_EMAIL
 if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
 else
    {
    return FALSE;
    }
 }
 function sjekk_kontakt_data($a) {
 /*
  *			  Sjekker at alle felt som skal være fylt ut, er det.
  */
 if ((strlen($a['check']) <= 0) || (strlen($a['email']) <= 0) ||  (strlen($a['body']) <= 0))  {
    sett_error_melding(hent_linje(7, $SYSTEM_LANG_FIL));
						    return false;
 }
 /*
  *			  Sjekker at E-post adressen er riktig.
  */
 if (!gyldig_epost($a['email'])) {
    sett_error_melding(hent_linje(18, $SYSTEM_LANG_FIL));
						    return false;
 }
 return true;
}
function gyldig_epost($adresse) {
 if (ereg("^[a-zA-Z0-9.\-\_\-]+@[a-zA-Z0-9\-]+\.[a-zA-z0-9\-\.]+$", $adresse)) {
    return true;
 } else {
    return false;
 }
}
if (isset($_POST['email']))
 {//if "email" is filled out, proceed
 //check if the email address is invalid
 $mailcheck = spamcheck($_POST['email']);
 if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    } //spamcheck
 else
    {//send email
 //Spamcheck
//this is the antall varer part
//pilotjakke grå//
$vare1 = $_POST['prod1'];
$vare2 = $_POST['prod2'];
$vare3 = $_POST['prod3'];
$vare4 = $_POST['prod4'];
$vare5 = $_POST['prod5'];
$pilotjakkegrå=
"\n Pilotjakke pelsforet Grå Small: $vare1\r\n
Pilotjakke pelsforet Grå Medium: $vare2\r\n
Pilotjakke pelsforet Grå Large: $vare3\r\n
Pilotjakke pelsforet Grå X-Large: $vare4\r\n
Pilotjakke pelsforet Grå XX-Large: $vare5\r\n
";
//Allværsjakke orrange/gul
$vare01 = $_POST['test1'];
$vare07 = $_POST['test2'];
$vare08 = $_POST['test3'];
$vare09 = $_POST['test4'];
$vare00 = $_POST['test5'];
$allværorgul=
"\n Allværsjakke kombi oransj/gul Small: $vare01\r\n
Allværsjakke kombi oransj/gul Medium: $vare02\r\n
Allværsjakke kombi oransj/gul Large: $vare03\r\n
Allværsjakke kombi oransj/gul X-Large: $vare04\r\n
Allværsjakke kombi oransj/gul XX-Large: $vare05\r\n
";
//pilotjakke blå
$vare6 = $_POST['sumpbb1'];
$vare7 = $_POST['sumpbb2'];
$pilotjakkeblå=
"\n Pilotjakke pelsforet Blå Small: $vare6\r\n
Pilotjakke pelsforet Blå Medium: $vare7\r\n
";
//vinterkjeledresser
$vare8 = $_POST['vint1'];
$vare9 = $_POST['vint12'];
$vare10 = $_POST['vint2'];
$vare11 = $_POST['vint3'];
$vare12 = $_POST['vint4'];
$vare13 = $_POST['vint5'];
$vare14 = $_POST['vint6'];
$vare15 = $_POST['vint7'];
$vare16 = $_POST['vint8'];
$vinterkjeledress=
"\n Vinterkjeledress Blåklæder Str:46 : $vare8\r\n
Vinterkjeledress Blåklæder Str:48: $vare9\r\n
Vinterkjeledress Blåklæder Str:50: $vare10\r\n
Vinterkjeledress Blåklæder Str:52: $vare11\r\n
Vinterkjeledress Blåklæder Str:54: $vare12\r\n
Vinterkjeledress Blåklæder Str:56: $vare13\r\n
Vinterkjeledress Blåklæder Str:58: $vare14\r\n
Vinterkjeledress Blåklæder Str:60: $vare15\r\n
Vinterkjeledress Blåklæder Str:62: $vare16\r\n
";
//pilotjakke univern rød
$vare21 = $_POST['univrød1'];
$vare22 = $_POST['univrød2'];
$vare23 = $_POST['univrød3'];
$jakkeunivrød=
"\n Pilotjakke Univern Rød Small: $vare21\r\n
Pilotjakke Univern Rød Medium: $vare22\r\n
Pilotjakke Univern Rød X-Large: $vare23\r\n
";
//protec selebukse svart
$vare24 = $_POST['selebukse1'];
$vare25 = $_POST['selebukse2'];
$vare26 = $_POST['selebukse3'];
$vare27 = $_POST['selebukse4'];
$selebukse=
"\n Protec Combi Vinterbukse m/seler Medium: $vare24\r\n
Protec Combi Vinterbukse m/seler Large: $vare25\r\n
Protec Combi Vinterbukse m/seler X-Large: $vare26\r\n
Protec Combi Vinterbukse m/seler XX-Large: $vare27\r\n
";
//vinterjakke
$vare28 = $_POST['jakkevint1'];
$vare29 = $_POST['jakkevint2'];
$vare30 = $_POST['jakkevint3'];
$vare31 = $_POST['jakkevint4'];
$vare32 = $_POST['jakkevint5'];
$vare33 = $_POST['jakkevint6'];
$provinterjakke=
"\n Protec Vinterjakke Svart/Orange Small: $vare28\r\n
Protec Vinterjakke Svart/Orange Medium: $vare29\r\n
Protec Vinterjakke Svart/Orange Large: $vare30\r\n
Protec Vinterjakke Svart/Orange X-Large: $vare31\r\n
Protec Vinterjakke Svart/Orange XX-Large: $vare32\r\n
Protec Vinterjakke Svart/Grå XX-Large: $vare33\r\n
";
"; //varer om til php
$varer="
Pilotjakke pelsforet Grå: \r\n
$pilotjakkegrå \r\n\r\n
Allværsjakke kombi oransj/gul: \r\n
$allværorgul \r\n\r\n
Pilotjakke pelsforet Blå: \r\n
$pilotjakkeblå \r\n\r\n
Vinterkjeledress Blåklæder: \r\n
$vinterkjeledress \r\n\r\n
Pilotjakke Univern Rød: \r\n
$jakkeunivrød \r\n\r\n
Protec Combi Vinterbukse m/seler: \r\n
$selebukse \r\n\r\n
Protec Combi Vinterbukse m/seler: \r\n
$selebukse \r\n\r\n
Protec Vinterjakke Svart/Orange: \r\n
$provinterjakke \r\n\r\n
Lofoten Vinterdress Rød: \r\n
$lofoten \r\n\r\n
Pilotjakke pelsforet Grå/Rød: \r\n
$pilotgrårød \r\n\r\n
Allværsjakke kombi gul/blå: \r\n
$allgulblå \r\n\r\n
Allværsbukse kombi oransj/gul: \r\n
$allbukseorgul \r\n\r\n
Protec Allværsbukse svart: \r\n
$probukse \r\n\r\n
Vinterjakke Rød Univern: \r\n
$vintrød \r\n\r\n
Fleecejakke EN471 Orange: \r\n
$fleece \r\n\r\n
Thermo Vest: \r\n
$thermo \r\n\r\n
"; //varer
$order = $_POST['order_total'];
$to = $_POST['email'];
$to = "tommy@premiere-produkter.no";
$subject = "Lagersalg Bestilling";
$subject ='=?UTF-8?B?'.base64_encode($subject).'?=';
$antall = $_POST['antall'];
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$email2 = $_POST['email2'];;
$message = $_POST['message'];
$name_field = $_POST['name'] ;
$tlf = $_POST['tlf'] ;
$firma = $_POST['firma'] ;
$header  = "MIME-Version: 1.0";
$header .= "Content-type: text/plain; charset=utf-8";
$header .= "From: \"$name_field\" <$email_field>";
$header .= "Reply-To: \"$name_field\" <$email_field>";
$header .= 'BCC: tommy@premiere-produkter.no';
$header .= 'CC: tommy@premiere-produkter.no';
$header .= "Return-Path: \"$name_field\" <$email_field>";	   // these two to set reply address
$header .= "Message-ID:<".$now." TheSystem@".$_SERVER['SERVER_NAME'].">";
$header .= "X-Mailer: PHP v".phpversion();				 // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$mime_boundary=md5(time());
$body = "
===========================================
$varer
===========================================
\r\n\r\n Navn: $name_field\r\n
===========================================
E-Mail: $email_field\r\n
===========================================
Firma: $firma\r\n
===========================================
Telefon: $tlf\r\n
===========================================
Message: $message\r\n\r\n
===========================================
";

if (mail($to,$subject, $body, $header)) {
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
} else {
}
    }
 }
else //send mail og slikt
?>

 

 

 

And here's an extract of the html form

 

<form id="form2" method="POST" action="index.php" name="form">
<div id="contact_box">
			 <h2>   Total pris for alle varene du har valgt ut:
<input name="totalsum" onchange="updatesum()" readonly style="border:0px;"></h2>				 
<h2><INPUT TYPE=RESET VALUE="Resett formen"></h2>
<h4><b>Registrering, MÅ UTFØRES før du bestiller</b></h4>
    <p>
<label for="name">Navn:    </label>
<input type="text" name="name" size="19" placeholder="Ditt fulle navn?">
</p>
<p>
<label for="firma">Firma:    </label>
<input type="text" name="firma" size="19" placeholder="Hvilket firma er du fra?">
</p>
<p>
<label for="email">E-Post:  </label>
<input type="text" name="email" size="19" placeholder="Hva er e-posten adressen di?">
</p>
<p>
<label for="tlf">Telefon:  </label>
<input type="text" name="tlf" size="19" placeholder="Hvor mange vil du ha med deg?">
</p>

<label for="message">Melding:  </label>
<input type="text" name="message" size="39" id="message" placeholder="Noe mer du vil tilføye?">
<p></p>
<input type="hidden" name="email2" value="tommy@premiere-produkter.no">
<input type="submit" class="submit2" size="19" value="Bestill orderen" name="submit">
Du vil motta en bekreftelse på din varebestilling og deretter motta en faktura når vi har fått gått igjennom orderen.
	   </div> <!--email form-->
<div id="content">

		    <p><div id="imgcontent">
			 <a href="#" id="example3-show" class="showLink" onclick=  "showHide('example3');return false;"><img src="style/img/70251.png" /></a><br></p>
<div id="example3" class="more">
<p><b>Pilotjakke pelsforet</b></p>
<p>70% polyester / 30% bomull</p>
<p>To brystlommer med glidelås, telefonlomme, innerlomme,
to frontlommer med knapp og armlomme med glidelås.</p>
<p>Leveres i følgende farger: Grå</p>
<p>Leveres i str: <b>S-XXL</b></p>
<p>Pris per stk <b>kr 349</b></p>
<br />
<label for="prod1">Pilotjakke pelsforet, <b>Small</b>:    </label>
<select name="prod1" value="antall" placeholder="Antallet du vil bestille?" onchange="updatesum()">
<option value""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
</SELECT> <!--Small-->
<label for="prod2">Pilotjakke pelsforet, <b>Medium</b>:    </label>
<select name="prod2" value="antall" placeholder="Antallet du vil bestille?" onchange="updatesum()">
<option value""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
</SELECT> <!--Medium-->
<label for="prod3">Pilotjakke pelsforet,<b>Large</b>:    </label>
<select name="prod3" value="antall" placeholder="Antallet du vil bestille?" onchange="updatesum()">
<option value""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
</SELECT> <!--Large-->
<label for="prod4">Pilotjakke pelsforet,<b>X-Large</b>:    </label>
<select name="prod4" value="antall" placeholder="Antallet du vil bestille?" onchange="updatesum()">
<option value""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
</SELECT> <!--XXL pilojakkepelsfor-->
<label for="prod5">Pilotjakke pelsforet,<b>XXL</b>:    </label>
<select name="prod5" value="antall" placeholder="Antallet du vil bestille?" onchange="updatesum()">
<option value""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
</SELECT><!--XXL pilojakkepelsfor-->
<br />
</p>
 Total sum på produktet: kr
<input name="smu" onchange="updatesum()" readonly style="border:0px;">
    <p><a href="#" id="example3-hide" class="hideLink"
    onclick="showHide('example3');return false;">Gjem denne infoen</a></p>
    </div>
		    <p><h2>Pilotjakke pelsforet</h2></p>


		    </div> <!--This is how every form looks like, same setup just different names and quantity-->


</form>

 

 

Cheers,

 

Tommy

Link to comment
Share on other sites

Very sleepy, and running solely on Battery now, so I'll just make this short and simple:

  • Your spamcheck () function is not aptly named, it should have been validateEmail () or similar.
  • Your gyldig_epost () function is outdated, and superseded by the spamcheck () function anyway. Not that it's used in your script, because...
  • The sjekk_kontakt_data () is not used in your script, so you're not doing any verification that there has been any content posted. You need to update this function, delete the above function, and then call it.
  • Instead of checking for success, and then nesting your code in ever increasing levels, you should check for error conditions instead. Then, if errors are found, immediately add the error message to the output, and stop the script form parsing any further.
    That means, that instead of [ci]if (isset ($_POST['email'])) { //Do stuff; } else { // Show error }[/ic] you should do it like this:
    // If we don't have an e-mail filled out, stop parsing and return to the calling code.
    if (!isset ($_POST['email'])) {
    // Show error
    return;
    }
    
    // Do stuff.
    


     
    This will make your code a lot easier to maintain, for multiple reasons, and thus save you a lot of headaches in the future.

  • There is no validation of the input that goes into the e-mail headers, which means you're open against attacks.
  • You have the following line duplicated in your code, only 5 lines apart.
    $name_field = $_POST['name'];


  • Never accept user-submitted data as your total, or the price. Contain that to your server only, and rather calculate the total upon submission. It's trivial to change the order total, to say whatever one desires.

 

Then to your main question:

You don't conditionally ask for the fields taht may, or may not, be empty. Insntead your code assumes that they all contains nvalues of interest and thus should be added to the e-mail. The first step to fixing it to only employ the fields with values, is to make change the name of the input elements a bit. Make them into arrays, instead of sequentially named strings. So instead of prod1, prod2 and so forth, name them prod[1], prod[2] and so forth.

Preferably, use arrays to hold the key, value and the text string. That way you can use the arrays to dynamically generate the select-boxes, and to validate the user-submitted data.

 

Once you have the input fields named as arrays, it's easy to loop through them using foreach ()]. In this loop do whatever validation is necessary and appropriate, before you build up the e-mail string line by line.

Make sure you skip empty rows, or rows with a value of 0.

Link to comment
Share on other sites

Hey, thanks for the reply, i've done those changes now, i am not quite sure how i should go on about doing that foreach(i called every select input now for prod[1], prod[2] and so on )

Also when it generates the input form i need it to be the correct name for the product and size, for example, prod[1] is "pilotjakker grey small", so i cant just generate the quantity selected.

And is there a way to send email to the user after submitting and to my own email? i cant seem to make it send to both :(

Link to comment
Share on other sites

i saw someone use break 2 in a foreach loop and tried to see what it did but yeah i removed it shortly after posting here. That var dump shows me that the arrays work , but i can't seem to get it in my mail. I tried to make that foreach loop so that if it is greater than 0 it should post it to check_msg, but if its lower than 0 i dont have anything happening, should there be some sort of command for that?

Link to comment
Share on other sites

god this was annoying, just can't get it to work :( have tried different stuff with the foreach(in this test file im testing it on now im just using prod[] without any numbers inside the [] , works when i have used foreach on checkboxes before...)

 

$body =" $check_msg";
if (is_array($value))
{
foreach($_POST['prod'] as $value)
{

$check_msg .= "$value" ;

  {

echo print_r ($value);
  }
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
}
}

Link to comment
Share on other sites

Why do you have this line in your code?

if (is_array($value))

 

It seems as if you're just trying things, more or less randomly, in order to just make it work. Without giving too much thought into why you're adding the things you do, or exactly what they do. If you take a step back, and give yourself time to think thoroughly through what it is that you want to do, what you have available to work with, and what steps are necessary to do it, you should be able to figure this out quite easily.

 

Programming isn't writing code to solve a problem, it's writing code to automate the solution. So unless you know how to solve the problem before you start writing code, you're going to have problems.

After all, "hastverk er lastverk". ;)

Link to comment
Share on other sites

i got an error message saying that the foreach command had an invalid argument , so i googled around and stacked overflow and i found someone use a similar line in different occasions.

 

Yeah its probably true with "hasverk is lastverk" but still i was close until i decided to do this exclude forms in the email, but yeah i feel i have to make it now that ive made this effort ^^

 

but yeah i guess it all comes down to this foreach output, just doesnt seem to echo,print nor do anything, while the var_dump clearly shows the variables work.

Might be where i place this command in the php part of the script aswell.

Link to comment
Share on other sites

Since this is just at a stall i think ill just get on to the other problem i have, where i want to send a mail to both me and the user submitting the form, how would you add that line? i tried both:

 

$to = "tommy@premiere-produkter.no";
$to =.$_POST['mail'];



//and i also tried



$to = "tommy@premiere-produkter.no" . $_POST['mail'];

 

Only one of those mails worked, or in that last case none of the mails worked.

Edited by tommhans
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.