Click to See Complete Forum and Search --> : Help with radio/textbox
KnightWolfJK
03-14-2003, 03:43 PM
On this page (http://www.planetjk.com/report_error.shtml) I want to create a JS alert that will force the user to enter data into the "Other Details" box if they choose the "Other" radio button under 'Type of Error'.
The problem is that I'm using a cgi script to validate this form, and I can't dynamically change the required status of the "other details" box.
Would this work?
1)Set the "Other Details" box as required, causing the CGI to validate data
2)For the "Other details" TEXTAREA box, use a value of "_" to bypass the CGI validation under normal conditions
3)Use JS to clear out any data in the "Other details" TEXTAREA box ONLY IF the user chooses the 'Other' Radio button, causing the CGI to prompt the user to enter info into the "Other Details" TEXTAREA box?
If that would work, does anyone have any suggestions on the JS code?
I'd rather do it with an Alert box telling the user "You entered your Type of Error as Other; Please specify this error in the 'Other Details' box" but I'm not sure if an Alert box is compatible with the CGI validation.
Thanks in advance...
Try this:
<html><head>
<script>
function Test(){
otherinfo = document.Report_Error.OtherInfo.value;
if(document.Report_Error.elements[4].checked){
if(otherinfo=="" || otherinfo==" ") {
alert("You must put something in the textarea to specify what \"other\" is.");
document.Report_Error.OtherInfo.focus();
} } }
</script></head><body>
<form method="post" name="Report_Error" action="cgi-bin/FormMail.cgi">
<INPUT TYPE="radio" NAME="Error_Type" VALUE="Broken_Link">Broken Link<BR>
<INPUT TYPE="radio" NAME="Error_Type" VALUE="Broken_Image">Broken Image<BR>
<INPUT TYPE="radio" NAME="Error_Type" VALUE="Javascript Error">Javascript Error<BR>
<INPUT TYPE="radio" NAME="Error_Type" VALUE="Broken_Menu">Broken Menu<BR>
<INPUT TYPE="radio" NAME="Error_Type" VALUE="Other">Other
<br>
<textarea name="OtherInfo" wrap="physical" cols="40" rows="3"></textarea><br>
<input type=button value=" Submit " onClick="Test()">
</form></body></html>
KnightWolfJK
03-14-2003, 08:21 PM
I added the code to the HEAD and I added onClick="Test()" to my submit button- nothing happened. I was able to successfully send the form while having selected the Other radio button.
I'm by no means the expert here, but I don't see anything telling the script to look at Error_Type.Other. Could that be part of the problem?
Also, I'm using an image as my submit "button" and not the standard form submit button- will that affect the onlick command?
Thanks for the quick reply, I appreciate the help.
For your image, I'd use: <input type=image onclick="Test()"> instead of <input type=button onclick="Test()" value=" Submit ">.
Ok, let me break the script down for you, so you understand what we've got going. First we create a function called "Test()." Now we check if the 5th radio button is checked (that's what document.Report_Error.elements[4] is. 0=1, 1=2, 2=3, 3=4, 4=5, and so on). If it is, we check if the textarea has anything in it. If it does, we don't do anything, but if it doesn't, we alert them saying they can't submit it yet, and we set the focus to the textarea (so that the cursor is in it).
Oh, I forgot to add this, though.. change the script to this:
<script>
function Test(){
otherinfo = document.Report_Error.OtherInfo.value;
if(document.Report_Error.elements[4].checked){
if(otherinfo=="" || otherinfo==" ") {
alert("You must put something in the textarea to specify what \"other\" is.");
document.Report_Error.OtherInfo.focus();
return false;
} } }
</script>
That will prevent the form from being submitted when the don't add anything.
KnightWolfJK
03-15-2003, 09:33 AM
Still no luck yet... I pasted in the new JS and I've been using <input type="image" SRC="focus/images/butt-form_send.gif" onClick="Test()">. I'm still able to click the "Other" radio button and successfully submit the form.
I appreciate the breakdown of the script, it makes things a lot easier. Any ideas on what I'm doing wrong? I'm curious, what part of the script makes it look at the "Error.Type" field?
khalidali63
03-15-2003, 10:18 AM
See if the solution here does anything close to what you inted to do..
http://68.145.35.86/temp/KnightWolfJK.html
Cheers
Khalid
KnightWolfJK
03-16-2003, 10:47 PM
That worked like a charm, I really appreciate it. I noticed that once it validates I can leave the field blank and hit submit again, and the validation process is skipped the second time around. Is there anything I can add to fix that?
Thanks again for your time