Hello Everyone,

I have 2 checkboxes in a form and onclick of these. Once the checkbox is checked, only the fields that are relevant to the checkbox are displayed, with default values in it and to fetch the default values i need to trigger php code. So i can't perform the operation once the whole form is submitted. As the client i am working for does not support AJAX. I can't go for it.

So i have written onclick = document.formName.submit(); Now it is triggering the same page and i am able to write the code. I am not able to differentiate which checkbox is checked.

I don't want to use the procedure of:- calling javascript and then storing the value of the checkbox in a variable and making this variable as invisible.

I would like to write something like document.formName.submit('checkbox1'). So that i should be able to handle the value of this or i dont know.

Please suggest me an alternative method or better approach.

First of all, in these kinds of situations it is very usefull to use AJAX. The fact that your client does not 'support' it is strange.

Anyway, since you submit the forum, you can check the values of the post variables that are set. So you can get the value of a checkbox named 'checkbox1' with the variable $_POST. In order to check if the checkbox is checked (you're still with me? :) ) you can use the isset() function.

if(isset($_POST['checkbox1'])){
//checkbox1 is checked
}

with foreach($_POST as $key => $val){...} in this block you can check if (and what) checkboxes are selected

Why would you loop trough every post variable? That is a bit unnecessary IMO.

you are right, for his situation, it's enough to check every checkbox.

In my previous post I was thinking at a random number of checkboxes - i apologize for any confusion created ;)

Also, when using multiple checkboxes, you can name your checkboxes "checkbox[]" (for example). This way, $_POST will become an array containing the values of ALL checkboxes.

Anyway, checking every checkbox separately is enough for this particular problem.

Member Avatar for rajarajan2017

Below sample checks whether the check box is selected or not.

Sample:

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language="javascript">
	function testing(){
		 var hasSelected = false;
		 var FormObj = document.formName; // formName => name of form tag
		 
		 var chklen = FormObj.elements.length;
		 for( var n = 0; n < chklen; n++ )
		 {
			currobj = FormObj.elements[n];
			if(currobj.type == "checkbox" && currobj.checked)
				hasSelected = true;
		}
		if( hasSelected )
		{
			alert("One or more checkboxes are checked.");
			return true;
		}
		else
		{  
		   alert( "Please check at least one Record." );
		   return false;
		}
	}
</script>
</head>
<body>
<form name="formName" method="post" action="">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="Java">Java<br>
<input type="checkbox" name="list" value="Javascript">Javascript<br>
<input type="checkbox" name="list" value="Active Server Pages">Active Server Pages<br>
<input type="checkbox" name="list" value="HTML">HTML<br>
<input type="checkbox" name="list" value="SQL">SQL<br>
<input type="submit" name="commit" value="Submit" onClick="testing()"/>
</form>
</body>
</html>

Analyse this code and include the php code wherever you want according to the standard

Hello Everyone,

Thanks for all of your replies.

Requirement:- I have a form, in which i have 2 checkboxes. Onclick of a checkbox, values for first set textboxes(that are in the same form) needs to be fetched automatically. onclick of another checkbox the value of select tag and textarea needs to be filled
.
My approach:- I have created 2 checkboxes and onclick of each of the checkboxes i am submittting the same form. From where i can fetch the values of the fields that needs to be filled automatically, using php code.

print_r($_post) will return Array(checkbox1=>primary checkbox2=>secondary). The value of first and second checkboxes. But i will not able to find which is checked by this click. It just returns values of the checkboxes. But does not give me the info, onclick of which checkbox the form got submitted.

So, The question is how will i come to know, if the form got submitted, by click of first checkbox or second.

Hope i am clear in explaining the questions and waiting for replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.