Contact form white page of death - php

When I try to get my contact form working, my browser crashes and sends me a white page. I have found the reason, but I cannot understand what is wrong.
//getting fields
$naam = $_POST['naam'];
$email = $_POST['email'];
$adres = $_POST['adres'];
$postcode = $_POST['postcode'];
$telefoon = $_POST['telefoon'];
$iban = $_POST['iban'];
$15 = $_POST['15'];
$20 = $_POST['20'];
$25 = $_POST['25'];
$30 = $_POST['30'];
$anderbedrag = $_POST['anderbedrag'];
//message to webmaster
$message = $naam;
$message .= "jaarlijks bijdrage: ";
if ($15 != null){
$message .= '15,-';
}
if ($15 != null){
$message .= '20,-';
}
if ($15 != null){
$message .= '25,-';
}
if ($15 != null){
$message .= '30,-';
}
if ($anderbedrag != null){
$message .= $anderbedrag;
}

From the docs:
A valid variable name starts with a letter or underscore, followed by
any number of letters, numbers, or underscores.
So your variable names aren't valid ($15 etc).
Besides that; always enable error reporting when developing. Php would have told you this.
ini_set('display_errors', 1);
error_reporting(E_ALL);

Related

HTML, PHP form submit and then redirect error

I don't know what i'm doing wrong... I want to submit a php form and then I want to redirect to another page outside my url. This is my code, I'm a beginner in this, so any help is very appreciated.
Code:
<?php
if(empty($_POST['fname1']) && strlen($_POST['fname1']) == 0 || empty($_POST['lname1']) && strlen($_POST['lname1']) == 0 || empty($_POST['email1']) && strlen($_POST['email1']) == 0 || empty($_POST['address1']) && strlen($_POST['address1']) == 0 || empty($_POST['city1']) && strlen($_POST['city1']) == 0 || empty($_POST['cel1']) && strlen($_POST['cel1']) == 0)
{
return false;
}
$fname1 = $_POST['fname1'];
$lname1 = $_POST['lname1'];
$email1 = $_POST['email1'];
$address1 = $_POST['address1'];
$city1 = $_POST['city1'];
$cel1 = $_POST['cel1'];
$veg_24176 = $_POST['veg_24176'];
$soy_24176 = $_POST['soy_24176'];
$glu_24176 = $_POST['glu_24176'];
$lac_24176 = $_POST['lac_24176'];
$chili_24176 = $_POST['chili_24176'];
$nuts_24176 = $_POST['nuts_24176'];
$choco_24176 = $_POST['choco_24176'];
$comments1 = $_POST['comments1'];
$to = 'hola#chipzcolombia.com'; // Email submissions are sent to this email
// Create email
$email_subject = "Message from NUEVA.";
$email_body = "You have received a new message. \n\n".
"Fname1: $fname1 \nLname1: $lname1 \nEmail1: $email1 \nAddress1: $address1 \nCity1: $city1 \nCel1: $cel1 \nVeg_24176: $veg_24176 \nSoy_24176: $soy_24176 \nGlu_24176: $glu_24176 \nLac_24176: $lac_24176 \nChili_24176: $chili_24176 \nNuts_24176: $nuts_24176 \nChoco_24176: $choco_24176 \nComments1: $comments1 \n";
$headers = "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n";
$headers .= "From: contact#chipzcolombia.com\n";
$headers .= "Reply-To: $email1";
mail($to,$email_subject,$email_body,$headers);
return true;// Post message
ob_start();
exit(header("Location: http://example.com/thankyou.html"));
?>
The correct way of doing this is :
if(mail($to,$email_subject,$email_body,$headers)){
header("Location: http://example.com/thankyou.html");
exit();
}else{
return false;
}

Call to a member function verifyInput() on a non-object

It seems like a pretty common problem but I can't see to find the answer to my problem. I have been getting the dreaded "Call to a member function verifyInput() on a non-object" fatal error.
<?php
// empty values
$name = $email = $topic = $message = "";
$nameErr = $emailErr = $topicErr = $messageErr = "";
// link the name tags in HTML
$name = $_POST['name'];
$email = $_POST['email'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* name if */
if (empty($_POST['name'])) {
$nameErr = " * Name is required";
} else {
$name = test_input($_POST["name"]);
}
/* email if */
if ( ! empty($_POST['email'])) {
if ($email->verifyInput($_POST['email'], 6)){
$fill['email'] = $_POST['email']; //$fill is the array of values passed
} else {
$emailErr = " * Email is incorrect - Try Again";
}
} else {
$emailErr = " * Email is required";
}
// Form content
$recipient = "generic#gmail.com";
$subject = "Contact Form";
$formcontent = "4 Days of Fun.ca -
\n\n\nFrom: $name \n\nEmail: $email \n\nSubject: $topic
\n\n\nMessage: $message";
$mailheader = "From: $email - Subject: $topic \r\n";
//function to send mail
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you for contacting us, $name! \nWe will respond to you soon!";
}
//function for test_input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Validation of Inputs
function verifyInput($input, $type){
if ($type == 0) $pattern = '/^1$/';//just the number 1
elseif ($type == 1) $pattern = '/^[0-9]+$/';//just integers
elseif ($type == 2) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü]+$/';//just letters
elseif ($type == 3) $pattern = '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters
elseif ($type == 4) $pattern = '/^[A-Za-zÁ-Úá-ú0-9àÀÜü\s()\/\'":\*,.;\-!?&#$#]{1,1500}$/';//text
elseif ($type == 5) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü0-9\']+[A-Za-zÁ-Úá-úàÀÜü0-9 \'\-\.]+$/';//name
elseif ($type == 6) $pattern = '/^.+#[^\.].*\.[a-z]{2,}$/';//e-mail
elseif ($type == 7) $pattern = '/^((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))?[ -.]?([1-9][0-9]{3})[ -.]?([0-9]{4})$/';//brazilian phone
if (preg_match($pattern, $input) == 1) return true;
else return false;
}
?>
This is where the error is in question:if ($email->verifyInput($_POST['email'], 6)){ however I'm confused... I looked it up and everywhere states that the variable passed in ie $email may not be an object. But the variable is declared as an object here: $email = $_POST['email']; is it not? Or am I missing something big and obvious?
I just started learning PHP but I've got a lot of foundational knowledge with C++, so there are a lot of similarities I already get... This is not one of them.
Maybe I'm just not understanding the -> operator, or maybe it's a non-object because of something else?
Thanks in advance.
$email->verifyInput() would call the method (function) verifyInput() that is included in the class email, but here i can see that your function is not a member of any class, so if you need to call this fuction you just have to change:
if ($email->verifyInput($_POST['email'], 6)){...
to:
if (verifyInput($_POST['email'], 6)){...
And regarding your question about ->:
As i mentioned above -> is used after the instantiated class followed by the member or the method name. probably you need to read more about PHP, i would suggest this course, or you can jump directly to get a course about PHP OOP right here.

PHP Form Validation - Not Seeing Error Message [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is the form.
When I click submit without entering any required data, I do not see the error message, it just jumps to the top of the page. How can I fix this?
The PHP validation code added to the head of the form is:
if ($_POST['submit'])
{
/* Check all form inputs and strip unnecessary characters from data */
$quotetype = $_POST['quotetype'];
$name = check_input($_POST['name']);
$phone = check_input($_POST['phone']);
$email = check_input($_POST['email']);
$zipcode = check_input($_POST['zipcode']);
$appgender = $_POST['appgender'];
$appdob = check_input($_POST['appdob']);
$appsmoker = $_POST['appsmoker'];
$spousedob = check_input($_POST['spousedob']);
$spousesmoker = $_POST['spousesmoker'];
$child1gender = check_input($_POST['child1gender']);
$child1dob = check_input($_POST['child1dob']);
$child2gender = check_input($_POST['child2gender']);
$child2dob = check_input($_POST['child2dob']);
$child3gender = check_input($_POST['child3gender']);
$child3dob = check_input($_POST['child3dob']);
$child4gender = check_input($_POST['child4gender']);
$child4dob = check_input($_POST['child4dob']);
$currentcarrier = check_input($_POST['currentcarrier']);
$carriertype = $_POST['carriertype'];
$coverage = $_POST['coverage'];
$deductible = check_input($_POST['deductible']);
$premium = check_input($_POST['premium']);
$officecopay = check_input($_POST['officecopay']);
$rxcopay = check_input($_POST['rxcopay']);
$medconditions = $_POST['medconditions'];
$coverageamount = $_POST['coverageamount'];
$comments = $_POST['comments'];
if (is_array($quotetype))
$displayquotetype = implode(",",$quotetype);
$errorstring = ""; //default value of error string
/* Check for required fields */
if (empty($quotetype))
$errorstring = $errorstring."*Quote Type ";
if (!$name)
$errorstring = $errorstring."*Name ";
if (!$phone)
$errorstring = $errorstring."*Phone ";
if (!$email)
$errorstring = $errorstring."*Email ";
if (!$zipcode)
$errorstring = $errorstring."*Zip Code ";
if ($appgender=="choose")
$errorstring = $errorstring."*Gender ";
if ($appsmoker=="choose")
$errorstring = $errorstring."*Smoker";
if ($errorstring =="") {
$to = 'name#domain.com';
$subject = 'Quote Request';
$message = " <html><body> </body></html> ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$subject,$message,$headers);
header('location: messagesent.html');
exit();
}
}
/* Functions used */
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Form within index.php:
<form id="quote-form" name="quote-form" method="post" action="index.php">
...
<?php
if ($errorstring!="")
{echo "<span class='errormessage'>Please fill out the following fields: $errorstring<br></span>";}
?>
...
</form>
Add "name" attribute to your submit input.
<input type="submit" class="rounded-button button-shadow" id="submit" value="Submit" name="submit" />

PHP Feedback form Checkbox error

Ok here is a shortened version of the php for my contact form, (the checkboxes are not being sent through correctly)
<?php
//please fill this in at least!
$myemail = "";
$title = "Feedback Form";
if(isset($_POST['submit'])) { //form has been submitted
//set variables with filters
$cont_name = filter_var($_POST['cont_name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['cont_email'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['cont_phone'], FILTER_SANITIZE_STRING);
$first_time = filter_var($_POST['first_time'], FILTER_SANITIZE_STRING);
$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING);
function valid_email($str){
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;}
$errors = 0; //by default there are no errors
$trimcont_name = trim($cont_name);
if(empty($trimcont_name)){
//the name field is empty
$errors = 1; //tips off the error messages below
$errorcont_name = "The name field is empty"; //this error is displayed next to the label
}
if(!valid_email($email)) {
//email is invalid or empty
$errors = 1;
$erroremail = "The email address was not valid";
}
$trimphone = trim($phone);
if(empty($trimphone)){
//the phone field is empty
$errors = 1;
$errorphone = "The phone field is empty";
}
$trimfirst_time = trim($first_time);
if(empty($trimfirst_time)){
//the first_time field is empty
$errors = 1;
$errorfirst_time = "This field is empty";
}
$trimhear_about = trim($hear_about);
if(empty($trimhear_about)){
//the hear_about field is empty
$errors = 1;
$errorhear_about = "This field is empty";
}
if($spam != "") {
//spam was filled in
$errors = 1;
$errorspam = "The Spam box was filled in";
}
if($errors == 0) {
$sendto = $myemail;
$message = <<<DATA
DETAILS
Name: $cont_name
Email: $email
Phone: $phone
Was this the first time you have been to us?
$first_time
How did you hear about us?
$hear_about
DATA;
$headers = 'From: ' . $name . '<' . $email . '>';
if(mail($sendto, $title, $message, $headers)) {
//this is where it sends, using the php mail function
$success = true;
//set all the variables to blank to prevent re-submitting.
$cont_name = "";
$email = "";
$phone = "";
$hear_about = "";
$first_time = "";
} else {
$success = false;
}
} else {
$success = false;
}
}
?>
And the area not functioning correctly is
<fieldset>
<legend>How did you hear about us? <span class="phpformerror"><?php echo $errorhear_about; ?></span></legend>
<div><input type="checkbox" name="hear_about[]" value="Web" /> Web</div>
<div><input type="checkbox" name="hear_about[]" value="Newspaper" /> Newspaper</div>
<div><input type="checkbox" name="hear_about[]" value="Radio" /> Radio</div>
<div><input type="checkbox" name="hear_about[]" value="Driving" /> Driving Past</div>
<div><input type="checkbox" name="hear_about[]" value="Referal" /> Referal</div>
<div><input type="checkbox" name="hear_about[]" value="Other" /> Other</div>
</fieldset>
At the moment it will only come through displaying one of the variables if multiple variables are selected.
hear_about is an array and filter_var() does not handle arrays correctly. Instead use filter_var_array():
$hear_about = filter_var_array($_POST['hear_about'], FILTER_SANITIZE_STRING);
Remember that $hear_about is an array, and must be treated like one throughout your code (e.g. just using $hear_about won't work, it needs to be $hear_about[0], $hear_about[1], etc).
So for example in your trim line you would need something like:
foreach($hear_about as $key => $value) {
$trimhear_about[$key] = trim($value);
if(empty($trimhear_about[$key])){
//the hear_about field is empty
$errors = 1;
$errorhear_about[$key] = "This field is empty";
}
}
This will preserve the benefits of dealing with an array.
$_POST['hear_about'] is an array of values. You are handling it as a simple string!
I think you can solve simply replacing the line:
$hear_about = filter_var($_POST['hear_about'], FILTER_SANITIZE_STRING);
With:
$hear_about = filter_var(implode(', ', $_POST['hear_about']), FILTER_SANITIZE_STRING);
The implode function (doc) "transform" an array to a string by concatenating the array values with the given glue. So you can just concatenate selected "How did you hear about us?" options with a comma and then use the resulting string as the other data.

How to update my php contact email form script to redirect to a url after submit?

I am using this email script (at the bottom). where and what should I include to redirect to a different url after submit? Also, is this script safe enough?
<?php
error_reporting(E_ALL ^ E_NOTICE);
$my_email = "mymail#mail.com";
$from_email = "";
$continue = "/";
$errors = array();
// Remove $_COOKIE elements from $_REQUEST.
if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
// Validate email field.
if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{
$_REQUEST['email'] = trim($_REQUEST['email']);
if(substr_count($_REQUEST['email'],"#") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("#",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}
}
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}
// Check for a blank form.
function recursive_array_check_blank($element_value)
{
global $set;
if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{
foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}
}
}
recursive_array_check_blank($_REQUEST);
if(!$set){$errors[] = "You cannot send a blank form";}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}
if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}
// Build message.
function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com";
$message = stripslashes($message);
$subject = "FormToEmail Comments";
$subject = stripslashes($subject);
if($from_email)
{
$headers = "From: " . $from_email;
$headers .= PHP_EOL;
$headers .= "Reply-To: " . $_REQUEST['email'];
}
else
{
$from_name = "";
if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);}
$headers = "From: {$from_name} <{$_REQUEST['email']}>";
}
mail($my_email,$subject,$message,$headers);
?>
after mail() add
header("Location: anotherlocation.php");
However to your second question, I'd use $_POST instead of $_REQUEST

Categories