Solved

jquery chained selects

Posted on 2008-10-22
5
2,918 Views
Last Modified: 2013-12-13
Hello
note: sorry for my english
OK , I try to use a library jquery and plugin jquery chained select, but i have 2 problem:
1. how i can get data from database mysql?
i post code php :
******************************PHP
<?php
$array = array();
if ($_GET['_name'] == 'tipglass1')
{
       if ( $_GET['_value'] == 0 ) {// . . . . . . . . . . . . . . . . . . . . . . nullo
            //$array[] = array('0' => 'Seleziona una tipologia');      
        } elseif ( $_GET['_value'] == 1 )  {      // . . . . . . . . . . . . . . . . . . . monolitico
            $array[] = array('1' => '4 chiaro');
            $array[] = array('2' => '5 chiaro');      
            $array[] = array('3' => '6 chiaro');
        } else {                                                // . . . . . . . . . . . . . . . . . . . mstratificato
            $array[] = array('1' => 'selezionare il numero di pannelli del primo vetro stratificato');
        }
}
echo json_encode( $array );
?>
***************************************************


2. into code jquery how i can hidden the second selectbox when the first is unselect?
Now i post the code here:
***********************************
<script language="JavaScript" type="text/javascript" src="js/jquery.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery.chainedSelects.js"></script>
<script language="JavaScript" type="text/javascript">
$(function()
{

      $('#tipglass1').chainSelect('#glass1','query/queryglass1.php',
      
      {
            before:function (target) //before request hide the target combobox and display the loading message
            {
                  $("#loading").css("display","block");
                  $(target).css("display","none");
                  $("#glass1_li").css("display","none");
            },
            after:function (target) //after request show the target combobox and hide the loading message
            {
                  $("#loading").css("display","none");
                  $(target).css("display","inline");
                  $("#glass1_li").css("display","inline");
                        
                  
                  
            }
            
      });
      
});
</script>

****************************

reghards,
Denny
0
Comment
Question by:denny3d
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22797748
1.  
You will have to grab the data from the MySQL database and then encode it as JSON data.
This will require querying the database and then putting it into a form that can be JSON encoded.  I recommend you create a separate topic for that.

2.
Not sure what you mean by "unselect".  One of the options has to be selected.
Do you mean when you select an option with no value or value=0?:
<script language="JavaScript" type="text/javascript" src="js/jquery.js"></script>

<script language="JavaScript" type="text/javascript" src="js/jquery.chainedSelects.js"></script>

<script language="JavaScript" type="text/javascript">

$(function()

{
 

      $('#tipglass1').chainSelect('#glass1','query/queryglass1.php',

      

      {

            before:function (target) //before request hide the target combobox and display the loading message

            {

                  $("#loading").css("display","block");

                  $(target).css("display","none");

                  $("#glass1_li").css("display","none");

            },

            after:function (target) //after request show the target combobox and hide the loading message

            {

                  $("#loading").css("display","none");

                  $(target).css("display","inline");

                  $("#glass1_li").css("display","inline");

                  if(!$this.val() || this.val() == 0) $(target).hide();

            }

            

      });

      

});

</script>

Open in new window

0
 

Author Comment

by:denny3d
ID: 22801919
ok for the second question:

if I add
if(!$this.val() || this.val() == 0) $(target).hide();

i have this problem:

$this.val = not defined

I must:
if result of first select box = 0 , hide $("#glass1_li")
else
$("#glass1_li").css("display","inline");
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22802171
Typo
if(!($(this).val()) || $(this).val() == 0) $(target).hide();
0
 

Author Comment

by:denny3d
ID: 22802220
no its not work.
I post code so you understand:




I would that if into selectbox id="tipglass1" the value selected is != 0  the id="glass1_li" is display:inline,
while if id="tipglass1" the value selected is == 0 the id="glass1_li" is visible display:none.


html file

<script language="JavaScript" type="text/javascript">

