Link to home
Create AccountLog in
Avatar of imagekrazy
imagekrazy

asked on

need help with my HTML form and php validation form

I made another simple html form and  a validation php form, but my validation php form  will still send me a  blank information to my  email if there is  nothing in the fields,How do i  stop receiving a empty out put of my fields in my html form.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php //
error_reporting(E_ALL);

// SEND MAIL FROM A FORM
echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";
echo $_POST["message"]."</p>";

//echo "<p>Your state is: <b>".$_POST["state"]."</b>.</p>";
// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";
//$to[]  = "Her@Your.org";
//$to[]  = "Him@Your.org";

// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
//    $state      = clean_string($_POST["state"]);
      $message   = clean_string($_POST["message"]);
//      $group1     = clean_string($_POST["group1"]);
      
      
    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query from someone named $name \n\n";
//    $content   .= "state: $state\n";
    $content   .= "Email: $email\n";
//      $content   .= "Choose one: $group1\n";
    $content   .= "Message None: $message\n";
      
    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} // END OF PHP - PUT UP THE FORM
?>
</body>

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I would use a counter with the POST fields.  If it was still 0 when you were done, you had no content and nothing to email.  This is one way.
$cntr = 0;
// CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
$email      = clean_string($_POST["email"]);
$cntr += strlen($email);  // add length of variable
$name       = clean_string($_POST["name"]);
$cntr += strlen($name);  // add length of variable
....

Open in new window

Avatar of imagekrazy
imagekrazy

ASKER

I did  what you said , but it came up with this,
Notice: Undefined variable: cntr in /home/content/c/i/r/circleimage/html/myvebsite/HHL/HHLform.php on line 43
MAIL WORKED FOR circleimage@gmail.com


 What  i want is ,   the user  to submit it and if he or she leaves something blank it spits out,  to them " thank you but you forgot to fill in the everything"
I just showed how to get something you could check, not how to do the whole thing.  I thought you'd know how to do the rest.  And I don't where line 43 is or why 'cntr' is 'undefined'.
i already did the php validation form,  i don't know how to write  the code for telling the  the user he or she forgot to fill in the blank fields
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks for being helpful, but its giving me an error, this is my code;

<?php //
error_reporting(E_ALL);

// SEND MAIL FROM A FORM
// echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
//echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";
//echo $_POST["message"]."</p>";

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
      $cntr += strlen($email);  // add length of variable
    $name       = clean_string($_POST["name"]);
      $cntr += strlen($name);  // add length of variable
      

      $message   = clean_string($_POST["message"]);
      $cntr += strlen($message);  // add length of variable

      
      
    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query from someone named $name \n\n";

    $content   .= "Email: $email\n";

    $content   .= "Message None: $message\n";
      
    // SEND MAIL TO EACH RECIPIENT
      
if($cntr > 0){
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
   
          }
           else
             {
                 echo "Thank you but you forgot to fill in the everything."; )
         }
             
      }
}
// END OF PHP - PUT UP THE FORM
?>
Yes, you put the 'else' at the end inside the 'foreach' block instead of where I showed you.
if($cntr > 0){
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
   
    } // end of foreach
                        
   } else
       {
        echo "Thank you but you forgot to fill in the everything."; )
       }

Open in new window


this is what i put in ; and it is still causing an syntax error
snipet1

$cntr = 0;
// CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
$email      = clean_string($_POST["email"]);
$cntr += strlen($email);  // add length of variable
$name       = clean_string($_POST["name"]);
$cntr += strlen($name);  // add length of variable
$name       = clean_string($_POST["message"]);
$cntr += strlen($message);  // add length of variable


and snipet 2

//what you told me
if($cntr > 0){
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
   
    } // end of foreach
                       
   } else
       {
        echo "Thank you but you forgot to fill in the everything."; )
       }


?>
There was an extra curly brace at the end of this line:
{
        echo "Thank you but you forgot to fill in the everything.";
       }

