0

Hi,I want to retain value of checkbox,when some validation error occours.
In this i am calling a function checkCheckBoxes() & want to call this function
when validation error occours.

<input type="checkbox" name="chk1"  onclick='checkCheckBoxes() <?php if (isset($_POST['chk1'])) echo ' checked="checked"'; ?>'/>check for Birth year only

function

function checkCheckBoxes() 
{
	
	if(document.myform.chk1.checked)
    {
     document.myform.day.disabled=true;
     document.myform.Month.disabled=true;
    }
    else
    {
       document.myform.day.disabled=false;
       document.myform.Month.disabled=false;
    }
}
4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by urtrivedi
0

try this

<input type="checkbox" name="chk1"  onclick='checkCheckBoxes()' value="1" <?php if ($_POST['chk1']=="1") echo 'checked="checked"'; ?>/>check for Birth year only
0

still function is not working.
only checkbox is selected after validation failure.

0

function is running fine here

I think you have problem with

<input type="checkbox" name="chk1"  onclick='checkCheckBoxes()' />check for Birth year only

or if you have chrome, then open tool> java script console, there you will able to see error.

0

function is running fine here

I think you have problem with

<input type="checkbox" name="chk1"  onclick='checkCheckBoxes()' />check for Birth year only

or if you have chrome, then open tool> java script console, there you will able to see error.

Thanks,function is working fine.but my problem is:
I have 3 drop down list.when a user check checkbox,it call checkCheckBoxes() function & disable first 2 dropdown.
What i am trying,
suppose a user check the checkbox & there is some validation error while submiting,
then the checkbox remain check & disable drop down remain disable
my problem is disable drop down does not remain disable.

0

suppose a user check the checkbox & there is some validation error while submiting,
Are you validating form in PHP side?

0

post complete code, and let me know when logic fails (what you input and what not)

<tr>
<td align="right">जन्म तिथि:&nbsp;&nbsp;</td>
<td>
<div style="float:left; width:auto;">
<select name="year" id="year">
    <?php
		for($y=1920; $y<=2012; $y++)
		{
	?>
		<option value="<?php echo $y; ?>" <?php if($y==$_POST["year"]) echo "selected"; ?> > <?php echo $y; ?> </option>
    <?php
		}
    ?>
    </select>

</div><?php
if(isset($_POST['submit']))
			$ccountry = $_POST['Month'];
			else
			$ccountry = '';
							
		 
		 $country_to_preselect = $ccountry;
		
						
			$options_month = array
			(
			''	 => '-- Select --', 
		         "1" => 'January',
			 "2" => 'Feb',
			 "3" => 'March',
			 "4"=>'April',
		         "5"=>'May',
		         "6"=>'June',
		         "7"=>'july',
		         "8"=>'August',
		         "9"=>'September',
		         "10"=>'October',
		         "11"=>'November',
		         "12"=>'December'
						);
            	?>

<div style="float:left; width:auto; padding-left:10px;">
	<select name="Month" onchange="showdate(document.getElementById('year').value, this.value);">
	<?php
		foreach ($options_month as $index => $value)
			{
			   echo '    <option value="' . $index . '"';
				 if ($index == $country_to_preselect)
				   {
						  echo ' selected ';
				   }
				    echo '>' . $value . '</option>';
						}
					?>
	</select>
</div>
<div id="txtHint" style="width: 43px; float:left; padding-left:10px;">
 <select name="day" id="day">
    <?php
		for($d=1; $d<=31; $d++)
		{
	?>
		<option value="<?php echo $d; ?>" <?php if($d==$_POST["day"]) echo "selected"; ?> > <?php echo $d; ?> </option>
    <?php
		}
    ?>
    </select>
</div>
&nbsp;<input type="checkbox" name="chk1"  onclick='checkCheckBoxes()' value="1" <?php if ($_POST['chk1']=="1") echo 'checked="checked"'; ?>/>check for Birth year only</td>
</tr>

Js code

function checkCheckBoxes() 
{
	
	if(document.myform.chk1.checked)
    {
     document.myform.day.disabled=true;
     document.myform.Month.disabled=true;
    }
    else
    {
       document.myform.day.disabled=false;
       document.myform.Month.disabled=false;
    }
}

function showdate(yr, mon)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="select-date.php";
url=url+"?yr="+yr;
url=url+"&mon="+mon;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
0

at the end of page load write following js code

<script lang='javascript'>
checkCheckBoxes();
</script>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.