PDA

View Full Version : Hide elements based on database data



Net51
11-27-2007, 02:55 PM
I've got a web form with some elements that I want to be hidden until a checkbox is checked. The elements that need hiding are within a DIV (called F1div) and the checkbox has this onclick code:

onclick="modifydiv(this.checked,'F1div')"

Which runs this code:

<script>
function modifydiv(val,divname)
{
oDiv = eval("document.all."+divname);
if(val)
{
oDiv.style.visibility = "visible";
oDiv.style.display = "inline";
}
else
{
oDiv.style.visibility = "hidden";
oDiv.style.display = "none";
}
}
</script

Now this works OK and when the check box is checked or unchecked the div is shown or hidden.

I would like though the form at page load to either show or hide the div depending on the value of the check box at page load (as the checkbox is bound to a boolean database field) too but aren't sure how to do this.

I want this because when the visitor first visits the page the checkbox is unchecked (so I can hide the div at pageload) but if they check the box and submit the form (saving the data to the database) and then later come back to the page I want it to look at the value of the checkbox and either show or hide the div accordingly.

Any ideas?

Thanks.

zea726
11-28-2007, 08:40 AM
I didnt get ur problem very clearly.... on the basis of my understanding i m trying to help u. if i m wrong, forgive me.

now ... as the value of the checkbox is coming to the page while u loading the page...just simply take the value to a hidden variabel..

suppose you have got the value by post or get method;

<input type="hidden" name="hidden_chkbox" name="hidden_chkbox_ id" value="<? php echo $_POST['val_chkbox']; ?>">

now take it to java script
<script>

var val= document.getElementById("hidden_chkbox_ id");

</script>

so you have the value of the checkbox in the variabel val. now you can run a function while loading the page

<script>

function start(){
// do what you want to do
if(val==...){
///
}
else{
////
}
}

window.onload=start();
</script>

Net51
11-30-2007, 03:24 PM
Thanks zea726,

Yes that's what I'm looking for. I basically have form fields that I hide depending on which check boxes the user clicks. The selection is then stored in the database and I want it so that when they return to the page the form hides the fields it needs to again based on the previously store selection.

That code looks like what I need - I'll give it a try and see! :D