Open in new window

I really do appreciate your help, but why am i getting an  error

here is my code


<?php //
error_reporting(E_ALL);

// SEND MAIL FROM A FORM
echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";
echo $_POST["message"]."</p>";


function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{

$cntr = 0;
// CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
$email      = clean_string($_POST["email"]);
$cntr += strlen($email);  // add length of variable
$name       = clean_string($_POST["name"]);
$cntr += strlen($name);  // add length of variable
$name       = clean_string($_POST["message"]);
$cntr += strlen($message);  // add length of variable

      
      
    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query From a person named $name \n\n";

    $content   .= "Email: $email\n";

   $content   .= "Message None: $message\n";
      

if($cntr > 0) {
    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} else { echo "Thank you but you forgot to fill in the everything.";


} // END OF PHP - PUT UP THE FORM
?>
okay now i am getting closer, it is working, but not stating the echo statement
You have two sections that check for content so you can put it in there twice, once for each 'if' statement.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php //
error_reporting(E_ALL);

// SEND MAIL FROM A FORM
// echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
//echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";
//echo $_POST["message"]."</p>";

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
      $cntr += strlen($email);  // add length of variable
    $name       = clean_string($_POST["name"]);
      $cntr += strlen($name);  // add length of variable
      

      $message   = clean_string($_POST["message"]);
      $cntr += strlen($message);  // add length of variable

      
      
    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query from someone named $name \n\n";

    $content   .= "Email: $email\n";

    $content   .= "Message None: $message\n";
      
    // SEND MAIL TO EACH RECIPIENT
      
if($cntr > 0){
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
   
    } // end of foreach
                        
   } else
       {
        echo "Thank you but you forgot to fill in everything.";
       }

} else
       {
        echo "Thank you but you forgot to fill in everything.";
       }

// END OF PHP - PUT UP THE FORM
?>
</body>
</html>

Open in new window

Here is a generalized case that might make sense for your needs.  Please read it over and post back with any specific questions.  Good luck with your project, ~Ray
<?php // RAY_form example.php
error_reporting(E_ALL);
echo "<pre>\n"; // JUST FOR READABILITY


// QUESTION - HOW DO I FIND WHAT WAS POSTED AND CAPTURE IT FOR FURTHER PROCESSING
// QUESTION - HOW DO I TELL THE CLIENT TO ENTER THE OTHER FIELDS THAT WERE OMITTED


// THE FIELDS WE EXPECT FROM THE FORM WILL BE KEPT HERE
$required
= array
( "name"  => "Your Name"
, "email" => "Email Address"
, "phone" => "Telephone"
)
;

// ERROR MESSAGES, RECEIVED DATA, IF ANY, ARE HERE
$received      = array();
$error_fields  = array();
$error_message = '';

// IF THE FORM HAS BEEN SUBMITTED THE POST ARRAY IS NOT EMPTY
if (!empty($_POST))
{
    // WIPE OUT ANY POST VALUES THAT STILL CONTAIN THE DEFAULT FIELD VALUES
    foreach ($_POST as $key => $val)
    {
        if (trim($required[$key]) == trim($val)) unset($_POST[$key]);
    }

    // CHECK TO SEE IF EACH REQUIRED FIELD IS PRESENT
    foreach($required as $key => $val)
    {
        // IF THIS FIELD IS EMPTY - SET THE ERROR MESSAGE AND SAVE THE KEY (OTHER EDITS MAY APPLY)
        if (empty($_POST[$key]))
        {
            $error_message  .= "\nPLEASE ENTER $val";
            $error_fields[] = $key;
        }
        // IF THIS FIELD IS PRESENT AND VALID CAPTURE THE CONTENTS
        else
        {
            $received[$key] = $_POST[$key];
        }
    }

    // DISPLAY THE ERROR MESSAGE, IF ANY
    echo $error_message;

    // ACTIVATE THIS TO SHOW WHAT FIELDS WE CAPTURED
    // echo PHP_EOL . 'WE RECEIVED:' . PHP_EOL;
    // print_r($received);

    // IF NO ERRORS, PROCESSING COMPLETE (MAYBE UPDATE DATA BASE NOW?)
    if (empty($error_message))
    {
        die('THANK-YOU');
    }
} // END OF POST DATA PROCESSING

