-1

Okay, so after I read your initial post i did an UPDATE to the code (in an attempt to make it read simpler) per my understanding because, again, i'm a newbie to this. And looking back now, that may be the problem. So I appreciate you patience; please don't kill me ... Here's what I changed it to and tested on the server:

Here's the actual webpage (ContactsUs.php):

<!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" />
<link href="PSStyles.css" rel="stylesheet" type="text/css">
<title>Contact Us Form</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$("#qForm").validate({
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
comments: "required"
},
messages: {
firstname: "First Name Required",
lastname: "Last Name Required",
email: {
required: "Email Required",
email: "Invalid Email Address"
},
comments: "You must write a message"
}
});
});
</script>

</head>

<body>

<div id="wrapper">
<?php include 'header1.php'?>
</div>




<div id="ripmain">
<div id="menuet">

<nav>
<ul id="menubar">
<li><a href="index.php">Home</a></li>
<li><a href="AboutUs.php">About</a></li>
<li><a href="Location.php">Location</a></li>
<li><a href="GroomingServices.php">Grooming</a></li>
<li><a href="ContactUs.php">Contact Us</a></li>

</ul>
</nav>

</div>
</div>


<form method="POST" action="contact.php" id="qForm">
<fieldset width="954px">
<legend>Contact Us Form</legend>


<p>First Name: <input type="text" size="32" name="firstname" /></p>
<p>Last Name: <input type="text" size="32" name="lastname" /></p>
<p>Email: <input type="text" size="32" id="email" name="email" /></p>
<div id="rsp_email"><!-- --></div>


<td>Comments: </td>
<td>
<textarea name="Comments" cols="40" rows="3" wrap="virtual"></textarea>
</td>

<input type="hidden" name="subject" value="online_submission" />
<p><input type="submit" value="submit"></p>
</fieldset>
</form>

<?php include 'footer1.php';?>

</div>
</body>
</html>

Then I changed the action file (contact.php) to:

<?php
if(isset($_POST['firstname'])) {
$contact    =   validate_inputs($_POST);
if(in_array(false, $contact) === true) {
echo process_errors($contact);
exit;
}
else {
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!

Your contact form has been submitted by:

First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>

Comments:
<?php echo $contact['comments']; ?>

End of message
<?php
$message    =   ob_get_contents();
ob_end_clean();

// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) {
header('Location: thanks.html');
exit();
}
else
die("An error occurred while sending. Please contact the administrator.");
}
}

?>

So, THIS MORNING i've gone back and applied the actual changes as you suggested. The problem is that i'm getting a syntax error on line 140 in the php tag after the closing html tag. it has a problem with one of the closing brackets. Here's this code which would be the new webpage (ContactForm.php):

function error_codes($code = false)
{
$valid['firstname'] =   "Enter your name";
$valid['lastname']  =  "Enter your name";
$valid['subject']   =  "Write a subject";
$valid['email']     =  "Invalid email";
$valid['comments']  =   "Write your comments";

return (isset($valid[$code]))? $valid[$code] : false;
}
// Run the validation and return populated array
function validate_inputs($REQUEST)
{
/* Check all form inputs using check_input function */
$valid['firstname'] =   check_input($REQUEST['firstname']);
$valid['lastname']  =   check_input($REQUEST['lastname']);
$valid['subject']   =   check_input($REQUEST['subject']);
$valid['email']     =   check_input($REQUEST['email'],"email");
$valid['comments']  =   check_input($REQUEST['comments']);

return $valid;
}
// Modify your validate function a bit to do only validation, no returning of errors
function check_input($data = false, $type = false)
{
if($type == 'email')
return (filter_var($data,FILTER_VALIDATE_EMAIL))? $data : false;

$data   =   trim($data);
$data   =   stripslashes($data);
$data   =   htmlspecialchars($data);

return (!empty($data))? $data : false;
}
// This will loop through returned values and populate errors based on empty
function process_errors($array = false)
{
if(!is_array($array))
return $array;

foreach($array as $key => $value) {
if(empty($value))
$errors[]   =   error_codes($key);
}

return (!empty($errors))? show_error($errors) : false;
}
// display errors via buffer output
function show_error($myError)
{
ob_start();


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/css/default.css" rel="stylesheet">
<title>Contact Us Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#qForm").validate({
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
comments: "required"
},
messages: {
firstname: "First Name Required",
lastname: "Last Name Required",
email: {
required: "Email Required",
email: "Invalid Email Address"
},
comments: "You must write a message"
}
});
});
</script>
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo">
<h1 id="sitename"><img src="Images/logo.jpg" alt="logo" width="270" height="105" /></span></h1>
<h2 class="description">The home for pampered pets.</h2>
</div>
<div id="headercontent">
<h2>Happy Pets-timonials</h2>
<p>My owner took me to Sandy's for a bath and I got the 'spaw' treatment. - Rover</p>
</div>
<div id="sitecaption"> Satisfaction <span class="bigger">Guaranteed</span> </div>
</div>
<div id="ripmain">
<div id="menuet">
<nav>
<ul id="menubar">
<li><a href="PSTP.html">Home</a></li>
<li><a href="AboutUs.html">About</a></li>
<li><a href="Location.html">Location</a></li>
<li><a href="GroomingServices.html">Grooming</a></li>
<li><a href="ContactUs.html">Contact Us</a></li>
</ul>
</nav>
</div>

