m1k3yb0y Posted April 16, 2012 Share Posted April 16, 2012 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. 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> Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/ Share on other sites More sharing options...
batwimp Posted April 16, 2012 Share Posted April 16, 2012 Are those two blocks of code in different files? If so, how are you passing the $error and $mission information to the file that checks them? I feel there may not be enough code to troubleshoot this. Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/#findComment-1337944 Share on other sites More sharing options...
batwimp Posted April 16, 2012 Share Posted April 16, 2012 Sorry, I meant $error and $missing Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/#findComment-1337947 Share on other sites More sharing options...
m1k3yb0y Posted April 17, 2012 Author Share Posted April 17, 2012 $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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/#findComment-1338155 Share on other sites More sharing options...
Drummin Posted April 17, 2012 Share Posted April 17, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/#findComment-1338194 Share on other sites More sharing options...
m1k3yb0y Posted April 17, 2012 Author Share Posted April 17, 2012 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. 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. :'( Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/#findComment-1338253 Share on other sites More sharing options...
m1k3yb0y Posted April 18, 2012 Author Share Posted April 18, 2012 uhhhhh.....does anyone know how to get rid of that array text that shows up on my page and disappears when the error message shows up?! Quote Link to comment https://forums.phpfreaks.com/topic/261062-form-error-message-not-showing-up/#findComment-1338557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.