// END OF PROCESSING THE SUBMITTED INPUT - PUT UP THE FORM TO CAPTURE INPUT

echo '<form method="post">';
foreach ($required as $key => $value)
{
    echo "<br/>";

    // MAKE ERROR FIELDS SHOW IN RED
    if (in_array($key, $error_fields))
    {
        echo '<span style="color:red;">';
    }
    else
    {
        echo '<span>';
    }

    // PRESERVE THE VALUES THAT HAVE BEEN ENTERED
    if (isset($received[$key]))
    {
        $display_value = $received[$key];
    }
    else
    {
        $display_value = $value;
    }

    // PRODUCE THE INPUT CLUE
    echo "$value: </span>";

    // PRODUCE THE INPUT CONTROL
    echo '<input name="' . $key . '" value="' . $display_value . '" />';
}
echo '<input type="submit" />';
echo '</form>';

Open in new window

wow that is cool, now how do i get that sent to me in my email, this is what i did and it didn't work;

snipet:

<?php // RAY_form example.php
error_reporting(E_ALL);
echo "<pre>\n"; // JUST FOR READABILITY


// QUESTION - HOW DO I FIND WHAT WAS POSTED AND CAPTURE IT FOR FURTHER PROCESSING
// QUESTION - HOW DO I TELL THE CLIENT TO ENTER THE OTHER FIELDS THAT WERE OMITTED


// THE FIELDS WE EXPECT FROM THE FORM WILL BE KEPT HERE
$required
= array
( "name"  => "Your Name"
, "email" => "Email Address"
, "message" => "message"
)
;

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";

// ERROR MESSAGES, RECEIVED DATA, IF ANY, ARE HERE
$received      = array();
$error_fields  = array();
$error_message = '';
What happened to the rest of the script?  It looks like it is cut off.  The script I posted was 97 lines.
this is what i did , and it didn't send me the info by email, but it worked but no email sent to me:(

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php // RAY_form example.php

error_reporting(E_ALL);
echo "<pre>\n"; // JUST FOR READABILITY
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";






// THE FIELDS WE EXPECT FROM THE FORM WILL BE KEPT HERE
$required
= array
( "name"  => "Your Name"
, "email" => "Email Address"
, "message" => "message"
)
;



// ERROR MESSAGES, RECEIVED DATA, IF ANY, ARE HERE
$received      = array();
$error_fields  = array();
$error_message = '';

// IF THE FORM HAS BEEN SUBMITTED THE POST ARRAY IS NOT EMPTY
if (!empty($_POST))
{
    // WIPE OUT ANY POST VALUES THAT STILL CONTAIN THE DEFAULT FIELD VALUES
    foreach ($_POST as $key => $val)
    {
        if (trim($required[$key]) == trim($val)) unset($_POST[$key]);
    }

    // CHECK TO SEE IF EACH REQUIRED FIELD IS PRESENT
    foreach($required as $key => $val)
    {
        // IF THIS FIELD IS EMPTY - SET THE ERROR MESSAGE AND SAVE THE KEY (OTHER EDITS MAY APPLY)
        if (empty($_POST[$key]))
        {
            $error_message  .= "\nPLEASE ENTER $val";
            $error_fields[] = $key;
        }
        // IF THIS FIELD IS PRESENT AND VALID CAPTURE THE CONTENTS
        else
        {
            $received[$key] = $_POST[$key];
        }
    }

    // DISPLAY THE ERROR MESSAGE, IF ANY
    echo $error_message;

    // ACTIVATE THIS TO SHOW WHAT FIELDS WE CAPTURED
    // echo PHP_EOL . 'WE RECEIVED:' . PHP_EOL;
    // print_r($received);

    // IF NO ERRORS, PROCESSING COMPLETE (MAYBE UPDATE DATA BASE NOW?)
    if (empty($error_message))
    {
        die('THANK-YOU FOR FILLING OUT THE FORM');
    }
} // END OF POST DATA PROCESSING