<form method="POST" action="ContactProcess.php" id="qForm">
<fieldset>
<legend>Contact Us Form</legend>
<p>First Name: <input type="text" size="32" name="firstname" /></p>
<p>Last Name: <input type="text" size="32" name="lastname" /></p>
<p>Email: <input type="text" size="32" id="email" name="email" /></p>
<div id="rsp_email"><!-- --></div>
<td>Comments: </td>
<td>
<textarea name="comments" cols="40" rows="3" wrap="virtual"></textarea>
</td>
<input type="hidden" name="subject" value="online_submission" />
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<div id="footer"> &copy; Copyright 2015 Time Live, Inc. All rights reserved. <br>
Hours: Mon-Fri: 6 am to 11 pm; Sat & Sun: 8 am to 10pm <br>
Links to other local services:   <li><a href="http://www.hillsidevetclinic.org">Hillside Vet Clinic</a></li> <li><a href="http://www.petsmart.com">PetSmart Stores</a></li> <li><a href="http://www.poochhotel.com">Pooch Hotel</a> </div>
</body>
</html>

<?php
$data   =   ob_get_contents();
ob_end_clean();

return $data;
}

function send_email($settings = false)
{
$to         =   (!empty($settings['to']))? $settings['to']:false;
$from       =   (!empty($settings['from']))? "From:".$settings['from'].PHP_EOL:false;
$subject    =   (!empty($settings['subject']))? $settings['subject']:false;
$message    =   (!empty($settings['message']))? $settings['message']:false;

if(in_array(false, $settings) === true)
return false;

return (mail($to,$subject,$message));
}
?>

And here's the new post file (ContactProcess.php) per your suggestion:

<?php
if(isset($_POST['firstname'])) {
$contact    =   validate_inputs($_POST);
if(in_array(false, $contact) === true) {
echo process_errors($contact);
exit;
}
else {
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!

Your contact form has been submitted by:

First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>

Comments:
<?php echo $contact['comments']; ?>

End of message
<?php
$message    =   ob_get_contents();
ob_end_clean();

// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) {
header('Location: thanks.html');
exit();
}
else
die("An error occurred while sending. Please contact the administrator.");
}
}

I have not tested this second code; but will do so and let you know what i find; in the meantime any advice on the revised/updated code above? Thanks again for your help ...

1 Answers1

0

Try splitting up some of your logic into little functions, it is easier to keep track of tasks. Also for the form, try using form validation via jQuery:

