Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with show/hide div tag using php and javascript

    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:
    Code:
    <?php
    $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;
    	if(mylist.selectedIndex==2)
    		myLayer.display="block";
    	else
    		myLayer.display="none";
    }</script></head>
    <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>
    		</select>
    	<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>
    		</select>
    	</div>
    	<input type="submit" value="Submit" name="submit_reg">
    	<input type="reset" value="Reset" name="reset">
    </form></html>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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).

    PHP Code:
    <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.

  • Users who have thanked Fumigator for this post:

    Daxini (03-26-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •