Dropdown list

I have two dropdown lists with the following code :



If I click on one of the dropdown lists the other dropdown list should get the same value. Than I will use the value as the number of products per page. Is this possible in PHP ?

Thanks in advance

Seems possible…

Could you tell me how ?

Sure here’s a demo…

Click the run button on this page and change the first drop down value.


But that’s Javascript. Is this also possbile in PHP ?

Yeah it’s possible, but why would you want to cause a post back when you can simply do it with javascript?

Because I just want to know how to do this in php. Can you tell me ?

On the post back you have to read the $_post value of the first drop down and then when you create the html form again you set the selected value of the first and second drop down.

What do you mean with creating the html form again ?

Every post back regenerates the HTML Form and you can control how that is regenerated with PHP If/Then statements to select different options based on user input…

So what I mean by creating the HTML form again, is simple a postback of the webpage and your code can choose to display whatever it wants in html where you will manipulate your html with php to show what you want it to show.

Look into AJAX tutorials. I understand you want to do it PHP but that is not the best practice. Unless you want to learn AJAX (Javascript) and have that tie to you PHP code you will loose visitors when a page needs to load everytime they change a value in the dropdown.

I know it is better to do it in Javascript but I first want to learn PHP. The page must reload because the value of the dropdown list sets the number of products on one page.

But how can I cause a postback if I click on the dropdownlist. I would appreciate it if you give me an PHP example.

Because PHP is a server side language, you are either looking at adding a button, at least for now to cause the postback, or you are looking at JS to cause the page reload, which as already stated is where you should be doing this anyway.

Add the select control and put a button next to it for the postback… I have seen sites that do this. Then go through again and correct it.

So it isn’t possible in PHP without a submit button.

Do you know the differences between server side and client side languages?

Yes I know now. Thank you.

Yep, Submit button or JavaScript to cause the Postback, so you can do it in php…

Now I have the following code :

[php]$options = array (
12 => 12,
24 => 24,
36 => 36,
48 => 48

echo"<form name = 'form' action = 'indexbody.php' method = 'GET'>
	<select name='amount'>";

	foreach($options  as $key => $value)
		$selected = $_GET['amount'] == $key ? 'selected' : '';
		if($key == 24 && !$_GET['amount'])
			$selected = 'selected';
		echo"<option value = '".$key."' ".$selected.">".$value."</option>";
	<input type = 'submit' name = 'submit' value = 'zend'>
		echo "test: ".$_GET['amount'];[/php]

If I click on the value 36 in the dropdown list and I go to the next page, the value of the dropdown list should stay 36. But it changes back to 24. What should I do ?

Try Changing

[php]if($key == 24 && !$_GET[‘amount’])[/php]


[php]if($key == 24 && !isset($_GET[‘amount’]))[/php]

That doesn’t help. I already solved it by setting the amount variable in the function where the buttons are created :

[php]function setButtons() {

$page = $_GET['page'];
$pages = $_GET['pages'];
$serv = $_GET['service'];
$amount = $_GET['amount'];

if($page > 1) {
echo"<a class='a' href='indexbody.php?service=$serv&page=1&amount=$amount'><div id='first'>|< </div></a>";
echo"<a class='a' href='indexbody.php?service=$serv&page=".($page - 1)."&amount=$amount'><div id='previous'> < </div></a>";

if($pages > 10)
$left_range = 8;
$left_range = 9;

if($page >= 10)
$right_range = 1;
$right_range = 10 - $page;
for($i = ($page - $left_range) ; $i <= ($page + $right_range); $i++) {
	if($i > 0 && $i <= $pages) {		
		if($page == $i)
		echo"<a class='a'><div id='selected'>$i</div></a>";
		echo"<a class='a' href='indexbody.php?service=$serv&page=$i&amount=$amount'><div id='p'>$i</div></a>";

if($page < $pages) {
echo"<a class='a' href='indexbody.php?service=$serv&page=".($page + 1)."&amount=$amount'><div id='next'> > </div></a>"; 
echo"<a class='a' href='indexbody.php?service=$serv&page=$pages&amount=$amount'><div id='last'> >| </div></a>";


This is my own solution. I don’t know if there is an other solution.

Sponsor our Newsletter | Privacy Policy | Terms of Service