form page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/css/default.css" rel="stylesheet">
<title>Contact Us Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#qForm").validate({
            rules: {
                firstname: "required",
                lastname: "required",
                email: {
                    required: true,
                    email: true
                },
                comments: "required"
            },
            messages: {
                firstname: "First Name Required",
                lastname: "Last Name Required",
                email: {
                    required: "Email Required",
                    email: "Invalid Email Address"
                },
                comments: "You must write a message"
            }
        });
});
</script>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <div id="logo">
            <h1 id="sitename"><img src="Images/logo.jpg" alt="logo" width="270" height="105" /></span></h1>
            <h2 class="description">The home for pampered pets.</h2>
        </div>
        <div id="headercontent">
            <h2>Happy Pets-timonials</h2>
            <p>My owner took me to Sandy's for a bath and I got the 'spaw' treatment. - Rover</p>
        </div>
        <div id="sitecaption"> Satisfaction <span class="bigger">Guaranteed</span> </div>
    </div>
    <div id="ripmain">
        <div id="menuet">
            <nav>
                <ul id="menubar">
                  <li><a href="PSTP.html">Home</a></li>
                  <li><a href="AboutUs.html">About</a></li>
                  <li><a href="Location.html">Location</a></li>
                  <li><a href="GroomingServices.html">Grooming</a></li>
                  <li><a href="ContactUs.html">Contact Us</a></li>
                </ul>
            </nav>
        </div>

<form method="POST" action="contact.php" id="qForm">
    <fieldset>
        <legend>Contact Us Form</legend>
        <p>First Name: <input type="text" size="32" name="firstname" /></p>
        <p>Last Name: <input type="text" size="32" name="lastname" /></p>
        <p>Email: <input type="text" size="32" id="email" name="email" /></p>
        <div id="rsp_email"><!-- --></div>
        <td>Comments: </td>
        <td>
            <textarea name="comments" cols="40" rows="3" wrap="virtual"></textarea>
        </td>
            <input type="hidden" name="subject" value="online_submission" />
        <p><input type="submit" value="Submit"></p>
    </fieldset>
</form>
<div id="footer"> &copy; Copyright 2015 Time Live, Inc. All rights reserved. <br>
Hours: Mon-Fri: 6 am to 11 pm; Sat & Sun: 8 am to 10pm <br>
Links to other local services:   <li><a href="http://www.hillsidevetclinic.org">Hillside Vet Clinic</a></li> <li><a href="http://www.petsmart.com">PetSmart Stores</a></li> <li><a href="http://www.poochhotel.com">Pooch Hotel</a> </div>
</body>
</html>

functions required on the contact form:

// This will return error messages (you could expand it to be database driven)
function error_codes($code = false)
    {
        $valid['firstname'] =   "Enter your name";
        $valid['lastname']  =  "Enter your name";
        $valid['subject']   =  "Write a subject";
        $valid['email']     =  "Invalid email";
        $valid['comments']  =   "Write your comments";

        return (isset($valid[$code]))? $valid[$code] : false;
    }
// Run the validation and return populated array
function validate_inputs($REQUEST)
    {
        /* Check all form inputs using check_input function */
        $valid['firstname'] =   check_input($REQUEST['firstname']);
        $valid['lastname']  =   check_input($REQUEST['lastname']);
        $valid['subject']   =   check_input($REQUEST['subject']);
        $valid['email']     =   check_input($REQUEST['email'],"email");
        $valid['comments']  =   check_input($REQUEST['comments']);

        return $valid;
    }
// Modify your validate function a bit to do only validation, no returning of errors
function check_input($data = false, $type = false)
    {
        if($type == 'email')
            return (filter_var($data,FILTER_VALIDATE_EMAIL))? $data : false;

        $data   =   trim($data);
        $data   =   stripslashes($data);
        $data   =   htmlspecialchars($data);

        return (!empty($data))? $data : false;
    }
// This will loop through returned values and populate errors based on empty
function process_errors($array = false)
    {
        if(!is_array($array))
            return $array;

        foreach($array as $key => $value) {
                if(empty($value))
                    $errors[]   =   error_codes($key);
            }

        return (!empty($errors))? show_error($errors) : false;
    }
// display errors via buffer output
function show_error($myError)
    {
        ob_start();
?><!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>
<b>Please correct the following error:</b><br />
<?php echo implode("<br />".PHP_EOL,$myError); ?>
</body>
</html>
<?php
        $data   =   ob_get_contents();
        ob_end_clean();

        return $data;
    }

function send_email($settings = false)
    {
        $to         =   (!empty($settings['to']))? $settings['to']:false;
        $from       =   (!empty($settings['from']))? "From:".$settings['from'].PHP_EOL:false;
        $subject    =   (!empty($settings['subject']))? $settings['subject']:false;
        $message    =   (!empty($settings['message']))? $settings['message']:false;

        if(in_array(false, $settings) === true)
            return false;

        return (mail($to,$subject,$message));
    }

