Jump to content

How do I solve this?


Cornelia

Recommended Posts

Hi,

this is my first post in this forum! :)

 

I have a problem with a mail script, and I can't solve it without help from you guys. At first, I have a dynamic form where the user can add their own input fields based on how many products they like to add to the "shopping cart" (just a mail that's going to be sent to me). To get this information, I have the following script.

{ 
  $produkt[$num] = '<strong>Artikel: </strong>'.   
$_POST['product'.$num] .'<br /><strong>Antal: </strong>'.    
$_POST['value'.$num] .'<br /><strong>Färg: </strong>'.    
$_POST['color'.$num]; 
} 

 

Now the question. How am I supposed to include this information in my mail? The rest of the information is stored in a txt-file.

Here's the script:

 

$maxtime = mktime() - ($maxnrofdaystopkeepdata * 86400);
$handle=opendir("data");
while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
            $filetime = filemtime("data/".$file);
            if ($filetime < $maxtime){
                  unlink("data/".$file);
            }
      }
}
closedir($handle);

[b]if (isset($_GET[C])){
      $C = $_GET[C];
      $S = $_GET[s];
      $file = "data/".$S.".txt";
      if (file_exists($file)){
            if ($C == "y"){
                  $fp = fopen($file,"r");
                  $contents = fread ($fp, filesize($file));
                  fclose($fp);
                  $contents = explode("%%%%",$contents);
                  $Epostadress = $contents[0];
                  $vars = explode("#$%&%$#",$contents[1]);
                  $vals = explode("#$%&%$#",$contents[2]);

                  $fp = fopen("messages/data.txt","r");
                  $message = fread ($fp, filesize("messages/data.txt"));
                  fclose($fp);

                  for ($k=0;$k<count($vars);$k++){
                        $var = $vars[$k];
                        $val = $vals[$k];
                        if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
                              $formdata .= $var.": ".$val."\n";
                              $message = str_replace("%$var%",$val,$message);
                        }
                  }
                  $message = str_replace("%formdata%",$formdata,$message);
                  $from = "From: " . $Epostadress;
                  mail($emailfordatamail, "$texts[datamailsubject]", $message, $from);
                  unlink($file);
                  header("Location: $confirmsendpage");
                  exit;[/b]
            } elseif ($C == "n"){
                  unlink($file);
                  header("Location: $confirmdeletepage");
                  exit;
            }
      }
      echo "<html>\n<head>\n<script>\n<!--\nalert('".$texts[novalidentry]."');\n";
      echo "history.go(-1);\n//-->\n</script>\n</head>\n</html>";
      exit;
} elseif (isset($_POST)){
      $Epostadress = $_POST[Epostadress];
  $Telefonnummer = $_POST[Telefonnummer];
      $compulsory = $_POST[compulsory];
      $varkeys = array_keys($_POST);
      if (!eregi ("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_.-]+$", $Epostadress)) {
            $error = $texts[emailerror];
      }

for ($num = 1; isset($_POST['product'.$num]); $num++)
{ 
  $produkt[$num] = '<strong>Artikel: </strong>'.   
$_POST['product'.$num] .'<br /><strong>Antal: </strong>'.    
$_POST['value'.$num] .'<br /><strong>Färg: </strong>'.    
$_POST['color'.$num]; 
} 
  if(!eregi ('^[-0-9]{1,20}$',$Telefonnummer)) {
  		$error = $texts[phoneerror];
		}



      if (isset($compulsory)){
            for($i=0;$i<count($varkeys);$i++){
                  $var = $varkeys[$i];
                  $val = $_POST[$var];
                  if (in_array($var, $compulsory)&&($val==""||!isset($val))){
                        $errors[]=$var;
                  }
            }
      }
      if (isset($error)||isset($errors)){
            echo "<html>\n<head>\n<script>\n<!--\nalert('";
            echo $texts[errors]."\\n";
            if (isset($error)){
                  echo "- ".$error."\\n";
            }
            if (isset($errors)){
                  echo "- ".$texts[blankfields]." ";
                  for ($j=0;$j<count($errors);$j++){
                        echo $errors[$j]." ";
                  }
            }
            echo "');\nhistory.go(-1);\n//-->\n</script>\n</head>\n</html>";
            exit;
      }

      $S = md5(uniqid(rand()));
      $confirmlink = $urlofthisfile."?C=y&S=".$S;
      $cancellink = $urlofthisfile."?C=n&S=".$S;

      $fp = fopen("messages/confirm.txt","r");
      $message = fread ($fp, filesize("messages/confirm.txt"));
      fclose($fp);

      for ($k=0;$k<count($varkeys);$k++){
            $var = $varkeys[$k];
            $val = stripslashes($_POST[$var]);
            if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
                  $formdata .= $var.": ".$val."\n";
                  $message = str_replace("%$var%",$val,$message);
            }
      }

      $message = str_replace("%confirmlink%",$confirmlink,$message);
      $message = str_replace("%cancellink%",$cancellink,$message);
      $message = str_replace("%formdata%",$formdata,$message);

      $vars = implode("#$%&%$#",$varkeys);
      $vals = implode("#$%&%$#",$_POST);
      $data = $Epostadress."%%%%".$vars."%%%%".$vals;

      $file = "data/".$S.".txt";
      $fp = fopen($file,"a");
      fputs($fp,$data);
      fclose($fp);

      $from = "From: " . $fromforconfirmmail;
      mail($Epostadress, "$texts[confirmmailsubject]", $message, $from);
      header("Location: $noticepage");
      exit;
} else {
      exit;
}
?>

