Page 1 of 1

JS Function in PHP && GET THE RETURNED VALUE in PHP

Posted: Wed Jan 31, 2007 11:26 am
by vamsinadella
Hi,

I have been struggling from a long time to accomplish dependable select boxes in my php page. I have a select box say s1, which, on change should effect a few other select boxes say s2, s3, s4 to change options (ofcourse based on the value of s1).

What i did is - i wrote a JS function to get the value selected from s1 and i'm calling this JS function from onchage event in s1 like:

Code: Select all

<td><b>Partner:</b>
<select name="partnername" onChange = 'Get_Selected_Item(document.serverform.partnername);'>
<?php
	populatelist('vendor_id','servermodeltable', 'DELL');
?>
</select>
</td>
The populatelist Is a PHP function that populates the list from dB. THE JAVASCRIPT FUNCTION IS:

Code: Select all

<script = "javascript">
function Get_Selected_Item(obj) 
{
	alert('In the JS Function');
	len = obj.options.length;
	i = 0;
  	$chosen = "none";
  	for (i = 0; i < len; i++)
  	{ 
   	if (obj.options[i].selected) 
    	{
      	$chosen = obj.options[i].value;
     	}
 	 }
	window.location.reload();
	return $chosen;
}
</script>
Now my problem is: To set the options for s2, s3, s4 based on s1.value, I NEED THAT VALUE ($chosen) in php to select the options for s2 s3 and s4 fromdB. Once it is out of the function the $chosen is not retaining it's value. I have quite a few pages where i have to implement this but i'm not able to proceed. Any help/suggestions/questions are welcome. Thanks for your help.

- Vamz

Posted: Wed Jan 31, 2007 11:42 am
by daedalus__
I'm not sure I understand what you are asking; I only skimmed through your post. You should look into the XmlHttpRequest Object if you want to send data in-between PHP and Js.

Posted: Wed Jan 31, 2007 11:59 am
by feyd
This sounds like a chained select or something similar.

Posted: Wed Jan 31, 2007 12:19 pm
by vamsinadella
I WANT:

s1 - select box
s2 s3 s4 - select boxes but dependent on s1. for example, if i select a value a froms1, the s2, s3, s4 should show me values pertaining to s1.

for ex - consider this: you have a "car make" select box, "model" select box and "type" select box

first you select Honda from make....then model should only show Accord, Civic, element...etc... and type should only show Sedan, Coupe, truck etc...whatever...

MIne is Something similar.

So first i have to get the selected value of the make right? (in my case s1) so i'm writing a JS function to get this value. Perfect. the function works and the value is stored in a variable $chosen IN THE JS FUNCTION. BUt i need tis value outside the JS function to use it to populate the lists s2, s3, s4.

In simple words all i want is this value $chosen, returned by the JS function to be available for me while i'm populating the select boxes s2, s3, s4.

Does this make any sense.......

Thanks.

- vamz

Posted: Wed Jan 31, 2007 12:59 pm
by feyd
Yep, that's a chained select.

Posted: Wed Jan 31, 2007 1:01 pm
by louie35
using AJAX will be your best option as was mentioned above.

Posted: Wed Jan 31, 2007 6:34 pm
by RobertGonzalez
There is a sloppier way which uses more overhead but no AJAX. That is to literally select all records and put them into the secondary select boxes and when the first one changes, tie the content of the 2nd, 3rd and 4th boxes to the id of the first one. I have done this before (I wouldn't recommend it as a viable alternative in high traffic cases) and I know it works, but given the proliferation of newer technologies (namely AJAX) I would try to venture into something like that.

Posted: Wed Jan 31, 2007 8:21 pm
by feyd
psst, Ajax has been around for eight years, if memory serves. :)

Posted: Wed Jan 31, 2007 8:52 pm
by Kieran Huggins
... at least eight!

jquery your way to ajax!

Posted: Wed Jan 31, 2007 11:25 pm
by RobertGonzalez
Sorry, I meant the much growing popularity of the XMLHTTPRequest stuff and the what not.

/ Sheesh, what a bunch of nitpicky code jockeys... :roll: :wink:

Posted: Thu Feb 01, 2007 1:02 am
by feyd
Everah wrote:Sorry, I meant the much growing popularity of the XMLHTTPRequest stuff and the what not.

/ Sheesh, what a bunch of nitpicky code jockeys... :roll: :wink:
I was referring to the acronym "buzzword-technology" too.. not the cleaning product. :P