Forum Moderators: open

Message Too Old, No Replies

Show / Hide script how to keep "state" of div if form submission fails

         

nigelt74

11:45 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Moderators please change the title to something that makes more sense, as i can't figure out how to condense this question down - sorry

I have a hide/show script on a form controlled by a radio button that lets say finds out if someone has a drivers licence

If the answer is
1) yes then it asks them for the class of their license as well as their license number
2) No then it doesn't ask them anything

And that all works beautifully to a point, I have server side error checking set up which works fine, the only problem is that if the form is rejected by the server because a required value hasn't been filled in (anywhere in the form) the Div will reset to its original status, as in the radio button will be ticked for yes but the associated questions in the div are hidden, i can't seem to find a way to preserve the state of the div

This is the code used in my head section


<script type="text/javascript">
<!--

function changeDiv(the_div,the_change)
{
var the_style = getStyleObject(the_div);
if (the_style!= false)
{
the_style.display = the_change;
}
}

function hideAll()
{
changeDiv("married_questions","none");
changeDiv("single_questions","none");
}
function hideAll2()
{
changeDiv("passport","none");
changeDiv("n_passport","none");
}

function getStyleObject(objectId) {
if (document.getElementById && document.getElementById(objectId)) {
return document.getElementById(objectId).style;
} else if (document.all && document.all(objectId)) {
return document.all(objectId).style;
} else {
return false;
}
}
// -->
</script>

and here is the code that controls the actual questions,

Do you have a current drivers licence<br>
<input type="radio" name="v_lic" <?php if($v_lic == 'yes') echo "checked ";?>value="yes" onClick="hideAll3(); changeDiv('licence','block');">yes
<input type="radio" name="v_lic" <?php if($v_lic == 'no') echo "checked ";?>value="no" onClick="hideAll3();">no
<div id="licence" style="margin-left:30px;display:none">
<br>Please enter your licence number here<br>
<INPUT TYPE="TEXT" NAME="v_licn" VALUE="<?= $v_licn?>" SIZE="30" MAXLENGTH="30" ><br>
Please enter your licence class<br>
<INPUT TYPE="TEXT" NAME="v_licc" VALUE="<?= $v_licc?>" SIZE="30" MAXLENGTH="30" >
</div>


any help would be greatly appreciated as i am well and truly stumped

nigelt74

12:56 am on Oct 1, 2007 (gmt 0)

10+ Year Member



Ok I have got it working

A very inelegant solution but I added this within the javascript section of my head section

<?php
if($v_lic == 'yes') echo "
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload!= 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
hideAll3(); changeDiv('licence','block');
})
";
?>

Cheers

daveVk

1:04 am on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps change

<div id="licence" style="margin-left:30px;display:none">

to

<div id="licence" style="margin-left:30px;<?php if($v_lic == 'no') echo "display:none ";?> >

and so on

nigelt74

1:12 am on Oct 1, 2007 (gmt 0)

10+ Year Member



Cheers Dave that looks a lot simpler, I don't know why i didn't think to try that

Thanks