tags:

views:

27

answers:

2

i have a form, and i want to have it be limited at 10 characters minimum. that is no problem, but what i want to do is echo the error at the top of the page, which is being included, so i cant just do:

echo '<div class="error">Error</div>';

i want to have a designated div that is empty (will be on the included header page), but when there is an error it gets filled with the error text to output. anyone know how to do this not using sessions or cookies?

A: 

This is a clear use-case for javascript. PHP is strictly a server-side language; that is, the code you write is executed on the server and not the client. Javascript, on the other hand, is run inside the user's browser. So say you create a div like so: <div id="error_msg" />. Then you can write a snippet of javascript code that looks like this:

function display_error () {
    var err_msg_div = getElementById("error_msg");
    err_msg_div.innerHTML = "Error";
}

You would place this code in script tags at the top of your page inside the tags. More information on javascript form validation can be found here: http://www.w3schools.com/js/js_form_validation.asp

Hope this helps.
-tjw

Edit: if this isn't exactly what you're looking for, you might want to tag this post with 'javascript' to get more people who know about js form validation to answer the question.

Travis J Webb
A: 
<div id="error_msg" /></div>
<script>
function display_error (text) {
    var err_msg_div = getElementById("error_msg");
    err_msg_div.innerHTML = text;
}
display error('Error: your text here..');
</script>
apis17