开发者

My FORM problem issues

开发者 https://www.devze.com 2023-01-01 08:04 出处:网络
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.

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 = '';
}


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


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消