contact.php:

// Include above functions
if(isset($_POST['firstname'])) {
        $contact    =   validate_inputs($_POST);
        if(in_array(false, $contact) === true) {
                echo process_errors($contact);
                exit;
            }
        else {
                /* Let's prepare the message for the e-mail */
                ob_start();
?>Hello!

Your contact form has been submitted by:

First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>

Comments:
<?php echo $contact['comments']; ?>

End of message
<?php
                $message    =   ob_get_contents();
                ob_end_clean();

                // Send the message here
             if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) {
                        header('Location: thanks.html');
                        exit();
                    }
                else
                    die("An error occurred while sending. Please contact the administrator.");
            }
    }
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Thx! I appreciate your help. – great scott Oct 01 '15 at 22:47
  • ok; looked through the contact.php file and don't see what may be the problem there; do have a message in the body that says Please correct the following error ... but that doesn't explain why it's still not sending. would it need to connect. ... – great scott Oct 02 '15 at 20:25
  • Sorry what is your question? What I have posted is an update/revision of the `contact.php` page. If it's missing info it will exit and print to page the missing info. I tested the code on my hosting and it worked A-Ok. Did you try it out or what have you tried thus far? – Rasclatt Oct 02 '15 at 21:26
  • I could probably post a demo if you wanted to see it working. – Rasclatt Oct 02 '15 at 21:30
  • I have updated with the entire revised code. The only part I am not sure of was your mail at the top of the form page.... – Rasclatt Oct 02 '15 at 21:50
  • oh, i see; so, i made those changes; and yes a demo would be great; when I hit the submit button i got the following: Hello! Your contact form has been submitted by: First Name: Last Name: Email Comments: End of message "greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) { header('Location: thanks.html'); exit(); } else die("An error occurred while sending. Please contact the administrator."); } } // display errors via buffer output function show_error($myError) { ob_start(); ?> Please correct the following error: – great scott Oct 04 '15 at 00:13
  • ".PHP_EOL,$myError); ?> – great scott Oct 04 '15 at 00:13
  • well, it rolled over to a new page that said something like: whoops, wrong page; this page does not exist or has moved, etc ... at least it's not a blank page or one with code all over it ... lol – great scott Oct 05 '15 at 04:37
  • Well it goes to whoops because I don't have a thanks page on my site when it completes the send. Yours though, does yours have errors using this script? – Rasclatt Oct 05 '15 at 04:44
  • I assume you are writing "test"? I am getting a couple emails here and there...? – Rasclatt Oct 05 '15 at 06:03
  • yes, just "test"; i'm guessing it's just not working for me; kinda frustrating but I've got to move on to my last part of this website ... calling the database ... i really appreciate all of your help; believe me this hasn't been easy; : ) – great scott Oct 08 '15 at 17:44
  • Wait, so your implementation my script above doesn't return anything to you on your site? – Rasclatt Oct 08 '15 at 17:53
  • no, it doesn't; and now my header and footer aren't showing up; turns out i was supposed to make all of these pages php; and use php includes for the headers and footers; after doing the latter that's not even showing up; just the form ... *smh* and when I hit submit, the code no longer shows up at the top but it doesn't roll over to show that the form has been submitted ... – great scott Oct 08 '15 at 18:37
  • What is your folder structure? You probably need to make some adjustments to the organization of your site. – Rasclatt Oct 08 '15 at 18:39
  • So, i thought about that and had to make some moves/changes; when i FINALLY was able to connect to the remote server, it rolled over to a new page and gave me an error message about the third line in my code, something about $comments being undefined – great scott Oct 09 '15 at 16:52
  • So did it work except for the `$comments` being undefined? – Rasclatt Oct 09 '15 at 16:53
  • Ok; ran the code just as you suggested and got the following error message: Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\pet-shop\ContactForm.php on line 140; AND got the following message from the action file: Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15 – great scott Oct 09 '15 at 19:00
  • 1
    You totally answered my question!!!!! So, I marked it with a check mark but don't have enough points yet to up vote you. But as soon as i do, I will. Thanks again Rasclatt!!!! – great scott Oct 10 '15 at 00:37