Hi ... I wrote a script to send a feedback form to an email. Here's the script below;

//PHP CODE:

<?php session_start();
$submitted = FALSE;

ini_set("SMTP","mail.sitecghana.com");

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

$submitted = TRUE;  // The form has been submitted and everything is ok so far…

$name = htmlspecialchars($_POST['name'], ENT_QUOTES);
$email = htmlspecialchars($_POST['email'], ENT_QUOTES);
$message = htmlspecialchars($_POST['message'], ENT_QUOTES);

if ($email == "") {
// if the email is blank… give error notice.
echo "<p>"."Please enter your Email address so that we can reply to you."."</p>";
//pop_alert('Please enter your email');
$submitted = FALSE;  // Set this to FALSE so that it the message is not sent.
}
if ($message == "") {
// if the message is blank… give error notice.
//echo "<p>"."Please enter a message."."</p>";
pop_alert('Please enter a message');
$submitted = FALSE;  // Set this to FALSE so that it the message is not sent.
}

if ($_POST['email'] != "" && (!strstr($_POST['email'],"@") || !strstr($_POST['email'],".")))
{
// if the string does not contain “@” OR the string does not contain “.” then…
// supply a different error notice.
echo "<p>"."Please enter a valid e-mail address."."</p>";
//pop_alert('Please enter a valid email address');
$submitted = FALSE;  // Set this to FALSE so that it the message is not sent.
}

$to = "info@sitecghana.com";  // Set the target email address.
$header = "From: $email:";
$subject = "Feedback Message from Sitec Ghana!";
$message = "Feedback Message: $subject \n From: $name \n Message: $message \n";
if ($submitted == TRUE)
{
mail($to, $subject, $message, $header);
$_SESSION['email_status'] = "Thank you $name Your message has been sent.";
}
}

?>


THE HTML FORM:
<?php
                    echo "<h3>".@$_SESSION['email_status']."</h3>"; 
                    unset($_SESSION['email_status']);
                    session_destroy(); 
                     ?>

                    <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post" class="peThemeContactForm">

                      <div class="control-group">
                          <div class="controls">
                            <input id="name" type="text" name="name" placeholder="Your name here.." class="span5 required" data-fieldid="0">
                            <?php if ($name == "") {
// if the name is blank… give error notice.
echo "<p style=\"color:#ff0000;\">"."Please enter your name."."</p>";
//pop_alert('Please enter your name');
$submitted = FALSE;  // Set this to FALSE so that it the message is not sent.
}?>
                          </div>

                      </div>

                      <div class="control-group">
                          <div class="controls">
                            <input id="email" type="text" name="email" placeholder="Your email here.." class="span5 required" data-fieldid="1">
                            <?php if ($email == "") {
// if the email is blank… give error notice.
echo "<p>"."Please enter your Email address so that we can reply to you."."</p>";
//pop_alert('Please enter your email');
$submitted = FALSE;  // Set this to FALSE so that it the message is not sent.
}
if ($_POST['email'] != "" && (!strstr($_POST['email'],"@") || !strstr($_POST['email'],".")))
{
// if the string does not contain “@” OR the string does not contain “.” then…
// supply a different error notice.
echo "<p>"."Please enter a valid e-mail address."."</p>";
//echo('Please enter a valid email address');
$submitted = FALSE;  // Set this to FALSE so that it the message is not sent.
}?>
                          </div>

                      </div>

                      <div class="control-group">
                          <textarea name="message" id="message" class="span7 required" data-fieldid="3" cols="45" rows="8" placeholder="Your message here..">


                  </div>

                  <input id="send_message_button" class="btn btn-success" type="submit" name="send_message_button" value="Send Message"/>


                </form>

Recommended Answers

All 2 Replies

The script works because the form submits. But the issue is that i keep gettinga prompt that the $name, $email, $message variables are undefined.

It happens because you are defining these variables only when a POST request is submitted to the script, while on GET request (i.e. when you open the page) these variables are not defined.

So, initizialize them on top of the script:

<?php session_start();

    $submitted = FALSE;
    $name = '';
    $email = '';
    $message = '';

For the validation of the form, instead, use filter_input:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.