Jump to content

Getting error on php contact form


foxclone
Go to solution Solved by ginerjm,

Recommended Posts

I'm getting the following error on a contact form I'm trying to implement:

Fatal error: Uncaught Error: Undefined constant "secretcode" in /home/foxclo98/test.foxclone.com/contact.php:33 Stack trace: #0 {main} thrown in /home/foxclo98/test.foxclone.com/contact.php on line 33

Here's the full code of the contact form:

<?php
$errorlevel=error_reporting();
error_reporting($errorlevel & ~E_WARNING);
//...code that generates notices
error_reporting($errorlevel);
// initialization
session_start();

$email_contact = "help@foxclone.com";
$email_website = "webmaster@foxclone.com";

// definition of permitted types/subject/category. use to dynamically build the option list,
// pre-selecting any existing choice, and used in the validation logic
$permitted_types = ['Questions', 'Report Problem', 'Suggestion', 'Other', 'Website Problem'];
$secretcode = ['nospam'];
$post = []; // array to hold a trimmed working copy of the form data
$errors = []; // array to hold user/validation errors


// post method form processing
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// trim all the data at once
	$post = array_map('trim',$_POST); // if any of the fields are arrays, use a recursive call-back function here instead of php's trim function

	// inputs: name, email, type/subject/category, message - all required
		
	// validate the inputs
	$errors = [];
	//Other validation stuff.
	if ($post['secretcode'] != 'nospam')
	{
		$errors[secretcode] = 'You did not enter the correct secret code.';
	}
	
	
	if($post['name'] === '')
	{
		$errors['name'] = 'Name is required.';
	}
	if($post['email'] === '')
	{
		$errors['email'] = 'Email is required.';
	}
	else
	{
		// while it is true that the following email format validation will produce an error
		// for an empty value, you should specifically tell the visitor what is wrong with what
		// they submitted
		if (false === filter_var($post['email'], FILTER_VALIDATE_EMAIL))
		{
			$errors['email'] = 'The Email Address you entered does not appear to be valid.';
		}
	}
	if($post['type'] === '')
	{
		$errors['type'] = 'You must select a Type/Subject/Category.';
	}
	else
	{
		// you will only see the following error due to a programming mistake or someone/something submitting their own values
		if(!in_array($post['type'],$permitted_types))
		{
			$errors['type'] = 'The selected Type is invalid.';
			// you would want to log the occurrence of this here...
		}
	}
	if($post['message'] === '')
	{
		$errors['message'] = 'Message is required.';
	}
	else
	{
		if(strlen($post['message']) < 10)
		{
			$errors['message'] = 'The Message must be at least 10 characters.';
		}
	}

	// if no errors, use the submitted data
	if(empty($errors))
	{
         
		// apply htmlentities() to help prevent cross site scripting when viewing the received email in a browser
		$formcontent = htmlentities("From: {$post['name']}\r\nEmail: {$post['email']}\r\nSubject: {$post['type']}\r\nMessage: {$post['message']}", ENT_QUOTES);

		if ($post['type'] === "Website Problem")
		{
			$recipient=$email_website;
		}
		else
		{
			$recipient=$email_contact;
		}
		$email = $post['email'];
		// add $post['email'] as a Reply-to: header if desired, it is one, valid email address at this point
		$mailheader = "From: $email\r\n" ;
		$mailheader .= "Cc: $email\r\n";

		if(!mail($recipient, $post['type'], $formcontent, $mailheader))
		{
			// an error
			// setup the user error message
			$errors['mail'] = 'The email could not be sent, the site owner has been notified.';
		
			// system error handling goes here... - datatime, get the last error message, include the mail parameter values...
			// at this point, all parameters are either an internal value, have been validated they they are just an expected 
			// value/format, or have had htmlentities() applied.
			
		}
	
		// if no errors at this point, success
		if(empty($errors))
		{
			$_SESSION['success_message'] = "Mail Sent. Thank you {$post['name']}, we will contact you shortly..";
			die(header("Refresh:0"));
		}
	}
}

// html document starts here...
?>


