Help!!

So the problem is this;

I have a form with about 80 text inputs. And i would like to post forward only the fields that have been filled (given a value). As there are soooo many input fields, i really dont want to get the list of all id´s without a value to my e-mail. My script is following;

<?php
  extract($_POST);
  $message = "Ohesta löydät web-palvelun kautta tekemäsi tilauksen. "."\n";
      $message .= 'Asiakasnumero: ';
    $message .= $_SESSION["username"]."\n";
	foreach($_POST as $key => $val)
    $message .= "$key: $val\n";
  $sposti = $_POST['e-mail'];
  mail( "example@example.com", "Tilaus",
	  $message, "From: $sposti" );

?>

Now I guess somekind of "if" script is needed, but i havent got anything working so far...!?

Thanks!

    You need to check if $val has any value. You can for instance use the empty() function on $val to check if it contains a value other than, 0, '0', null, '' etc.

      foreach($_POST as $key => $val)
      {
         $val = trim($val);
         if($val !== '')
         {
            $message .= "$key: $val\n";
         }
      }
      

      PS: Do you really need that extract($_POST) you have in your code? If you're not using it, I'd get rid of it as it could potentially allow malicious users to overwrite existing variables; or else if you are going to use it, you should use the EXTR_SKIP constant for the optional second parameter in order to avoid such overwriting of existing variables.

        Thanks NogDog.. That works and no, i dont use extract($_POST) anymore, so its taken away...

        Then another noobie-question... =)

        As my form has a table with 3 cols and several rows and so each row has 3 input fields, which are like "number, name, and qty". Now the post that is sent to my email has all the data one below the other. Is there any way to have it formed somehow like it is in the table?

        And if so, then even better if the id´s werent shown at all in the mail. Is that possible?

        Hope you understood the point... =)

          All things (almost) are possible. You could set up the mail to be sent as HTML (see the [man]mail/man page for info on setting the appropriate MIME type and such), then generate a HTML table from the submitted data much as you might for a web page with multiple rows of data from your database.

            Write a Reply...