Solved

How to use PHP and Javascript to auto select Option value on load?

Posted on 2011-03-06
5
817 Views
Last Modified: 2012-05-11
Hi,

I'm trying to figure out how to get the dropdown or select option selected on load?
I don't know how to get or pass the values of $country and $state from the index.php into the javascript functions then how do I check if its the right option to set selected as true?
I don't know how or if you can mix javascript and php scripting...

I've tried to make this work for days now...
Just trying to test echo true with php into the function to test. I can't make anything work. I have tried every which way I could think of and then some....    the list goes and goes... ie..
   addOption(document.drop_down.DD1, "1", "United States", <?php echo "true"; ?>);
   addOption(document.drop_down.DD1, "1", "United States", <?php echo "\"true\""; ?>);
   addOption(document.drop_down.DD1, "1", "United States", "<?php echo "true"; ?>");
   addOption(document.drop_down.DD1, "1", "United States", "<?php echo 'true'; ?>");
   addOption(document.drop_down.DD1, "1", "United States", "<?php echo 'chr(34)'.'true'.'chr(34)'; ?>");

This may just be something that has to be set after the fact somehow with something like getElementById...

Thanks
Richard

 
<?php
//  index.php
// Get or load country and state variables
$country = "1";			// For testing set default as country 1
$state = "14";			// For testing set default as state as 14
?>

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Auto fill second dropdowns...</title>
<script language="javascript" src="list.js"></script>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fill_DD1();">
<form name="drop_down" action="index.php" method="POST" >
		
<SELECT NAME="DD1" onChange="fill_DD2();">
<Option value="">Country</option>
</SELECT>

&nbsp;

<SELECT NAME="DD2">
<Option value="">State</option>
</SELECT>

</form>
</body>
</html>

Open in new window

// list.js
// Function is used to fill the first dropdown selectbox on load
function fill_DD1(){ 
addOption(document.drop_down.DD1, "1", "United States", "false", 'false');
addOption(document.drop_down.DD1, "2", "Canada", "false", "false");
addOption(document.drop_down.DD1, "3", "United Kingdom", "false", "false");
fill_DD2();
}


// ON selection of first dropdown fill the second dropdown
function fill_DD2(){
 removeAllOptions(document.drop_down.DD2);
    // First remove all options from the second dropdown
 addOption(document.drop_down.DD2, "", "Select State", "");    // Add the first option value to second dropdown

// later I would like to get the values from the database or cache file
  if(document.drop_down.DD1.value == '1'){
   addOption(document.drop_down.DD2,"14", "Alabama", "false", "false");

   addOption(document.drop_down.DD2,"15", "Alaska", "false", "false");

   addOption(document.drop_down.DD2,"16", "Arizona", "false", "false");

  }


  if(document.drop_down.DD1.value == '2'){

   addOption(document.drop_down.DD2,"2", "Alberta", "false", "false");

   addOption(document.drop_down.DD2,"3", "British Columbia", "false", "false");

   addOption(document.drop_down.DD2,"4", "Manitoba", "false", "false");

  }

  if(document.drop_down.DD1.value == '3'){

   addOption(document.drop_down.DD2,"1", "London");

  }
}


// Function to remove all options from a selectbox
function removeAllOptions(selectbox){
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{selectbox.remove(i);}
}


// function to add options to a selectbox
function addOption(selectbox, value, text, defaultSelected, selected){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
        if (selected=="true"){optn.selected = selected;}
	selectbox.options.add(optn);
}

Open in new window

0
Comment
Question by:fwlrichard
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 35053442
I think you are trying to use PHP inside the file  list.js
 if that is the case then you are wrong ,
You should rename the file  list.js  to be list.php    and
change this line  <script language="javascript" src="list.js"></script>
 to be
<script language="javascript" src="list.php"></script>

this way it should work  
0
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 35053462
@fwlrichard: since your js isn't finished, I just add a sample code to give you the idea.

In PHP, you create two js var using PHP, like this

<?php
//  index.php
// Get or load country and state variables
$country = "1";                 // For testing set default as country 1
$state = "14";                  // For testing set default as state as 14
?>

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Auto fill second dropdowns...</title>
<script language="javascript" src="list.js"></script>
<script language="JavaScript">
<!--
var selectedCountry = <?php echo $country?>;      
var selectedState = <?php echo $state?>;      
//-->
</script>
</head>

In your js file (as I modified a little), it should look like this
function fill_DD1(){ 
	var ary = new Array();
ary[1] = "United States";
ary[2] = "Canada";
ary[3] = "United Kingdom";
for (i = 1; i <= 3; i++ )
{
	if (selectedCountry)
	{
		addOption(document.drop_down.DD1, i, ary[i], "true", 'true');
	}
	else {
		addOption(document.drop_down.DD1, i, ary[i], "false", 'false');
	}
}
fill_DD2();
}

Open in new window

0
 

Author Comment

by:fwlrichard
ID: 35053590
honestman31: and dsmile:
Thank you both for the quick reply.

honestman31: I tried changing the list.js to list.php and renaming list.js to list.php it didn't seem to solve the problem. I may not have the php formated correctly. I don't get a error but it don't work.
addOption(document.drop_down.DD1, "1", "United States", "false", "<?php echo 'test';?>");

dsmile: I agree that I will most likly use a array and set the selected with a IF statment.
But the problem is how do I get the value $country value into "selectedCountry" ?

Thanks
Richard
0
 
LVL 13

Expert Comment

by:dsmile
ID: 35053702
See the php code I gave you
<?php
//  index.php
// Get or load country and state variables
$country = "1";                 // For testing set default as country 1
$state = "14";                  // For testing set default as state as 14
?>

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Auto fill second dropdowns...</title>
<script language="javascript" src="list.js"></script>
<script language="JavaScript">
<!--
var selectedCountry = <?php echo $country?>;      
var selectedState = <?php echo $state?>;      
//-->
</script>
</head>

Open in new window

0
 

Author Comment

by:fwlrichard
ID: 35053740
dsmile:
Sorry about that.... I skiped the second script:
<script language="JavaScript">
<!--
var selectedCountry = <?php echo $country?>;      
var selectedState = <?php echo $state?>;      
//-->
</script>

 I have tested it and it does work.
Thank You
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

22 Experts available now in Live!

Get 1:1 Help Now