Retrieve checkbox status in a function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4749 Views - Last Post: 17 August 2007 - 02:27 PM

#1 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Retrieve checkbox status in a function

Posted 07 August 2007 - 01:45 PM

Hello, I tried to search for one thread like this one but I wasn't able to find it. Let explain what I want to do :
One page with 2 button and one checkbox, if you press one of the button, it redirects you to the selected option. If the check box is checked, a cookie is stored so that when the user come back, there's something that knows what he pressed.

let's post my index.php
<html>
<head>
<script language="Javascript1.2" src="functions.js" type="text/javascript">
</script>
<script language="Javascript1.2" src="lang_check.js" type="text/javascript">
</script>
</head>
<body>
<script language="javascript">
document.cookie
</script>
<form name="langButton" onsubmit="return check_pressed_button();" >
<input type="submit" value="Franšais" onclick="document.pressed=this.value"/>
<input type="submit" value="English" onclick="document.pressed=this.value"/>
<input type="checkbox" name="remember_lang" value="yes"/>Remember my decision
</form>
</body>
</html>



lang_check.js
var langCookie = get_cookie("lang");
	if(langCookie!=""){
		if(langCookie=="Franšais"){
			document.location.href="main_fr.php";
		}
		else if(langCookie=="English"){	
			document.location.href="main_en.php";
		}
	}



and the functions.js (in this one I will only post the functions that you need to see ...
function check_pressed_button(){
	if(document.pressed=="Franšais"){
		if(document.langButton.remember_lang.checked){
			document.write(set_cookie("lang","Franšais", 2100, 01, 01));
		}
		document.location.href="main_fr.php";	
	}
	else if(document.pressed=="English"){	
		if(document.langButton.remember_lang.checked){
			document.write(set_cookie("lang","English", 2100, 01, 01));
		}
			document.location.href="main_en.php";
	}
}	



My set_cookie function works fine because if I remove the "if(document.langButton.remember_lang.checked)" statement, the function saves the cookie and redirects too, but when I add the statement nothing happens...

I tried to put the checkbox in the params of the function but still doesn't work...
Thanks for the precious help, it's REALLY appreciated !

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve checkbox status in a function

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,068
  • Joined: 10-May 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 01:47 PM

Since checkboxes are javascript, & javascript executes client site, & php executes on the server, you'll need to pass the values of the checkbox (that are set client site) back to your php so it can know what the values are.

Think of it as like a teacher grading a test. She knows all the possible answers, but you have to check the boxes on the test. She can't grade them until you hand it back in.
Was This Post Helpful? 0
  • +
  • -

#3 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 02:04 PM

View Postno2pencil, on 7 Aug, 2007 - 01:47 PM, said:

Since checkboxes are javascript, & javascript executes client site, & php executes on the server, you'll need to pass the values of the checkbox (that are set client site) back to your php so it can know what the values are.

Think of it as like a teacher grading a test. She knows all the possible answers, but you have to check the boxes on the test. She can't grade them until you hand it back in.


I'm not sure if we understand each other well.

The cookie is stored on client side and the javascript check client side. The set_cookie is a javascript fucntion and works well. it just simply redirect to the page if the cookie is stored. my php code doesn'T need to know if the checkbox is checked or not.

I hope I'm more explicit ;)

EDIT : my main_en.php and main_fr.php don'T need to get the checkbox value (if this can help you to understand what I'm trying to do)

This post has been edited by blux: 07 August 2007 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,068
  • Joined: 10-May 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 02:06 PM

Ooopsie. Sometimes I think before I read.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 02:20 PM

First, the document.from_name wont work in all browsers, you should check for what events are available before doing this

function DOMCall(name) 
{
  //Checks the DOM features available
   if (document.layers) //checks document.layers
		return document.layers[name];
   else if (document.all) //checks document.all
		return document.all[name];
   else if (document.getElementById) //checks getElementById
		return document.getElementById(name);
}



Then use this to find the object

If (DOMCall('remember_lang').checked)
{
   //do your stuff
}



See if that helps at all
Was This Post Helpful? 0
  • +
  • -

#6 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 02:30 PM

I tried this DOMCall function and here's my check_pressed_button function

