Print error message next to input box


#1

Hi, i started working with php about a week ago so please go easy on me. I’m writing a little address book script so that i get to grips with the basic ideas in PHP. I wanted to check my input boxes so that if a valid email isn’t entered it will spit out an error and not submit the data to the database until correct info has been entered.

I’ve managed to do the regex checking that will tell me if a valid email has been entered or not but the script still carries on regardless and prints the error once the data has been submitted. I’m guessing i need to print the error in the html bit of the code so that it’s actually displayed next to the box it’s just how do i go about doing it. Please could someone out there help me as i’d love to get this working.

Code
[php]<?php

if (isset($_POST[‘submitted’])) {

$name = mysql_escape_string(trim($_POST[‘name’]));
$address = mysql_escape_string(trim($_POST[‘address’]));
$postcode = mysql_escape_string(trim($_POST[‘postcode’]));
$telephone = mysql_escape_string(trim($_POST[‘telephone’]));
$fixedemail = mysql_escape_string(trim($_POST[‘email’]));
$imglocation = mysql_escape_string(‘http://www.mysite.com/uploader1/images/’);

// This bit of code checks the form for correctly entered information
if (eregi(’^([-a-z0-9.]+)@([-a-z0.9]+.+[a-z]{2,6})$’,$fixedemail,$email))
{ print “” ;}
else { print "You entered an invalid email address. Please enter username@domain
" ; }

  // This bit puts the file in the $file variable into the folder on the server and changes its name so it isn't overwritten
	 $file_name = uniqid("img").".jpg";

      if ($file_name !="") {
        copy ("$file", "/var/www/vhosts/mysite.com/httpdocs/uploader1/images/$file_name")
	      or die ("Sorry there was a problem");}

 
  // This is the SQL part of the code
  $dbid = mysql_connect ('localhost', 'address', 'password');
          mysql_select_db('addresses',$dbid) 
          or die ("Cannot find database");

  $query = "INSERT INTO `book` (`aid`, `name`, `address`, `postcode`, `telephone`, `email`, `picture`) VALUES ('', '$name', '$address', '$postcode', '$telephone', '$email', '$imglocation$file_name')";
  $result = mysql_query($query,$dbid) 
   or die("INSERT error:".mysql_error());
 
  echo 'Row inserted and image uploaded';
  exit;

}
?>

Data submission - db version




Name:
Address:
Postcode:
Telephone:
Email:
Image:


[/php]

#2

I can tell you have a lot covered already. The trick is to echo/print the message at the right time. You could use a boolean variable to determine whether or not a script should save data, or output an error message:

[php]

<?php $validEmail = true; if (eregi("YOUR_REGEX")) { $validEmail = false; } if ($validEmail) { // Do your file/database stuff here } ?> <?php if (!$validEmail) { echo "No valid email provided."; } ?>

[/php]

Offtopic: I’d like to point out that you’re mixing up HTML and XHTML (self-closing tags), and are missing a Doctype declaration (which is necessary for XHTML). Also, your inputs are missing the mandatory attribute ‘type’. See more information here, and use this tool to validate your (X)HTML.


#3

Thanks for the pointers. I’ve added a doctype and have removed all the XHTML elements from the code. I’ve checked my html with the WC3 html validator and it’s now all fine. I’ll let you know how i get on with the error message.

Thanks very much for your help pointers. I now know what direction i should be heading in.


#4

No problem. Just here to help :slight_smile:


#5

I was wondering if you could offer me some more help. So far things have come quite naturally but i can’t get my head around this.

I’ve tried what you suggested or something similar i’m just not getting desired results.

Could you explain it a bit more for me. From what i can see, we’re setting $validEmail as true, then with the [php] if (eregi(“YOUR_REGEX”)) {
$validEmail = false;
}[/php] code are we not setting it to false? Surely we want it to be true after the REGEX?

This has left me scratching my head a bit. :-(


#6

We only want it to be true if the regex validates the submitted email address to be true. Otherwise, you’ll want it to be false. I’m not sure when your regex returns true: on a valid, or invalid email address? If on a valid one, then you should turn it around:

[php]
$validEmail = false;
if (REGEX) {
$validEmail = true;
}
[/php]

Hope I’ve cleared it up a bit for you :slight_smile: