 |
|
|
09-10-2007, 08:55 PM
|
Forms Just sit there
|
Posts: 89
|
I have a site with some forms. When you fill in all the correct forms and click submit it just says please enter all the correct forms like I told it to if someone didn't put in there email for instance. Please tell me if you want the url of the actual site, but hear is the code. Any help would really be appreciated.
Code:
<?php
include('Includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
nukeMagicQuotes();
}
/////////////////////////////////////////////////
//go ahead only if not suspect and all required fields OK
if (!$suspect && empty($missing)) {
//set defaul values for variables that might not exist
$quote = isset($quote) ? $subscribe : 'Nothing Selected';
}
///////////////////////////////////////////////////////////
$subject = 'Project Idea';
//list expected feilds
$expected = array('name', 'email', 'budget', 'comments');
//set required fields
$required = array('name', 'comments', 'email');
//creat an empty array for any missing fields
$missing = array();
///////////////////////////////////////////////////////////
//process email
if (array_key_exists('send', $_POST)) {
$to = "goldfishgraphics@gmail.com"; //my email
$subject = "Project Idea";
//create empty array for any missing fields
$missing = array();
//assume that there is nothing suspect
$suspect = false;
//create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';
//function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
// if one of the suspect phrases is found, set Boolean to tur
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
// check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);
if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
//process the $_POST variables
foreach ($_POST as $key => $value) {
//asign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
//if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
//otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}
//build the message
//go ahead if required feilds are in
if (!$suspect && empty($missing)) {
//build message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message = "Budget: $budget\n\n";
$message .= "Comments: $comments";
//limit line length to 65 characters
$message = wordwrap($message, 65);
// send it
$mailSent = mail($to, $subject, $message);
if ($mailsent) {
//missing is no longer needed if the meail is sent, so unset it
unset($missing);
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!-- Form CSS -->
<style type=”text/css”>
label {display: block;} /* make sure that the input will be displayed below the label */
.text_input {width: 450px;margin-bottom: 5px;} /* each input field with a class value of text_input will be 450px wide and will have a margin of 5px on the bottom */
textarea {width: 450px ! important;height: 150px;} /* each <textarea> tag will be 450px wide and have a height of 150px*/
</style>
<!-- (((((()))))) -->
<title>Goldfish Graphics</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id="container">
<div id="header">
<!-- PUT HEADER CODE HERE -->
</div>
<div id="contentt">
<!-- PUT CONTENTT CODE HERE -->
</div>
<div id="content">
<!-- CONTENT CODE -->
<?php
@include('Includes/menue.html');
?>
<h3 class="heading">Contact</h3>
<!-- FORM CODE ***************************************** -->
<form name="feedback" action="" method="post" class="bcontent">
<table>
<tr>
<td><label for="name">Name:</label></td>
<td>
<input name="name" id="name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name']).'"';
} ?>/>
</td>
</tr>
<tr>
<td><label for="email">Email:</label></td>
<td><input name="email" id="email" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email']).'"';
} ?>/>
</td>
</tr>
<tr>
<td><label for="budget">Budget:</label></td>
<td><input name="budget" id="budget" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['budget']).'"';
} ?>/>
</td>
</tr>
</table>
<label for="comments">Prodject Description:</label>
<br /><br />
<textarea name="comments" id="comments" cols="50" rows="10"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments']);
} ?></textarea>
<br /><br />
<input name="send" id="send" type="submit" value="Submit" />
</form>
<?php
if ($_POST && isset($missing)) {
?>
<p class="bcontent"><span class="gg">Please complete all required missing item(s).</span></p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="bcontent"><span class="gg">Sorry, there was a problem sending your message. Please try later.</span></p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class="bcontent"><span class="gg">Your message has been sent. Thank you.
</span></p>
<?php }?>
<!-- FORM CODE ********************************************* -->
<p class="bcontent">
You can also contact me at <span class="gg">goldfishgraphics@gmail.com</span>
</p>
</div>
<div id="contentb">
<!-- CONTENTB CODE -->
</div>
<div id="footer">
<!-- PUT FOOTER CODE HERE -->
<p class="copyright" align="center">©
<?php ini_set('date.timezone', 'American/Eastcoast');
$startYear = 2007;
$thisYear = date('Y');
if ($startYear == $thisYear) {
echo $startYear;
}
else {
echo "{$startYear}-{$thisYear}";
}
?>
Goldfish Graphics</p>
</div>
</div>
</body>
</html>
|
|
|
|
09-10-2007, 09:32 PM
|
Re: Forms Just sit there
|
Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
|
Always develop with error_reporting = 'E_ALL' and display_errors = 'on'. Your code had several errors, undeclared vars and remember, php var names are CasE senSitivE; $mailSent != $mailsent.
After cleaning up all the errors, the code works fine.
PHP Code:
<?php @include('Includes/corefuncs.php'); if (function_exists('nukeMagicQuotes')) { nukeMagicQuotes(); } //declare script vars $suspect = false;
/////////////////////////////////////////////////
//go ahead only if not suspect and all required fields OK if (!$suspect && empty($missing)) { //set defaul values for variables that might not exist $quote = isset($quote) ? $subscribe : 'Nothing Selected'; } ///////////////////////////////////////////////////////////
$subject = 'Project Idea';
//list expected feilds $expected = array('name', 'email', 'budget', 'comments'); //set required fields $required = array('name', 'comments', 'email'); //creat an empty array for any missing fields $missing = array();
///////////////////////////////////////////////////////////
//process email if (array_key_exists('send', $_POST)) { $to = "goldfishgraphics@gmail.com"; //my email $subject = "Project Idea"; //create empty array for any missing fields $missing = array(); //create a pattern to locate suspect phrases $pattern = '/Content-Type:|Bcc:|Cc:/i';
//function to check for suspect phrases function isSuspect($val, $pattern, &$suspect) { // if the variable is an array, loop through each element // and pass it recursively back to the same function if (is_array($val)) { foreach ($val as $item) { isSuspect($item, $pattern, $suspect); } } else { // if one of the suspect phrases is found, set Boolean to tur if (preg_match($pattern, $val)) { $suspect = true; } } }
// check the $_POST array and any subarrays for suspect content isSuspect($_POST, $pattern, $suspect);
if ($suspect) { $mailsent = false; unset($missing); } else {
//process the $_POST variables foreach ($_POST as $key => $value) { //asign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); //if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } //otherwise, assign to a variable of the same name as $key elseif (in_array($key, $expected)) { ${$key} = $temp; } } } //build the message
//go ahead if required feilds are in if (!$suspect && empty($missing)) {
//build message $message = "Name: $name\n\n"; $message .= "Email: $email\n\n"; $message = "Budget: $budget\n\n"; $message .= "Comments: $comments"; //limit line length to 65 characters $message = wordwrap($message, 65);
// send it $mailsent = mail($to, $subject, $message); if ($mailsent) { //missing is no longer needed if the meail is sent, so unset it unset($missing); } } }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!-- Form CSS --> <style type=”text/css”>
label {display: block;} /* make sure that the input will be displayed below the label */ .text_input {width: 450px;margin-bottom: 5px;} /* each input field with a class value of text_input will be 450px wide and will have a margin of 5px on the bottom */ textarea {width: 450px ! important;height: 150px;} /* each <textarea> tag will be 450px wide and have a height of 150px*/ </style> <!-- (((((()))))) -->
<title>Goldfish Graphics</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id="container">
<div id="header"> <!-- PUT HEADER CODE HERE --> </div> <div id="contentt"> <!-- PUT CONTENTT CODE HERE --> </div>
<div id="content"> <!-- CONTENT CODE --> <?php @include('Includes/menue.html'); ?> <h3 class="heading">Contact</h3> <!-- FORM CODE ***************************************** -->
<form name="feedback" action="" method="post" class="bcontent">
<table> <tr> <td><label for="name">Name:</label></td> <td> <input name="name" id="name" type="text" class="formbox" value="<? if(!empty($_POST['name'])) echo htmlentities($_POST['name']);?>" /> </td> </tr> <tr> <td><label for="email">Email:</label></td> <td><input name="email" id="email" type="text" class="formbox" value="<? if(!empty($_POST['email'])) echo htmlentities($_POST['email']);?>" /> </td> </tr> <tr>
<td><label for="budget">Budget:</label></td> <td><input name="budget" id="budget" type="text" class="formbox" value="<? if(!empty($_POST['budget'])) echo htmlentities($_POST['budget']);?>" /> </td> </tr> </table>
<label for="comments">Prodject Description:</label> <br /><br /> <textarea name="comments" id="comments" cols="50" rows="10"><? if(!empty($_POST['comments'])) echo htmlentities($_POST['comments']);?> </textarea> <br /><br /> <input name="send" id="send" type="submit" value="Submit" />
</form>
<?php if ($_POST && isset($missing)) { ?> <p class="bcontent"><span class="gg">Please complete all required missing item(s).</span></p> <?php } elseif ($_POST && !$mailsent) { ?> <p class="bcontent"><span class="gg">Sorry, there was a problem sending your message. Please try later.</span></p> <?php } elseif ($_POST && $mailsent) { ?> <p class="bcontent"><span class="gg">Your message has been sent. Thank you. </span></p> <?php }?>
<!-- FORM CODE ********************************************* --> <p class="bcontent"> You can also contact me at <span class="gg">goldfishgraphics@gmail.com</span> </p> </div>
<div id="contentb"> <!-- CONTENTB CODE --> </div>
<div id="footer"> <!-- PUT FOOTER CODE HERE --> <p class="copyright" align="center">© <?php ini_set('date.timezone', 'American/Eastcoast'); $startYear = 2007; $thisYear = date('Y'); if ($startYear == $thisYear) { echo $startYear; } else { echo "{$startYear}-{$thisYear}"; } ?> Goldfish Graphics</p> </div>
</div>
</body> </html>
p.s. there are still logic errors concerning $suspect and the email message but it works for the time being.
Last edited by metho; 09-10-2007 at 09:34 PM..
Reason: more errors still
|
|
|
|
09-10-2007, 09:50 PM
|
Re: Forms Just sit there
|
Posts: 89
|
Thank you first off, how do you turn on this error thingamajig? I don't know because I am very new to php and really to be honest am learning it by copying it out of books (I Know not the best way, but I am very busy right now. next summer I will learn it the right way.) But ya how do you turn on the error thing?
|
|
|
|
09-10-2007, 09:58 PM
|
Re: Forms Just sit there
|
Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
|
At the top of each page, include this little snippet:
PHP Code:
<?php @ini_set('error_reporting','E_ALL'); @ini_set('display_errors','On'); ?>
If no errors display and something is very wrong, your script may have a fatal error. In the case of a fatal error, overriding php's config with ini_set will not work.
|
|
|
|
09-10-2007, 10:01 PM
|
Re: Forms Just sit there
|
Posts: 89
|
Well its still not working I replaced your code with mine. I don't know if possibly it has to do with the fact that it takes a while for my site to update, but I hope its not a fatal error. So you are saying the code you posted should work?
|
|
|
|
09-10-2007, 10:23 PM
|
Re: Forms Just sit there
|
Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
|
It worked fine on my system, however I don't have includes/corefuncs.php. I supressed errors for this include to test the rest of the script.
When all fields were filled in, I received this email message:
Quote:
Budget: budget
Comments: comments
|
As I said, the script has logic errors as opposed to syntax errors. Logic errors are holes in your logic; php will parse and run the script no problems, but it will not run as you expected.
There may also be additional problems with corefuncs.php as well.
|
|
|
|
09-10-2007, 10:42 PM
|
Re: Forms Just sit there
|
Posts: 89
|
What do you mean it will run, but not as expected? Ok hear is the url of the site http://goldfishgraphicshome.com/contact.php it's not finished so don't like criticize the site to much, but anyway. Will it send the email like I want it to? If now What do I have To do to get it to work?
|
|
|
|
09-10-2007, 11:12 PM
|
Re: Forms Just sit there
|
Posts: 89
|
Sorry didn't mean to bug just wasn't sure if I am understanding you right. I got rid of nuke magic quotes and the include file so now it shouldn't be a problem, but what is messing it up for me but not for you? Is it my hosting company? (Go daddy)
|
|
|
|
09-10-2007, 11:57 PM
|
Re: Forms Just sit there
|
Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
|
Could be a number of things; but the lack of a "From: " header will trigger a lot of spam filters. Also, if the server generating this email has a dynamic IP or no reverse DNS, many mailservers will reject it.
Add the default "From: " address in the script to any spam whitelists you may have.
The code you chose to play with is rather convoluted for a contact form. Here's an easier version to for you to use and gain some understanding about the functions/logic used (tested and works 100% fine). The comments explain everything:
PHP Code:
<?php @include('Includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) nukeMagicQuotes(); //onpage errors: to display to user $onpageErrors = '';
//successful mailout $mailsent = false;
//FORM PROCESSING if ( isset($_POST['send']) ) { //long to short vars; trim and set required vars to false if empty $name = ( !empty($_POST['name']) ) ? trim($_POST['name']) : false; $email = ( !empty($_POST['email']) ) ? trim($_POST['email']) : false; $budget = ( !empty($_POST['budget']) ) ? trim($_POST['budget']) : NULL; $comments = ( !empty($_POST['comments']) ) ? trim($_POST['comments']) : false; //check if required fields are false (empty) if(!$name) $onpageErrors .= 'Your name is required.<br />'; if(!$email) $onpageErrors .= 'Your email is required.<br />'; if(!$comments) $onpageErrors .= 'Your comment is required.<br />'; //continue if error free if($onpageErrors == '') { //email address to send messages to $to = "goldfishgraphics@gmail.com"; //subject for email message $subject = "Project Idea"; //default From address $headers = "From: noReply@goldfishgraphics.com\r\n"; //create a pattern to locate suspect spam injection $pattern = '/Content-Type:|Bcc:|Cc:|To:/i'; // iterate through the POST array & any subarrays for spam injection foreach($_POST as $k => $v){ // if the index is an array, loop through each value // and check for spam injection if (is_array($k)) { foreach ($k as $item) { if(preg_match( $pattern,$item ) > 0) $onpageErrors .= "$k contains suspected spam content.<br />"; } } else { if (preg_match( $pattern, $v ) > 0) $onpageErrors .= "$k contains suspected spam content.<br />"; } }//end loop }//end if error free //go ahead if error free if ($onpageErrors == '') { //build message $message = "Name: $name\n\n"; $message .= "Email: $email\n\n"; $message .= "Budget: $budget\n\n"; $message .= "Comments: $comments"; //limit line length to 65 characters $message = wordwrap($message, 65); // send it $mailsent = mail($to, $subject, $message, $headers); //output error if mail fails to send if(!$mailsent) $onpageErrors .= 'There is a problem with server mail. Message could not be sent. Please try again later.<br />'; }//end if error free
}//end form processing ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!-- Form CSS --> <style type="text/css">
label {display: block;} /* make sure that the input will be displayed below the label */ .text_input {width: 450px;margin-bottom: 5px;} /* each input field with a class value of text_input will be 450px wide and will have a margin of 5px on the bottom */ textarea {width: 450px ! important;height: 150px;} /* each <textarea> tag will be 450px wide and have a height of 150px*/ </style> <!-- (((((()))))) -->
<title>Goldfish Graphics</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id="container">
<div id="header"> <!-- PUT HEADER CODE HERE --> </div> <div id="contentt"> <!-- PUT CONTENTT CODE HERE --> </div>
<div id="content"> <!-- CONTENT CODE --> <?php @include('Includes/menue.html'); ?> <h3 class="heading">Contact</h3>
<?php
//display errors if($onpageErrors != '') { ?>
<p style="color:#FF0000"><?php echo $onpageErrors; ?></p>
<?php //end display errors }
//if mail not sent, display form if(!$mailsent) { ?>
<form name="feedback" action="" method="post" class="bcontent"> <table> <tr> <td> <label for="name">*Name:</label> </td> <td> <input name="name" id="name" type="text" class="formbox" value="<? if(!empty($_POST['name'])) echo htmlentities($_POST['name']);?>" /> </td> </tr> <tr> <td> <label for="email">*Email:</label> </td> <td><input name="email" id="email" type="text" class="formbox" value="<? if(!empty($_POST['email'])) echo htmlentities($_POST['email']);?>" /> </td> </tr> <tr> <td><label for="budget"> Budget:</label> </td> <td><input name="budget" id="budget" type="text" class="formbox" value="<? if(!empty($_POST['budget'])) echo htmlentities($_POST['budget']);?>" /> </td> </tr> </table> <label for="comments">*Prodject Description:</label> <br /><br /> <textarea name="comments" id="comments" cols="50" rows="10"><? if(!empty($_POST['comments'])) echo htmlentities($_POST['comments']);?> </textarea> <br /> <p>*denotes required fields</p> <input name="send" id="send" type="submit" value="Submit" /> </form>
<?php //else display mailsent message } else { ?> <p class="bcontent"><span class="gg">Your message has been sent. Thank you.</span></p> <?php } ?> <p class="bcontent"> You can also contact me at <span class="gg">goldfishgraphics@gmail.com</span> </p> </div>
<div id="contentb"> <!-- CONTENTB CODE --> </div>
<div id="footer"> <!-- PUT FOOTER CODE HERE --> <p class="copyright" align="center">© <?php ini_set('date.timezone', 'American/Eastcoast'); $startYear = 2007; $thisYear = date('Y'); if ($startYear == $thisYear) { echo $startYear; } else { echo "{$startYear}-{$thisYear}"; } ?> Goldfish Graphics</p> </div>
</div>
</body> </html>
Last edited by metho; 09-11-2007 at 12:02 AM..
Reason: charset conflict after pasting code
|
|
|
|
09-11-2007, 12:21 AM
|
Re: Forms Just sit there
|
Posts: 89
|
Wow you are amazing. I wish I could repay you but you probably know everything already lol thanks a ton and I will go through that script and learn it well because it worked.
|
|
|
|
09-11-2007, 12:38 AM
|
Re: Forms Just sit there
|
Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
|
Ur welcome Bouzy. This is really only a few basics in php; forms, arrays, iterating thru arrays, error checking, conditionals and some functions; trim(), preg_match() and mail(). Get a grip on those and you're off to a good start.
|
|
|
|
07-06-2008, 02:50 PM
|
Re: Forms Just sit there
|
Posts: 95
Name: Indrit
|
hey I was just studying this kind of contact form or feedback form, call it whatever,and I saw it is identical to this book I am reading*(Web Development made easy), so I generally understand the code that comes from the book and written from the guy with the "goldenfish" website.
So my answer is: Why is there logical errors on that code? Could you give some more detail.
|
|
|
|
|
« Reply to Forms Just sit there
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|