views:

20

answers:

2

H I'm using php contact form from http://phpfmg.sourceforge.net/home.php. I thought I'd add an onfocus effect so when i click in the fields the value dissappears automatically.

But when I submit say if haven't filled in my requried fields I get this the values appearing again like http://s647.photobucket.com/albums/uu199/judibluebottles/YETI%20images/?action=view&current=form_issue.jpg

<input type="text" class="text_box" onfocus="if(this.value=='Telephone')this.value='';" value="TelephoneTelephone" id="field_5" name="field_5">
A: 

You'll want to use an onsubmit handler on the form that does the same thing the onfocus does: Clears the text out of the box if it's the default text. That way, the fields will be blank when they're submitted.

To do that, I'd put some structure in place to avoid duplicating code. Completely off the cuff:

var Placeholders = {
    "field_5": "Telephone",
    // ...and the other fields...
};

function clearFieldPlaceholder(field) {
    var placeholder;

    placeholder = Placeholders[field.name];
    if (placeholder && field.value == placeholder) {
        field.value = "";
    }
}

function formSubmit(form) {
    var index;

    for (index = 0; index < form.elements.length; ++index) {
        // You may want to filter here a bit, e.g., check if it's
        // a text field
        clearPlaceholder(form.elements[index]);
    }
}

onfocus for fields:

<input ... onfocus="clearPlaceholder(this);" ...>

onsubmit for form:

<form ... onsubmit="formSubmit(this);" ...>
T.J. Crowder
WOw Thanks T J where do I put this in the code? I have two files a form.php and form.lib.php file
judi
In a `script` tag on the page, e.g.: `<script type='text/javascript'>CODE HERE</script>` or in a separate file you link to the page via `<script type='text/javascript' src='FILENAME'></script>`
T.J. Crowder
Here is an example on my site http://artygirl.co.uk/form.html The php looks really complicated?
judi
A: 

I've given up doing complicated php contact forms unless someone has a miracle one that can do almost anything? For Wordpress contactform 7 is the easiest i've come across.

Would be great is someone makes an advanced plugin for pixie cms :)

judi