tags:

views:

42

answers:

3

So i have this in my form:

<textarea onfocus="javascript:clearContents(this);" rows="5" cols="40" id="comment" name="comment">Skriv hvorfor du vælger at stemme ja/nej. Skal indeholde detaljer, kritik/råd.  Klik for at skrive</textarea><br />
Ja: <input type="checkbox" value="Y" id="SCvote" name="SCvote"> eller Nej: <input type="checkbox" value="N" name="SCvote"> 

Now onfocus (when you click on the box) it clears the text "Skriv hvorfor...", but if you dont write anything and still submit it, my php code thinks it contains something and inserts that text "Skriv hvorfor...".

And another issue is the checkboxes. If i dont pick anything, it inserts "Y", even if i pick the other "N" it does "Y", and i dont want it to be possible that you can check both of them. how do i do?

definition of clearcontents:

function clearContents(element) {
  element.innerHTML = '';
}
A: 

Here, this will do it:

<html>
<head>
<script type="text/javascript">
var defaultTxt = 'Skriv hvorfor du vælger at stemme ja/nej.
Skal indeholde detaljer, kritik/råd. Klik for at skrive';
</script>
</head>
<body onload="javascript:document.getElementById('comment').value = defaultTxt;">
<form method="POST" action="response.php" >
<textarea onfocus="javascript:document.getElementById('comment').value='';" rows="5"
cols="40" id="comment" name="comment">
</textarea><br />
Ja: <input type="radio" value="Y" name="vote">
eller Nej: <input type="radio" value="N" name="vote">
<input type="submit"/>
</form>
</body>
</html>

By changing to radio buttons, you can only get a yes or no answer on the post. I'm not completely sure, but I suspect that by not actually changing the elements value, the default text still got posted. I'm probably going overboard with all the code I wrote, but it does work.

maximus
function clearContents(element) { element.innerHTML = '';}
Karem
A: 

You can turn your checkbox into a radio button(use type="radio") instead since you only need 1 answer and not both. Also, in your php code, check first if the value of comment = "kriv hvorfor du vælger at stemme ja/nej. Skal indeholde detaljer, kritik/råd. Klik for at skrive" if it is, put a blank comment or catch the error and tell the user to type a comment

corroded
Ok ive did that but still why does it do "Y" automaticly without picking, and the same when i press Nej "N"?
Karem
don't put an id on your Y radio button. if you need to put an id, make sure its different from your radio button's name. like id="SCVoteYes" or something
corroded
Its like this right now: Ja: <input type="radio" value="Y" id="SCvote" name="vote"></input> Nej: <input type="radio" id="SCvote" value="N" name="vote"> </input> it still just inserts Y when i press on the N radiobuttion. And i dont want it to insert Y if i didnt choose anything
Karem
don't make your IDs the same. you still made your IDs the same. just make SCVoteYes and SCVoteNo as your ids or something. making it the same just gets the first value it finds when you get your variable, in this case Y
corroded
A: 

For the checkboxes if you want to only pick one of the two then you need to useradio` buttons

For the textarea, you should check on submit if the text has changed and if not clear it .. ie.

<form onsubmit="if (!this.comment.cleared) clearContent(this.comment); return true;">
<textarea onfocus="this.cleared=true;javascript:clearContents(this);" rows="5" cols="40" id="comment" name="comment">...</textarea>
Gaby
I tried your example it wont work, it just inserts "..."
Karem