// END OF PROCESSING THE SUBMITTED INPUT - PUT UP THE FORM TO CAPTURE INPUT

echo '<form method="post">';
foreach ($required as $key => $value)
{
    echo "<br/>";

    // MAKE ERROR FIELDS SHOW IN RED
    if (in_array($key, $error_fields))
    {
        echo '<span style="color:red;">';
    }
    else
    {
        echo '<span>';
    }

    // PRESERVE THE VALUES THAT HAVE BEEN ENTERED
    if (isset($received[$key]))
    {
        $display_value = $received[$key];
    }
    else
    {
        $display_value = $value;
    }

    // PRODUCE THE INPUT CLUE
    echo "$value: </span>";

    // PRODUCE THE INPUT CONTROL
    echo '<input name="' . $key . '" value="' . $display_value . '" />';
}
echo '<input type="submit" />';
echo '</form>';
?>



</body>
</html>
Please use the code snippet to post code - it makes it much easier to read and copy, and we get line numbers which are helpful to the back-and-forth dialog here at EE.
This tested out fine for me.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php // RAY_temp_imagekrazy.php

error_reporting(E_ALL);
echo "<pre>\n"; // JUST FOR READABILITY
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";




// THE FIELDS WE EXPECT FROM THE FORM WILL BE KEPT HERE
$required
= array
( "name"  => "Your Name"
, "email" => "Email Address"
, "message" => "message"
)
;



// ERROR MESSAGES, RECEIVED DATA, IF ANY, ARE HERE
$received      = array();
$error_fields  = array();
$error_message = '';

// IF THE FORM HAS BEEN SUBMITTED THE POST ARRAY IS NOT EMPTY
if (!empty($_POST))
{
    // WIPE OUT ANY POST VALUES THAT STILL CONTAIN THE DEFAULT FIELD VALUES
    foreach ($_POST as $key => $val)
    {
        if (trim($required[$key]) == trim($val)) unset($_POST[$key]);
    }

    // CHECK TO SEE IF EACH REQUIRED FIELD IS PRESENT
    foreach($required as $key => $val)
    {
        // IF THIS FIELD IS EMPTY - SET THE ERROR MESSAGE AND SAVE THE KEY (OTHER EDITS MAY APPLY)
        if (empty($_POST[$key]))
        {
            $error_message  .= "\nPLEASE ENTER $val";
            $error_fields[] = $key;
        }
        // IF THIS FIELD IS PRESENT AND VALID CAPTURE THE CONTENTS
        else
        {
            $received[$key] = $_POST[$key];
        }
    }

    // DISPLAY THE ERROR MESSAGE, IF ANY
    echo $error_message;

    // ACTIVATE THIS TO SHOW WHAT FIELDS WE CAPTURED
    // echo PHP_EOL . 'WE RECEIVED:' . PHP_EOL;
    // print_r($received);

    // IF NO ERRORS, PROCESSING COMPLETE (MAYBE UPDATE DATA BASE NOW?)
    if (empty($error_message))
    {
        $data = print_r($received, TRUE);
        foreach ($to as $e)
        {
            mail ($e, $subj, $data, $from);
        }
    }
    die("THANKS FOR SUBMITTING THE FORM");
} // END OF POST DATA PROCESSING

// END OF PROCESSING THE SUBMITTED INPUT - PUT UP THE FORM TO CAPTURE INPUT

