Jump to content

Error reporting formatting


gsmith

Recommended Posts

I have validation on some pages that will return an error. Currently my scripts print the error message, and include the original page so the user can re-enter the items....so my error message is at the top of the previously displayed page.Is there a way to format where that error message goes? Could I get it to display the previous page and put the error message next to or under the incorrect item...and how would I do that?

Link to comment
Share on other sites

There are many ways!This is a php forum, but my suggested solution is javascript - that way you don't have to actually reload the page! One way would be to just add some span tags or something similar to your page next to the fields you are validating. Have the span tag contain your error message. Assign the id attribute to something meaningful - if the message is related to the password field name it "password_message". Set the span tag's class attribute to something meaningful - for example "hidden_message". Then when validating change the class of the desired span tag to something like "visible_message". Use css to define styles something like this:

.visible_message{display:inline;color:#cc0000;}.hidden_message{display:none;}

Use javascript for changing the class. You could use a function similar to this:

function show_message(message_field_id){	message_field = document.getElementById(message_field_id);	message_field.className = "visible_message";}

You should set the "onsubmit" attribute of your form to something like

<form action="submitpage.php"	onsubmit="return validate_form()"	method="post">

Make sure the validate_form() function returns true if everything passes validation, and false otherwise.There may of course be different and better ways. But this way certainly works...... I thought of posting a whole working example... but figured that might take all your fun away... you can see a simple example here -> http://sbrownii.tripod.com/form.html

Link to comment
Share on other sites

If you use php, you just need to know where you want the messages too show upYou could use values in the query string to tell you which fields need a message next to them.When your php runs it can just check when it gets to each field whether it needs to print a message for it or not.For example... when you search on most forums, it highlights the words in the post that match your search terms.Look here-> http://w3schools.invisionzone.com/index.ph...471&hl=selectorI searched for selector, clicked the link to that post and look at the url... it says "hl=selector". This tells the page to highlight the word "selector".If you look at the source will see a style entry like this

.searchlite{	background-color: yellow;	font-weight: bold;	color: red;}

Search for "searchlite" OR "selector" in the source and you will find something like this:

<span class='searchlite'>selector</span>

for the fun of it just change the url of this topic so it is like thishttp://w3schools.invisionzone.com/index.ph...opic=2534&hl=onChange "on" to any word you want...I'm sure I didn't have to talk about it so much... just wanted to show that what you are trying to do has been done many times.In your case, you don't so much care to change the class of a word, you just need to decide to show it, or not, using information passed through the query stringPerhaps you call the previous page like this to show that the password field needs to be corrected:

mysite.com/mypage.php?password=error

Then in your php you could do something like this

<input type="text" name="email" value=""/><?phpif (isset($_GET['password'])){  echo "<span class='error_message'>Required!</span>";}?><input type="password" name="password" value=""/>

I don't know if that's the kind of thing you are looking to do...You can use something like that, but you would lose the form info between pages unless you pass it back and forth. Passing user info using the query string is not very good as far a security goes....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...