<?php
// display any success message
if(!empty($_SESSION['success_message']))
{
	// for demo purposes, just output it as a paragraph. add any markup/styling you want
	echo '<p>';
	echo htmlentities($_SESSION['success_message'], ENT_QUOTES);
	echo " - <a href='index.php#home' style='color:#ff0099;'> Return Home</a>";
	echo '</p>';
	unset($_SESSION['success_message']);
}
?>

<?php
// display any errors
if(!empty($errors))
{
	// for demo purposes, just output them as a paragraph. add any markup/styling you want
	echo '<p>'; echo implode('<br>',$errors); echo '</p>';
}
?>

<?php
// (re)display the form here..., re-populating the fields with any existing values


?>

<?php require_once("header.php");?>

<style>
  input, select {
  width: 20rem;
  line-height:30px;
  border:2px solid #2f496e;
  padding: 0;
  margin: 0;
  height: 30px;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font: 500 1rem sans-serif;
  background: #fff;
}

.input {
  text-indent: 3px;
}
</style>

</head>
<body>
  <?PHP require_once("navbar.php"); ?>
<!--******************
*      CONTACT       *     
*******************-->

<div class="head__h1"> Need help? Have a suggestion? Why not send us an email?</div>     
  <div class="subtext"> You'll receive a copy of your inquiry for your records </div>
    <div class ="download">

	  <div class="cont__row" style="background-color: #d9b44a;">
         <div class="cont__column">
              
		       <form method="POST">
			   
			<label>Secret Code:</label><br>
            <input type="text" name="secretcode" id="secretcode" placeholder="Type nospam here">
              <br> <br>

            <label>Name</label><br> 
            <input type="text" name="name"><br> <br> 
                
            <label>Email</label><br> 
            <input type="email" name="email"><br> <br> 
          
        <label>Select a Category</label> <br> 
            <select name="type" id="category" size="1">
                <option value=''>                 </option>
                <option value='Questions'>Questions</option>
                <option value="Report Problem">Report Problem</option>
                <option value='Suggestion'>Suggestion</option>
                <option value='Other'>Other</option>
                <option value="Website Problem"> Website Problem</option>
            </select>
         
            </div>
        
            <div class="cont__column">
            <label>Message</label><br> 
            <textarea name="message" rows="10" cols="50" style="font: 500 1rem sans-serif"></textarea><br> <br> 
          
          
            <div class="button">
            <input type="image" id="myimage" src="images/email1.jpg" style="height:40px; width:160px;"/>
                
        </form>
      </div>
    </div>
  </div>
  </div> 
  <?PHP require_once("footer.php"); ?>

I'd appreciate ssome help on this.

Link to comment
Share on other sites

Quote

$errors[secretcode] = 'You did not enter the correct secret code.';

You have an array reference without quotes on it.  It thinks that's a constant when it is supposed to be an index value.

The error message should have pointed this out to you.

Edited by ginerjm
Link to comment
Share on other sites

48 minutes ago, foxclone said:
<?php
$errorlevel=error_reporting();
error_reporting($errorlevel & ~E_WARNING);
//...code that generates notices
error_reporting($errorlevel);

Not sure what you are attempting with the above but since the setting is only good for the duration of the script's execution, you really s/b thinking of doing an E_ALL as well as a display on the client's screen to aid in debugging your script easier.

error_reporting(E_ALL);
ini_set('display_errors', '1');

 

Link to comment
Share on other sites

  • Solution
1 hour ago, foxclone said:
$secretcode = ['nospam'];

The above line makes an array having one element (0) whose value is 'nospam'.

What are you planning to do with that array? I don't see it referenced but it still seems like a funny thing to be doing.  You are doing a check for the entry in this field but not with the array you have created.  Perhaps you simply want to create it near the top of your script as a simple static variable and not an array?

and then maybe you want to change this

    if ($post['secretcode'] != 'nospam')
To be 

if ($post['secretcode'] != $secretcode)

BTW - You seem to be doing this thing with your own version of the $_POST array for no good reason.  Is this something you read about somewhere that is supposed to be of help to your algorithm?  Why not just use $_POST?  It's a global array that you control and is accessible from your entire script.

 

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.