echo '<form method="post">';
foreach ($required as $key => $value)
{
    echo "<br/>";

    // MAKE ERROR FIELDS SHOW IN RED
    if (in_array($key, $error_fields))
    {
        echo '<span style="color:red;">';
    }
    else
    {
        echo '<span>';
    }

    // PRESERVE THE VALUES THAT HAVE BEEN ENTERED
    if (isset($received[$key]))
    {
        $display_value = $received[$key];
    }
    else
    {
        $display_value = $value;
    }

    // PRODUCE THE INPUT CLUE
    echo "$value: </span>";

    // PRODUCE THE INPUT CONTROL
    echo '<input name="' . $key . '" value="' . $display_value . '" />';
}
echo '<input type="submit" />';
echo '</form>';
?>



</body>
</html>

Open in new window

okay,  I will, any help will be appreciated

<!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 // RAY_form example.php

error_reporting(E_ALL);
echo "<pre>\n"; // JUST FOR READABILITY
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "circleimage@gmail.com";






// THE FIELDS WE EXPECT FROM THE FORM WILL BE KEPT HERE
$required
= array
( "name"  => "Your Name"
, "email" => "Email Address"
, "message" => "message"
)
;



// ERROR MESSAGES, RECEIVED DATA, IF ANY, ARE HERE
$received      = array();
$error_fields  = array();
$error_message = '';

// IF THE FORM HAS BEEN SUBMITTED THE POST ARRAY IS NOT EMPTY
if (!empty($_POST))
{
    // WIPE OUT ANY POST VALUES THAT STILL CONTAIN THE DEFAULT FIELD VALUES
    foreach ($_POST as $key => $val)
    {
        if (trim($required[$key]) == trim($val)) unset($_POST[$key]);
    }

    // CHECK TO SEE IF EACH REQUIRED FIELD IS PRESENT
    foreach($required as $key => $val)
    {
        // IF THIS FIELD IS EMPTY - SET THE ERROR MESSAGE AND SAVE THE KEY (OTHER EDITS MAY APPLY)
        if (empty($_POST[$key]))
        {
            $error_message  .= "\nPLEASE ENTER $val";
            $error_fields[] = $key;
        }
        // IF THIS FIELD IS PRESENT AND VALID CAPTURE THE CONTENTS
        else
        {
            $received[$key] = $_POST[$key];
        }
    }

    // DISPLAY THE ERROR MESSAGE, IF ANY
    echo $error_message;

    // ACTIVATE THIS TO SHOW WHAT FIELDS WE CAPTURED
    // echo PHP_EOL . 'WE RECEIVED:' . PHP_EOL;
    // print_r($received);

    // IF NO ERRORS, PROCESSING COMPLETE (MAYBE UPDATE DATA BASE NOW?)
    if (empty($error_message))
    {
        die('THANK-YOU FOR FILLING OUT THE FORM');
    }
} // END OF POST DATA PROCESSING

// END OF PROCESSING THE SUBMITTED INPUT - PUT UP THE FORM TO CAPTURE INPUT

echo '<form method="post">';
foreach ($required as $key => $value)
{
    echo "<br/>";

    // MAKE ERROR FIELDS SHOW IN RED
    if (in_array($key, $error_fields))
    {
        echo '<span style="color:red;">';
    }
    else
    {
        echo '<span>';
    }

    // PRESERVE THE VALUES THAT HAVE BEEN ENTERED
    if (isset($received[$key]))
    {
        $display_value = $received[$key];
    }
    else
    {
        $display_value = $value;
    }

    // PRODUCE THE INPUT CLUE
    echo "$value: </span>";

    // PRODUCE THE INPUT CONTROL
    echo '<input name="' . $key . '" value="' . $display_value . '" />';
}
echo '<input type="submit" />';
echo '</form>';
?>



</body>
</html>

Open in new window

HEllo again,

i worked also for me, but it came to me in this format
circleimage@gmail.com

Array
(
   [name] => steve
   [email] => steve @whatwhome.com
   [message] => bbb

)
but  why is it coming in this format Array()
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account