Thanks in advance!

 

Link to comment
https://forums.phpfreaks.com/topic/39934-how-do-i-solve-this/
Share on other sites

Here's sample code to deal with multiple for fields, passing the fields in arrays. Note the "[]" at end of each formfield's name.

 

<?php
/***
* was data submitted
*/
$message = '';
if (isset($_POST['action'])) {
    foreach ($_POST['product'] as $k => $product) {
        if ($product != '') {
            /***
            * get other asociated field values 
            */
            $value = $_POST['value'][$k];
            $color = $_POST['color'][$k];
            /***
            * add to message
            */
            $message .= sprintf('%-20s%12s  %-12s%s', $product, $value, $color, "\r\n"); 
        }
    }
}

/***
* display message to check code
*/
echo "<pre>$message</pre>";
?>

<form method='post'>
    <table>
        <tr>
            <th>Product</th>
            <th>Value</th>
            <th>Color</th>
        </tr>
        <tr>
            <td><input type="text" name="product[]" size="12"></td>
            <td><input type="text" name="value[]" size="12"></td>
            <td><input type="text" name="color[]" size="12"></td>
        </tr>
        <tr>
            <td><input type="text" name="product[]" size="12"></td>
            <td><input type="text" name="value[]" size="12"></td>
            <td><input type="text" name="color[]" size="12"></td>
        </tr>
        <tr>
            <td><input type="text" name="product[]" size="12"></td>
            <td><input type="text" name="value[]" size="12"></td>
            <td><input type="text" name="color[]" size="12"></td>
        </tr>
        <tr>
            <td><input type="text" name="product[]" size="12"></td>
            <td><input type="text" name="value[]" size="12"></td>
            <td><input type="text" name="color[]" size="12"></td>
        </tr>
        <tr>
            <td><input type="text" name="product[]" size="12"></td>
            <td><input type="text" name="value[]" size="12"></td>
            <td><input type="text" name="color[]" size="12"></td>
        </tr>
    <table>
    <input type="submit" name="action" value="Submit">
</form> 

Link to comment
https://forums.phpfreaks.com/topic/39934-how-do-i-solve-this/#findComment-193576
Share on other sites

Hi again,

I solved it! I just had to add

<input name="compulsory[]" type="hidden" id="compulsory[]" value="product"+num+"" />

after my input fields. But, there's still a but. This is the code to write all the input entries to data.txt.

if (isset($_GET[C])){
      $C = $_GET[C];
      $S = $_GET[s];
      $file = "data/".$S.".txt";
      if (file_exists($file)){
            if ($C == "y"){
                  $fp = fopen($file,"r");
                  $contents = fread ($fp, filesize($file));
                  fclose($fp);
                  $contents = explode("%%%%",$contents);
                  $Epostadress = $contents[0];
                  $vars = explode("#$%&%$#",$contents[1]);
                  $vals = explode("#$%&%$#",$contents[2]);

                  $fp = fopen("messages/data.txt","r");
                  $message = fread ($fp, filesize("messages/data.txt"));
                  fclose($fp);

                  for ($k=0;$k<count($vars);$k++){
                        $var = $vars[$k];
                        $val = $vals[$k];
                        if ($var!="compulsory" && (strtolower($var)!="submit" && strtolower($var)!="reset")){
                              $formdata .= $var.": ".$val."\n";
                              $message = str_replace("%$var%",$val,$message);
                        }
                  }
                  $message = str_replace("%formdata%",$formdata,$message);
                  $from = "From: " . $Epostadress;
                  mail($emailfordatamail, "$texts[datamailsubject]", $message, $from);
                  unlink($file);
                  header("Location: $confirmsendpage");
                  exit;
            } elseif ($C == "n"){
                  unlink($file);
                  header("Location: $confirmdeletepage");
                  exit;
            }
      }
      echo "<html>\n<head>\n<script>\n<!--\nalert('".$texts[novalidentry]."');\n";
      echo "history.go(-1);\n//-->\n</script>\n</head>\n</html>";
      exit;
}

 

Is it possible to add a

<br />

before every "products loop"? If yes, how?

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/39934-how-do-i-solve-this/#findComment-193593
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.