function check_pressed_button(){
	if(document.pressed=="Franšais"){
		if(DOMCall('remember_lang').checked){
			document.write(set_cookie("lang","Franšais", 2100, 01, 01));
		}
		document.location.href="main_fr.php";	
	}
	else if(document.pressed=="English"){	
		if(DOMCall('remember_lang').checked){
			document.write(set_cookie("lang","English", 2100, 01, 01));
		}
			document.location.href="main_en.php";
	}
}	



Still doesn't work. I tried in firefox, which nothing happens, and IE does one thing : when the check box is selected, it prints undefined and redirect to the page

Still need help :(

EDIT : I tried to put id="remember_lang" in the check box input tag and now firefox does the sane thing that IE : if the check box is checked, the redirect works, otherwise it does NOTHING.

EDIT2 : I also tried action="" method="post" in the form tag but still no luck.

This post has been edited by blux: 07 August 2007 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 04:54 PM

I apologize, once I read my post I realized I forgot to mention adding id="remember_lang" to the checkbox, I apologize for that. I've looked at your script and made some changes and I want you to try it

function check_pressed_button(){
	if(document.pressed=="Franšais"){
		if(DOMCall('remember_lang').checked){
			document.write(set_cookie("lang","Franšais", 2100, 01, 01));
			document.location.href="main_fr.php";
		}else{
				 document.location.href="main_fr.php";
			}
					   
			
	}
	else if(document.pressed=="English"){	
		if(DOMCall('remember_lang').checked){
			document.write(set_cookie("lang","English", 2100, 01, 01));
			document.location.href="main_fr.php";
		}else{
						 document.location.href="main_fr.php";
			}
	}
}	


That should solve your issue there :)

This post has been edited by PsychoCoder: 07 August 2007 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 05:03 PM

Oh my goodness ! FINALLY I found the problem. I will share this 4 hours of work with you guys :

my mistake was that I wrote document.location.href="myurl"

I should have written :
window.location.href="myurl";

and now everything is fine !

Thanks a lot guys for your help :)

This post has been edited by blux: 07 August 2007 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 05:22 PM

This is the weirdest thing I've ever seen, see if this helps at all


function check_pressed_button(){
	var language = "";
	var checked = false;
	var page = "";
	if(document.pressed=="Franšais"){	
		if(DOMCall('remember_lang').checked){ 
			checked = true;
			language = "Francias";
			page = "main_fr"
		}	
	}
	else if(document.pressed=="English"){	
		if(DOMCall('remember_lang').checked){
			checked = true;
			language = "English";
			page = "main_en"
		}
	}
	if(checked == true){
	   document.write(set_cookie("lang"' + language + ', 2100, 01, 01));
	   document.location.href= page+".php";
	}
} 



There might need to be some tweaking as I dont know the content of your set_cookin method, and I'm not 100% you can use variables in a location.href either.
Was This Post Helpful? 0
  • +
  • -

#10 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 08:28 PM

I don't know too, Without alert box my script doesn't works and with the alertbox it works ... I tried putting alert("OK") before, after, inside if statement and with that it works fine ... I will start throwing some tears.
function check_pressed_button(){
	if(document.pressed=="Franšais"){	
		if(DOMCall('remember_lang').checked){			
			document.write(set_cookie("lang","Franšais", 2020, 01, 01));	
			window.location.href="main_fr.php";	
			alert("OK1");
		}else{	
		window.location.href="main_fr.php";	
		alert("OK1")
		}
	}
	else if(document.pressed=="English"){	
		if(DOMCall('remember_lang').checked){
			document.write(set_cookie("lang","English", 2020, 01, 01));
			window.location.href="main_en.php";
			alert("OK1")
		}else{
		window.location.href="main_en.php";		
		alert("OK1")
		}
	}
}



