Welcome to WebmasterWorld Guest from 23.20.18.183

Forum Moderators: open

Message Too Old, No Replies

Checkbox Help

     
7:11 pm on Jul 24, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Ok here is the deal I have a form with a bunch of checkboxes. When they are clicked I call a JavaScript Function that calls a php file which in turn echos its output in JavaScript (confusing I know but it is irrelevant). Here is my JavaScript function
function loadFile(targetFile) {
var day = new Date();
var id = day.getTime();
targetFile += '?'+id;
var elem = document.createElement("script");
elem.setAttribute("src", targetFile);
document.getElementsByTagName("body")[0].appendChild(elem);
}
Now this is what one of my checkboxes looks like.

<input type="checkbox" name="highmark" onclick="loadFile('highmark.php')" >

Right now whenever the user checks or unchecks the box, the "highmark.php" file runs. However I want to run the "highmark.php" file only when the user checks the box. If the user unchecks the box I don't want to do anything.

I know the logic I need, where I call this function multiple times I need to pass in the script name and the checkbox name e.g. function loadFile(targetFile, checkBoxName). Then if the checkBox has been checked I want to run my code. Otherwise I don't want to do anything. I am not a JavaScript programmer so I don't know the syntax. Any help? Thanks,

7:29 pm on July 24, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:July 5, 2005
posts:352
votes: 0


Simply check to see if the checkbox is checked;

<input type="checkbox" name="highmark" onclick="if(this.checked){loadFile('highmark.php')}" >

Hope it helps.

9:05 pm on July 24, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts:1130
votes: 0


That does work, but now when I refresh my page I loose the checkbox being checked. I want to be able to refresh the page and not loose the checkbox being checked. Is there any way around that? Thanks,
8:55 am on July 25, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


Store the selected items in a database/session/cookie.

HTH, Tom

12:29 pm on July 25, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts:1130
votes: 0


Are you talking about a session with PHP or JavaScript. If I do a PHP session then I would have to print that session variable for every checkbox and you have no idea how many checkboxes I have. If you are talking about some other kind of session variable I'm all ears.
3:45 pm on July 26, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


Javascript doesn't support sessions. I meant PHP sessions. How many checkboxes are there? I don't see the problem.

Tom

2:16 pm on July 28, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts:1130
votes: 0


Over 200 checkboxes the problem is unless I restructure my entire web page, I would have to output a php variable for each checkbox e.g. <input type = "checkbox" name = "checkbox1" value = "<?php echo($_SESSION['checkbox1Value']); ?>"> <input type = "checkbox" name = "checkbox2" value = "<?php echo($_SESSION['checkbox1Value2']); ?>"> and that would be ugly and inconvenient.
9:30 am on July 29, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


Stuff all the checkboxes in one array and put that in one session. Does that help?

Tom

11:47 am on July 29, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts:1130
votes: 0


No because I have html in front of each array with different text and each of the check boxes have to have a different name. Even if I did output them like that, I would still have to pass in all of the checkbox names and the html with different text in each one. Since I'm already done with the page pretty much, I'm just going to leave it
11:55 am on July 29, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


That's your call, I just mentioned another option.

Tom