homepage Welcome to WebmasterWorld Guest from 54.167.144.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Checkbox Help
andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3706596 posted 7:11 pm on Jul 24, 2008 (gmt 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,

 

Trace

5+ Year Member



 
Msg#: 3706596 posted 7:29 pm on Jul 24, 2008 (gmt 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.

andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3706596 posted 9:05 pm on Jul 24, 2008 (gmt 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,

Arno_Adams

5+ Year Member



 
Msg#: 3706596 posted 8:55 am on Jul 25, 2008 (gmt 0)

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

HTH, Tom

andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3706596 posted 12:29 pm on Jul 25, 2008 (gmt 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.

Arno_Adams

5+ Year Member



 
Msg#: 3706596 posted 3:45 pm on Jul 26, 2008 (gmt 0)

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

Tom

andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3706596 posted 2:16 pm on Jul 28, 2008 (gmt 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.

Arno_Adams

5+ Year Member



 
Msg#: 3706596 posted 9:30 am on Jul 29, 2008 (gmt 0)

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

Tom

andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3706596 posted 11:47 am on Jul 29, 2008 (gmt 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

Arno_Adams

5+ Year Member



 
Msg#: 3706596 posted 11:55 am on Jul 29, 2008 (gmt 0)

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

Tom

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved