Form issue(s)

Hello there,
I created a form and some php script, and after testing it a bunch of times, the form shows up, but the if commands and other code i have in the php file just aren’t working.
I’m not really sure what I am missing, because from looking at other examples online, I have pretty much everything I would want on the contact form. It’s not anything fancy…it just has required fields, some messages, and email verification too.

Could someone direct me in the right direction by a link or just some advice please?
Thanks you very much. Btw, I’m sorry in advance if there is some huge thing that’s obvious that’s missing, I’ve been learning PHP little by little.

The only problem I can think of is that in order for the echo statements to show up it would have to be on the same page with the form. But, if that is the case, what should I write in order for it to show up on the same html page?


 <form method="POST" action="mail.php">
   Name (required):<br />
<input type="text" name="name" size="19"><br>
   <br>
   E-Mail (required):<br />
<input type="text" name="email" size="19"><br>
   <br>
   Message:<br />
   <textarea rows="10" name="message" cols="40"  wrap="hard"></textarea>
   
   <br>
   <br>
   <input type="submit" value="Submit" name="submit">
</form>



<?php
if(isset($_POST['submit'])) {

$to = "MYEMAIL@cox.net";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "From:

 $name

 Email:

 $email

 Message:

 $message";
$finished = mail($to, $subject, $body);

if (empty($_POST['name']) ) {
        echo "Please enter your name";
        }
if (empty($_POST['email']) ) {
        echo "Please enter your e-mail address";
        }
if (empty($_POST['message']) ) {
        echo "Pleace enter your message";
        }
if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
        echo "Please enter a valid e-mail address";

if ($finished) {
    echo "Thank you for your message, ".$name.". <BR> You will be contacted as soon as possible at ".$email.".";
} else {
    echo "There seems to be a problem sending your message. Please try again. If this problem persists, contact support@MYDOMAIN.COM";}    

}
?>

Hello,
thank you for your reply.

But, the form still doesn’t seem to do anything, once I click ‘submit’, it goes to the php page but it stays blank.
I changed a few things and also added what you suggested, but it’s not wanting to work with me.
The IF statements look right to me…comparing them w/ others’ forms. Maybe the $finished variable is wrong perhaps? :o/


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

if(!empty($_POST)) { 

$to = "MYEMAIL@cox.net";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "From:

 $name

 Email:

 $email

 Message:

 $message";
$finished = mail($to, $body);

if(!empty($_POST)['finished']) { 
    echo "Thank you for your message, ".$name.". <BR> You will be contacted as soon as possible at ".$email.".";
} else {
    echo "There seems to be a problem sending your message. Please try again. If this problem persists, contact support@MYEMAIL";}

if (empty($_POST['name']) ) {
        echo "Please enter your name";
        }
if (empty($_POST['email']) ) {
        echo "Please enter your e-mail address";
        }
if (empty($_POST['message']) ) {
        echo "Pleace enter your message";
        }
if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
        echo "Please enter a valid e-mail address";

    

}
?> 
</body>
</html>


Hello,
wow thanks A LOT for your help.!!!

I also noticed that I didn’t close the last part with another {, lol.
It’s always the little things.

I tested the form out, and there’s something else i’d like to add.
When i dont fill out the required field, it goes to the php page, it states that i didnt fill something out, however, it still says that it was sent out successfully.

What should I do if I dont want it to send without required field?

And lastly, if I want the errors and/or succesfful messege showing up on the SAME page as the contact page, I will have to somehow combine the two scripts, right?

You need to nest your if statements that do the validation


if(//check if name was entered){
     if(//check if email was entered AND that it is valid) {
          if(//check that they enterd a message) {
               //send mail
          }
          else {
               //Mail not sent successfully error message
          }
     }
     else {
          //Invalid or blank email error message
     }
}
else {
     //Blank name error message
}

This way, it only goes on to the next step if the previous one passes validation.

[quote=djheru;2342232]You need to nest your if statements that do the validation


if(//check if name was entered){
     if(//check if email was entered AND that it is valid) {
          if(//check that they enterd a message) {
               //send mail
          }
          else {
               //Mail not sent successfully error message
          }
     }
     else {
          //Invalid or blank email error message
     }
}
else {
     //Blank name error message
}

This way, it only goes on to the next step if the previous one passes validation.[/quote]

Hello,
thanks alot man, I was trying to figure out what I was doing wrong!! I’ll try that one out, thanks a lot for your help!!