When I submit the form for this script, the result is the message echoed for the "else" // If the script did not run ok.
I can't figure out what is wrong, but I think it has to do with the mail() function..

if (isset($_POST['submitted'])) {

// Check for name.
if (eregi ('^[[:alnum:]\.\' \-]{2,15}$', stripslashes(trim($_POST['name'])))) {
   $n = $_POST['name'];
} else {
   $n = FALSE;
   echo '<p class="error">Please include your name.</p>';
}

// Check for email.
if (eregi ('^[[:alnum:][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes($_POST['email']))) {
   $e = $_POST['email'];
} else {
   $e = FALSE;
   echo '<p class="error">Please include a valid email address.</p>';
}

if (eregi('^[[:alnum:][a-z0-9_\.\-]{2,15}$', stripslashes(trim($_POST['subject'])))) {
	$s = $_POST['subject'];
} else {
	$s = FALSE;
	$s = 'I looked at your Portfolio...';
}

// Check for message
if (empty($_POST['message'])) {
   $m = $_POST['message'];
} else {
   $m = FALSE;
   echo '<p class="error">You did not include your message.</p>';
}

if ($n && $e && $s && $m) { // Everythings ok.

	$to = 'example@example.com';
	$body = 'From: ' . $n . '\n\n';
	$body .= $m;

	//send me the users email
	mail ($to, $_POST['subject'], $body, $_POST['email']);

	// Finish the page.
	echo '<p align="center"><strong>Thank you! </strong><br />Your email has been sent to <a href="mailto: example@example.com">example@example.com</a>.  If your message requires a reply for any questions or comments you may have had, you should recieve a response within a week. <br />Have a nice day!</p>';


} else { // If it did not run ok.
	echo '<p class="error">Your message could not sent due to a system error. I apologize for the inconvenience.</p>';
}

} // End of the main submit conditional.

    Is submitted the name of you form botton? can you post your form code?

      Here is the form:

      <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      <table width="80%" border="0" align="center" cellpadding="3" cellspacing="0">
        <tr>
        <td colspan="2"><strong>Tell Me What You Think! </strong>
      </td>
        </tr>
        <tr>
          <td align="right"> <strong>Name:</strong> </td>
          <td width="72%"><input name="name" type="text" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" size="30" maxlength="30" /></td>
        </tr>
        <tr>
          <td align="right"><strong>Email Address:</strong></td>
          <td><input name="email" type="text" id="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" size="30" maxlength="30" /></td>
        </tr>
        <tr>
          <td align="right"><strong>Subject:</strong></td>
          <td><input name="subject" type="text" id="subject" value="<?php if (isset($_POST['subject'])) echo $_POST['subject']; ?>" size="30" maxlength="30" /></td>
        </tr>
        <tr>
          <td align="right" valign="top"><strong>Message:</strong>:</td>
          <td rowspan="2"><textarea name="message" cols="45" rows="7" id="message"></textarea></td>
        </tr>
        <tr>
          <td align="right" valign="top">&nbsp;</td>
          </tr>
        <tr>
          <td colspan="2" align="center"><input name="submit" type="submit" value="Send Message" />
            <input name="submitted" type="hidden" id="submitted" value="TRUE" /></td>
          </tr>
      </table>
      
      </form>	
      	

        I always cheat and use strlen() I know this probably isnt best practice but I find it easier to do

        if(strlen('$_POST['submitted']) > 3) {
        //

        }

        isset just always confuses me or im unsure if it would not be set etc, I just find doing a string lenth function more reliable..

          Does the strlen function work the same way as isset, or give the same result? Can I replace isset with it in every situation, or are there times when isset would be better to use?

          Changing it to strlen wouldn't solve my problem I have would it?

            well strlen() returns the number of characters in a string, which is pretty definite, whereas isset() returns boolean i.e. true or false. However I find it will give me a false positive if there is an errant character or nbsp so sometimes i use strlen() because its easier for me to read for me even though I know its not the fastest or best/best practice..

              Oh ok. Well I tried changing it to strlen, but when I try submitting a test email, I get the message for if theres no message "You did not include your message."
              And I get the else message for when something didnt work "Your message could not be sent due to a system error. I apologize for the inconvenience."

              Any ideas why this is happening, because I have written something for the message, so I shouldn't be getting that else message.

                echo $_POST['submitted'] for debugging purposes and see what its sending or anything at all

                  You have 2 buttons!! one is hidden.. Why is that?? well the second isnt really a button just a hidden field

                  You know that a submit button already sends a var so you dont need a hidden field..

                  <td colspan="2" align="center"><input name="submit" type="submit" value="Send Message" />
                  <input name="submitted" type="hidden" id="submitted" value="TRUE" /></td>

                  I think should be something like

                  <input type="submit" value="Send Message" name="submitted" >

                  or do a var_dump($_POST) should show you all the post vars coming in i think...

                  miramardesign
                  http://miramardesign.com
                  I code therefore I am.

                    Ok I figured it out 😃. The problem was with the "if (empty($_POST['message']))".

                    I changed it to "(!empty(" and it works now.

                    Here is the whole script for send a basic email:

                    // check if form is submitted	
                    if (isset($_POST['submitted'])) {
                    
                    // Check for name.
                    if (eregi ('^[[:alnum:]\.\' \-]{2,15}$', stripslashes(trim($_POST['name'])))) {
                       $name = $_POST['name'];
                    } else {
                       $name = FALSE;
                       echo '<p class="error">Please include your name.</p>';
                    }
                    
                    // Check for email.
                    if (eregi ('^[[:alnum:][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes($_POST['email']))) {
                       $email = $_POST['email'];
                    } else {
                       $email = FALSE;
                       echo '<p class="error">Please include a valid email address.</p>';
                    }
                    
                    if (eregi('^[[:alnum:][a-z0-9_\.\-]{2,15}$', stripslashes(trim($_POST['subject'])))) {
                    	$subject = $_POST['subject'];
                    } else {
                    	$subject = FALSE;
                    	$subject = 'I looked at your Portfolio...';
                    }
                    
                    // Check for message
                    if (!empty($_POST['message'])) {
                       $body = htmlspecialchars($_POST['message']);
                    } else {
                       $body = FALSE;
                       echo '<p class="error">You did not include your message.</p>';
                    }
                    
                    if ($name && $email && $subject && $body) { // Everythings ok.
                    
                    	$to = 'example@example.com';
                    	$headers = "From: \"$name\"<$email>\n\n";
                    
                    	//send me the users email
                    	mail ($to, $_POST['subject'], $body, $headers);
                    
                    	// Finish the page.
                    	echo "<div style=\"position: relative; top: 20px; left: 20px; text-align: left; \">
                    	Thank you, <strong><i> $name </i></strong><br />";
                    	echo "Your message:<br />
                    		<strong> $subject </strong><br />
                    		\"<strong><font color=\"#990000\"> $body </font></strong>\"
                    		<br />
                    		Was sent successfully to: <br />
                    		<a href=\"mailto: $to\">$to </a><br /><br />
                    		</div>
                    		";
                    
                    
                    } else { // If it did not run ok.
                    	echo 'Your message could not sent due to a system error. <br />
                    	I apologize for the inconvenience.';
                    }
                    
                    } // End of the main submit conditional.
                    
                    ?>
                    
                    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                    <table width="50%" border="0" align="center" cellpadding="3" cellspacing="0">
                      <tr>
                      <td colspan="2" align="left"><strong>You can  email me using this form.</strong></td>
                      </tr>
                      <tr>
                        <td align="right"> <strong>Name:</strong> </td>
                        <td width="72%" align="left"><input name="name" type="text" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" size="25" maxlength="30" /></td>
                      </tr>
                      <tr>
                        <td align="right"><strong>Email:</strong></td>
                        <td align="left"><input name="email" type="text" id="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" size="25" maxlength="30" /></td>
                      </tr>
                      <tr>
                        <td align="right"><strong>Subject:</strong></td>
                        <td align="left"><input name="subject" type="text" id="subject" value="<?php if (isset($_POST['subject'])) echo $_POST['subject']; ?>" size="25" maxlength="30" /></td>
                      </tr>
                      <tr>
                        <td align="right" valign="top"><strong>Message:</strong>:</td>
                        <td rowspan="2"><textarea name="message" cols="40" rows="5" id="message"></textarea></td>
                      </tr>
                      <tr>
                        <td align="right" valign="top">&nbsp;</td>
                        </tr>
                      <tr>
                        <td colspan="2" align="center"><input name="submit" type="submit" value="Send Message" /> 
                          &nbsp; 
                          <input name="submitted" type="hidden" id="submitted" value="TRUE" />
                          <input name="reset" type="reset" id="reset" value="Reset" /></td>
                        </tr>
                    </table>
                    
                    </form>	
                    
                      miramardesign wrote:

                      You know that a submit button already sends a var so you dont need a hidden field..

                      Actually, I don't believe submit buttons are required to return a value as per the W3C.

                      miramardesign wrote:

                      I just find doing a string lenth function more reliable..

                      And also more prone to causing notices/warnings (I can't remember which ATM) when the variable isn't set. If you just wanted to know if they entered a value or not, see if [man]empty/man is false (this also first checks to see if the variable is set, so there's no need to worry about that as well). If you absolutely must know that they entered x number of characters or more, you should use [man]isset/man first and then [man]strlen/man..

                      if(isset($foo) && strlen($foo) > 3)
                        bradgrafelman wrote:

                        Actually, I don't believe submit buttons are required to return a value as per the W3C.

                        What do you mean by that? That its a good idea to use check the value of a hidden field like:

                        <input name="sent" type="hidden" value="TRUE" />

                        with:

                        if (isset($_POST['sent'])) {

                        This is how I learned to write it from the book by Larry Ullman "PHP and MySQL for dynamic websites"

                        I figure since it works, its good for me. I don't know if it fits with W3C though.

                          That would be the surest way to see if a form was submitted (regardless of anything else in the form or what the user entered in the form)... at least as far as I know.

                            Write a Reply...