View Full Version : Problem with show/hide div tag using php and javascript

03-26-2009, 05:27 AM
hi, I am newbie to web development using javascript and php. I am creating a form and I want a dynamic look on my form. I am having a listbox and I want that according to selection "yes/no" a <div> tag is show & hide and for that I write a javascript code that is working fine according to selection. I am also doing form validation in php and if any error occurs it is displayed when we submit the form. But my problem is that when page is displayed with error, listbox selection is "yes" due to my php coding but the <div> tag is not visible because I call javascript showhide() function on onchange event. so I need some help here, I want to know that I where I should make changes in my code or write some more code or function in javascript or php or ajax, so that when page is displayed with error and listbox selection is "yes" that <div> tag should be displayed.
Here is my code:

$error_message = array();
if ($_POST['submit_reg'] != '')
{ $org = $_POST['org'];
$benefit = $_POST['benefit'];
if($org=="select") $error_message[] = 'Are you a Member of our Organisation?';
else if($org=="yes") { if($benefit=="select") $error_message[] = 'Please select your Benefit or Allowance'; } }
if($_POST['submit_reg'] == '' || count($error_message) > 0)
{ if (count($error_message) > 0)
{ foreach($error_message as $val)
{ echo $val; } } }
<html><head><script type="text/javascript">
function showhide_func()
{ var mylist=document.getElementById("org");
var myLayer = document.getElementById("layer").style;
<form method="POST" action="showhide.php" onReset="return confirm('Are you sure you wish to clear the form?');" name="org_reg" >
<label>Are you a Member of our Organisation?</label>
<select name="org" id="org" onchange="showhide_func()" on>
<option value="select" <?php if ($org == 'select') echo "selected=\"selected\""; ?>> Select</option>
<option value="no" <?php if ($org == 'no') echo "selected=\"selected\""; ?>> No</option>
<option value="yes" <?php if ($org == 'yes') echo "selected=\"selected\""; ?>>Yes</option>
<div id="layer" style="display:none" >
<label>If YES, What Benefit or Allowance are you currently receiving?</label>
<select name="benefit" id="benefit" >
<option value="select" <?php if ($benefit == 'select') echo "selected=\"selected\""; ?>> Select</option>
<option value="disability" <?php if ($benefit == 'disability') echo "selected=\"selected\""; ?>> Disability</option>
<option value="aged" <?php if ($benefit == 'aged') echo "selected=\"selected\""; ?>> Aged</option>
<input type="submit" value="Submit" name="submit_reg">
<input type="reset" value="Reset" name="reset">

03-26-2009, 07:29 AM
You can set the display attribute of the div using PHP just like you are choosing which option should be selected (and using the same variable).

<div id="layer" style="display:<?php echo ($org == 'yes' ? 'block' : 'none'); ?>;">

Also, you should be echoing your errors within the confines of the html structure (in its own div, or whatever). Right now you are echoing the errors before you even echo the <html> tag.