Jump to content

Form error message not showing up?!


m1k3yb0y

Recommended Posts

As some of y'all may not know, I am new to this php coding stuff so if anything does not work the way I want it to, it goes to the forum. And this is the case when just after I solved a problem with my first contact form, I run into another problem again.  :shrug: My latest problem involves the form error thing that tells you that you did not enter any required fields. After I added a error message thing to my contact page and tested it, it doesn't show up.

Oh and here's the code:

<?php if ($missing || $errors) { ?>
<div id="maincontent">
<p class="warning">You did not enter the required information. Please try again.</p></div>

 

Here is what makes up my contact form.

<?php $errors = array();
$missing = array();
if (isset($_POST['send'])) {
$to = '[email protected]';
$subject = 'New Feedback Received on MikeyTateLive Productions website';
$expected = array('name', 'email', 'comments');
$required = array('name', 'comments');
require ('/www/zymichost.com/m/t/l/mtlproductions/htdocs/processmail.inc.php'); } ?>
<div id="wrapper">
    <div id="maincontent">
      <p>*=required</p>
    </div>
</div>
<form id="feedback" method="get" action="">
  <p>
    <label for="name">*Name:</label></br>
    <input name="name" id="name" type="text" class="formbox">
  </p>
  <p>
    <label for="email">Email:</label></br>
    <input name="email" id="email" type="text" class="formbox">
  </p>
  <p>
    <label for="comments">*Comments:</label></br>
    <textarea name="comments" id="comments" cols="60" rows="8"></textarea>
  </p>
  <p>
    <input name="send" id="send" type="submit" value="Send"
  </p>
  </form>

Link to comment
https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/
Share on other sites

$missing is in my processmail.inc.php file.

Here's what the file looks like:

<?php
foreach ($_POST as $key => $value) {
  $temp = is_array($value) ? $value : trim($value);
  if (isset($temp) && in_array($key, $required)) {
    $missing[] = $key;
  }
    elseif (in_array($key, $expected)) {
      ${$key} = $temp; ?>

May reasons the the error messages weren't showing, POST vs Get being one of them.  Here's another example of what you could do. Untested.

<?php
$missing = array();
if (isset($_POST['send'])) {
$to = '[email protected]';
$subject = 'New Feedback Received on MikeyTateLive Productions website';

$required = array('name', 'comments');

foreach ($_POST as $key => $value) {
$value = trim($value);
	if (empty($value)){
		if (in_array($key, $required)) {
		$missing[] = $key;
		}
	}		
}
	if (!empty($missing)) {
	$errors='<div id="maincontent">';
		foreach ($missing as $m){
		$errors.='<p class="warning">You did not enter the required information for '.$m.'. Please try again.</p>';
		}
	$errors.='</div>';
	}else{
	$message="Re: New Feedback Received on MikeyTateLive Productions website"."\n\n";

	$message.="From: {$_POST['name']}\n";
	$message.="Email: {$_POST['email']}\n\n";

	$message.="Comments:\n";
	$message.="{$_POST['comments']}\n";

	$message.="Thank you,\n";
	$message.="MikeyTateLive Productions\n";

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

	}	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Feedback form</title>       
</head>
<body>
<div id="wrapper">
<?php if (isset($errors)){ echo $errors;} ?>    
</div>
<form id="feedback" method="post" action="">
  <p>
    <label for="name">*Name:</label></br>
    <input name="name" id="name" type="text" class="formbox" value="<?php if (isset($_POST['name'])){ echo $_POST['name']; }?>" />
  </p>
  <p>
    <label for="email">Email:</label></br>
    <input name="email" id="email" type="text" class="formbox" value="<?php if (isset($_POST['email'])){ echo $_POST['email']; }?>" />
  </p>
  <p>
    <label for="comments">*Comments:</label></br>
    <textarea name="comments" id="comments" cols="60" rows="8"><?php if (isset($_POST['comments'])){ echo $_POST['comments']; }?></textarea>
  </p>
  <p>
    <input name="send" id="send" type="submit" value="Send" />
  </p>
  </form>
</body>
</html>

 

May reasons the the error messages weren't showing, POST vs Get being one of them.  Here's another example of what you could do. Untested.

<?php
$missing = array();
if (isset($_POST['send'])) {
$to = '[email protected]';
$subject = 'New Feedback Received on MikeyTateLive Productions website';

$required = array('name', 'comments');

foreach ($_POST as $key => $value) {
$value = trim($value);
	if (empty($value)){
		if (in_array($key, $required)) {
		$missing[] = $key;
		}
	}		
}
	if (!empty($missing)) {
	$errors='<div id="maincontent">';
		foreach ($missing as $m){
		$errors.='<p class="warning">You did not enter the required information for '.$m.'. Please try again.</p>';
		}
	$errors.='</div>';
	}else{
	$message="Re: New Feedback Received on MikeyTateLive Productions website"."\n\n";

	$message.="From: {$_POST['name']}\n";
	$message.="Email: {$_POST['email']}\n\n";

	$message.="Comments:\n";
	$message.="{$_POST['comments']}\n";

	$message.="Thank you,\n";
	$message.="MikeyTateLive Productions\n";

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

	}	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Feedback form</title>       
</head>
<body>
<div id="wrapper">
<?php if (isset($errors)){ echo $errors;} ?>    
</div>
<form id="feedback" method="post" action="">
  <p>
    <label for="name">*Name:</label></br>
    <input name="name" id="name" type="text" class="formbox" value="<?php if (isset($_POST['name'])){ echo $_POST['name']; }?>" />
  </p>
  <p>
    <label for="email">Email:</label></br>
    <input name="email" id="email" type="text" class="formbox" value="<?php if (isset($_POST['email'])){ echo $_POST['email']; }?>" />
  </p>
  <p>
    <label for="comments">*Comments:</label></br>
    <textarea name="comments" id="comments" cols="60" rows="8"><?php if (isset($_POST['comments'])){ echo $_POST['comments']; }?></textarea>
  </p>
  <p>
    <input name="send" id="send" type="submit" value="Send" />
  </p>
  </form>
</body>
</html>

 

 

Thanks man!! It worked! But the array text is actually showing up on the page now. :facepalm: It'll go away after the error message shows up. But how do I permanently get rid of it? AND I gotta buy the extra mail thing that my free hosting thing has. :'(

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.