$(function()

{
 

	$('#tipglass1').chainSelect('#glass1','query/queryglass1.php',

	

	{

		before:function (target) //before request hide the target combobox and display the loading message

		{ 

			$("#loading").css("display","block");

			$(target).css("display","none");

			$("#glass1_li").css("display","none");

		},

		after:function (target) //after request show the target combobox and hide the loading message

		{ 

			$("#loading").css("display","none");

			$(target).css("display","inline");

			$("#glass1_li").css("display","inline");

			}

	});

	

});

</script>

</head>

<body>
 

<div id="loading">Loading ...</div>
 

<h2>VETRA CAMERA DOPPIO COMPOSIZIONE</h2>
 

<form name="dgu" method="post" action="">

	<fieldset>

    <legend>Vetro Camera doppio</legend>

    	<ol>

    		<li>

                <label>Tipologia glass 1:</label>

                <!-- tipology combobox -->

                <select id="tipglass1" name="tipglass1">

                <option value="0" selected>- seleziona -</option>

                <option value="1">Monolitico</option>

                <option value="2">Stratificato</option>

                </select>

		    </li>

    		<li id="glass1_li" style="display:none">

                <label>glass 1:</label>

                <select name="glass1" id="glass1" style="display:none" ></select>

			</li>

    	</ol>

    </fieldset>

</form>
 
 

*****************js file selecbox

jQuery.fn.chainSelect = function( target, url, settings ) 

{

  return this.each( function()

  {

	$(this).change( function( ) 

	{

		settings = jQuery.extend(

		{

			after : null,

			before : null,

			usePost : false,

			defaultValue : null,

			parameters : {'_id' : $(this).attr('id'), '_name' : $(this).attr('name'), '_nr' : $(this).attr('number')}

        } , settings);
 

		settings.parameters._value =  $(this).val();
 

		if (settings.before != null) 

		{

			settings.before( target );

		}
 

		ajaxCallback = function(data, textStatus) 

		{

			$(target).html("");//clear old options

			data = eval(data);//get json array

			for (i = 0; i < data.length; i++)//iterate over all options

			{

			  for ( key in data[i] )//get key => value

			  {	

					$(target).get(0).add(new Option(data[i][key],[key]), document.all ? i : null);

              }

			}
 

			if (settings.defaultValue != null)

			{

				$(target).val(settings.defaultValue);//select default value

			} else

			{

				$("option:first", target).attr( "selected", "selected" );//select first option

			}
 

			if (settings.after != null) 

			{

				settings.after(target);

			}
 

			$(target).change();//call next chain

		};
 

		if (settings.usePost == true)

		{

			$.post( url, settings.parameters, ajaxCallback );

		} else

		{

			$.get( url, settings.parameters, ajaxCallback );

		}

	});

  });

};
 

*****************phpfile

<?php

$array = array();

if ($_GET['_name'] == 'tipglass1') 

{

	 if ( $_GET['_value'] == 0 ) {// . . . . . . . . . . . . . . . . . . . . . . nullo

		//$array[] = array('0' => 'Seleziona una tipologia');	

	  } elseif ( $_GET['_value'] == 1 )  {	// . . . . . . . . . . . . . . . . . . . monolitico

		$array[] = array('1' => '4 chiaro');

		$array[] = array('2' => '5 chiaro');	

		$array[] = array('3' => '6 chiaro');

	  } else {								// . . . . . . . . . . . . . . . . . . . mstratificato

		$array[] = array('1' => 'selezionare il numero di pannelli del primo vetro stratificato');

	  }

}

echo json_encode( $array );

?>

Open in new window

0
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22802254
Should do as you describe.
I won't be around for a while, but for anyone else who can help him.

The plugin is here.
http://codeassembly.com/Simple-chained-combobox-plugin-for-jQuery/

It's a hierarchical select, that renders JSON data.
if($('#tipglass1').val() != 0) $('#glass1_li').css('display','inline');
 

if($('#tipglass1').val() == 0) $('#glass1_li').css('display','none');

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Form with Modals 16 42
WordPress syntax 2 24
How can I upload multiple images from a HTML form? 2 18
Session timeout 5 13
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now