here's my complete functions.js
function DOMCall(name)
{
  //Checks the DOM features available
   if (document.layers) //checks document.layers
		return document.layers[name];
   else if (document.all) //checks document.all
		return document.all[name];
   else if (document.getElementById) //checks getElementById
		return document.getElementById(name);
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );
  if ( exp_y )
  {
	var expires = new Date ( exp_y, exp_m, exp_d );
	cookie_string += "; expires=" + expires.toGMTString();
  }
  if ( path )
		cookie_string += "; path=" + escape ( path );
  if ( domain )
		cookie_string += "; domain=" + escape ( domain ); 
  if ( secure )
		cookie_string += "; secure";  
  document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
	return ( unescape ( results[1] ) );
  else
	return null;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
  document.delCookie.action="index.php";
}
function check_pressed_button(){
	if(document.pressed=="Franšais"){	
		if(DOMCall('remember_lang').checked){			
			document.write(set_cookie("lang","Franšais", 2020, 01, 01));	
			window.location.href="main_fr.php";	
			alert("OK1");
		}else{	
		window.location.href="main_fr.php";	
		alert("OK1")
		}
	}
	else if(document.pressed=="English"){	
		if(DOMCall('remember_lang').checked){
			document.write(set_cookie("lang","English", 2020, 01, 01));
			window.location.href="main_en.php";
			alert("OK1")
		}else{
		window.location.href="main_en.php";		
		alert("OK1")
		}
	}
}	



with this function.js and :check_lang.js and index.php stated earlier in this thread, you can recreate my weird bug.

This post has been edited by blux: 07 August 2007 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieve checkbox status in a function

Posted 07 August 2007 - 08:54 PM

I tried this (without the set_cookie), I did something else in the if statement. Also got rid of all the if..else statements

switch(document.pressed)
{
	 case "Francais":
		 if(DOMCall('remember_lang').checked)
		 {
		  document.write(set_cookie("lang","Francias",2100,01,01));
		  }
		  document.location.href = "main_fr.php"; 
		  break;
	 case "English":
		  if(DOMCall('remember_lang').checked)
		 {
		  document.write(set_cookie("lang","Francias",2100,01,01));
		  }
		  document.location.href = "main_fr.php";
		  break;
	 default://whatever you want to be the default action goes here
}


This post has been edited by PsychoCoder: 07 August 2007 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#12 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: Retrieve checkbox status in a function

Posted 08 August 2007 - 04:39 AM

I tried with the switch case statement, and it's the same strange behavior. I'm really wondering what's wrong with this little dang script. I guess it's the DOMCall because I comment the set_cookie function in the IF statement and the script does the same thing again (nothing unless I check the box)

This post has been edited by blux: 08 August 2007 - 04:41 AM

Was This Post Helpful? 0
  • +
  • -

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieve checkbox status in a function

Posted 08 August 2007 - 04:42 AM

Well take that out, but you need to remember all browsers interpret differently so you need a way to find out if it supports document.GetElementById, document.all, document.layers. Get rid of the DOMCall and go back to using document.form_name.control_name and see what happens
Was This Post Helpful? 0
  • +
  • -

#14 blux  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-August 07

Re: Retrieve checkbox status in a function

Posted 08 August 2007 - 01:52 PM

View PostPsychoCoder, on 8 Aug, 2007 - 04:42 AM, said:

Well take that out, but you need to remember all browsers interpret differently so you need a way to find out if it supports document.GetElementById, document.all, document.layers. Get rid of the DOMCall and go back to using document.form_name.control_name and see what happens

Even if i Take DOMCall or document.form_name.control_name or passing it through parameters of the function, the result is the same ... I think I will translate my javascript to php script, but the problem is : I'm forced to use javascript for the damn window.location.href="url" and I simply don't have any clue how to do it correctly ... If you know please help me out it would be really appreciated :)
Was This Post Helpful? 0
  • +
  • -

#15 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieve checkbox status in a function

Posted 08 August 2007 - 04:59 PM

I dont know why this isnt working for you, I just created 2 pages, one with a button and a checkbox. I put this code it and it worked fine

switch(document.pressed)
{
	 case "Francais":
		 if(DOMCall('remember_lang').checked)
		 {
			   document.write(set_cookie("lang","Francias",2100,01,01));//I used an alert() here
			   document.location.href = "main_fr.php";
		  }else
				{
					   document.location.href = "main_fr.php";
				 }
		  break;
	 case "English":
		  if(DOMCall('remember_lang').checked)
		 {
			  document.write(set_cookie("lang","Francias",2100,01,01)); //I used an alert() here
			  document.location.href = "main_fr.php";
		  }else
				 {
					 document.location.href = "main_fr.php";
				  }
		  break;
	 //default://whatever you want to be the default action goes here
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2