Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    How can I check if any radio button is selected? I want to enable the submit button only after the user checks a radio button. Otherwise, a user can hit the submit button without selecting any answer. My form is html but I am displaying with php using echo statemetns so php reply is fine too...
    Here is my submit button right now
    echo "<input type=\"submit\" name=\"submitname\" value=\"submit my answer\"/>";

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    I found this on the internet ... using Javascripting ...

    <form>
    <input name="rad" type="radio" value="Yes" onclick="this.form['sub'].disabled=false"> YES
    <br>
    <input name="rad" type="radio" value="No" onclick="this.form['sub'].disabled=true"> NO
    <br>
    <br>
    <input type="submit" name="sub" value="Submit" disabled>
    </form>


    Further discussion on whether or not to disable it right away when it loads:

    window.onload = init;
    function init() {
    if (!document.getElementById) return;
    var submitObj = document.getElementById('sub');
    submitObj.disabled = true;
    }
    <input type="submit" name="sub" id="sub" value="Submit">



    So, you do have the thought about Javascripting enabled or not,
    and whether it should be disabled right away or not. Not sure how
    important it is to you, and if it really matters.

    If it's some sort of legal agreement "agree/disagree", you may not want
    to use Javascripting, but instead let them submit and send them back
    if they didn't check the "agree" button. That would be using PHP instead
    of Javascripting.


  4. #3
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    Okay what I chose to do was, on the second file that the form submits to, I say if the response is null, then display the first form file again. However, it brings up only the submit button, nothing else. Why might tat be?

    (Note: I also tried bringing up a intermediate file, which refreshes to the first file that displays the form, such as :<meta http-equiv="refresh" content="5;url=http://......./pollq.php"> That also just brings up the submit button, nothing else, no radio button options or anything. )

    if (is_null($response))
    {
    include("pollq.php");
    }
    else
    {//the rest of the entire code that updates and displays the answer}

    So again, when pollq is called if the answer is null, it only dsplays the submit button. What might be the reason and how can I fix so it brings the whole form again, like it correctly does at the first loading?

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Here's a quick script I wrote that works. The second radio button unlocks the select box.
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<script language="javascript">
    			var enableIfThisValue = 'value2';
    			function checkRadio() {
    		        var selectBox = document.getElementById('selectBox');
    				var input = document.forms[0].elements['radio1'];
    				for (i = 0; i < input.length; i++) {
    					if (input[i].checked) {
    				        selectBox.disabled = (input[i].value != enableIfThisValue);
    					}
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<form action="#">
    			<input type="radio" value="value1" id="radio1_value1" name="radio1" onchange="checkRadio()" />
    			<input type="radio" value="value2" id="radio1_value1" name="radio1" onchange="checkRadio()" />
    			<select name="box" id="selectBox" disabled="true">
    				<option value="1">Hi</option>
    			</select>
    		</form>
    	</body>
    </html>
    

    ([Edit: uploaded it, too)

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    ketanco ...

    On that note ... how you want to do it ...

    maybe on the original form, check for a PHP SESSION variable.

    Always display the SUBMIT, but in the next intermediate script,
    if the radio button is checked, set a SESSION variable.

    From there, go to your next script and check for the SESSION
    variable, if it's set, OK, otherwise, go back to your original form.

    If they check the radio button and later on, come back to the form,
    it will know that they already checked the button. The PHP SESSION
    remains set for the time that they have their browser open, no matter
    which web sites the visit.



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

check whether radio button hide

,

how to check radio button is selected css

Click on a term to search for related topics.
All times are GMT -6. The time now is 09:00 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com