8 Replies - 643 Views - Last Post: 05 October 2011 - 12:53 PM Rate Topic: -----

#1 wonderlhily  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 19-August 11

Error messages dont appear

Posted 05 October 2011 - 09:44 AM

<?php
session_start();
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
    $name = trim(strip_tags($_POST['name']));
    $email = trim(strip_tags($_POST['email']));
    $comments = trim(strip_tags($_POST['comments']));
    $secure = strtoupper(trim(strip_tags($_POST['secure'])));
    $match = $_SESSION['captcha']; // the code on the image

// input error checking
    if ($name=="") {
        $err.= "Please provide your name<br/>";
    }
    if (!$email) {
        $err.= "Please provide your email address<br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address.<br/>";
        }
    } 
    if ($comments=="") {
        $err.= "Please provide comments<br/>";
    }
    if (!$secure) {
        $err.= "No security code entered<br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch<br/>";
    }
    if ($err=="") {
    // success - input passed all tests
    echo "What you do with success is up to you.";
    exit();
    }
}
?>
<head>
<title>Trolls go away</title>
<style type="text/css">
body,td {
	font-family:arial, helvetica, sans-serif;
	background:#fff;
	color:#000;
	font-size:12px;
}
input, textarea {
	background:#eee;
	color:#000;
	font-size:12px;
	border:1px solid #000;   
 }
</style>
</head>
<body>
<?php
if ($err!="") {
    echo "<strong>Form Error(s)</strong><br/>";
    echo "<font color='#cc3300'>". nl2br($err). "</font><br/>";
}
?>

<form name="captcha" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table cellpadding="3" cellspacing="2" style="border:1px dotted #666;">
<tr>
<td>Name:</td><td><input type="text" name="name" value="<?php echo $_POST['name'];?>"/></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" value="<?php echo $_POST['email'];?>"/></td>
</tr>
<tr>
<td valign="top">Comments:</td><td><textarea rows="5" columns="30" name="comments"><?php echo $_POST['comments'];?></textarea></td>
</tr>
<tr>
<td>Security Code</td><td><input type="text" name="secure"/></td>
</tr>
<tr>
<td><img src="captcha_image.php" alt="security image" border="0"/></td><td><input type="submit" name="submit" value="Send"/></td>
</tr>
</table>
</form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Error messages dont appear

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,064
  • Joined: 08-June 10

Re: Error messages dont appear

Posted 05 October 2011 - 10:03 AM

do you expect errors? (weird question, but how am I supposed to know whether there should be errors?)

PS. easiest email check:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {}

Was This Post Helpful? 0
  • +
  • -

#3 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: Error messages dont appear

Posted 05 October 2011 - 10:31 AM

You can also use
if (empty($blah)) {
// blah
}


To check to see if a variable is empty :) You could also use isset. Also, I don't see where you begin you HTML tag (the reason I'm stating that is because I see where you end it). I also see no doctype. It's also generally frowned upon using tables for layout, forms aren't tabular data.
Was This Post Helpful? 0
  • +
  • -

#4 wonderlhily  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 19-August 11

Re: Error messages dont appear

Posted 05 October 2011 - 11:05 AM

sorry my mistake.. what i mean is the input error checking
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Error messages dont appear

Posted 05 October 2011 - 11:34 AM

I tried your code. And while I had to turn the PHP errors off to do so, it seems to work just fine. All the fields generate the expected error messages. Are you sure your problem is with the code you gave us?



With that said, there are a couple of other things worth mentioning...

First of all, you really really need to turn on the PHP error reporting. That code should be generating all sorts of warnings, which should not be ignored. The simplest way to do this is to add the following to the top of your code, but these should really be set in your php.ini on development environments.
ini_set('display_errors', true);
error_reporting(E_ALL | E_NOTICE);



And second, it's generally a bad idea to use a submit button to test if a form has been submitted. They are not guaranteed to be sent with the form. It's the presence of the data you want to check, not some button that may or may not have been pressed.
// Don't do:
if (isset($_POST['submit'])) ...

// Rather do:
if (isset($_POST['data1'], $_POST['data2'], $_POST['dataN'])) ...


Was This Post Helpful? 0
  • +
  • -

#6 wonderlhily  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 19-August 11

Re: Error messages dont appear

Posted 05 October 2011 - 12:27 PM

undefined variable err

what should i do?
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,064
  • Joined: 08-June 10

Re: Error messages dont appear

Posted 05 October 2011 - 12:38 PM

check if the variable is there. typically done through isset(). you can also fix it otherwise, but that depends on the use case.
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Error messages dont appear

Posted 05 October 2011 - 12:40 PM

One of two things.

  • Define the variable.

  • Replace the variable with one that is defined.


You can not print a value that doesn't exist. So if you do <?php echo $err; ?>, the $err variable must exist or PHP won't be able to print it, and it will print this lovely error.
Was This Post Helpful? 0
  • +
  • -

#9 wonderlhily  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 19-August 11

Re: Error messages dont appear

Posted 05 October 2011 - 12:53 PM

thank you for helping me.. i really appreciate it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1