onChange event

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
samir_gambler
Forum Newbie
Posts: 19
Joined: Wed Mar 31, 2010 9:03 am

onChange event

Post by samir_gambler »

Hi i am new to PHP and i have a combobox

Code: Select all

<select class="inputbox" name="orderby" >
<option value="product_list">Select</option>
<option value="product_name" selected="selected"> Product Name</option>
<option value="product_price"> Price</option>
<option value="product_cdate"> Latest Products</option>
</select>
I want to reorder by data displayed based on the option selected. I know it can be done through onchange event but can anyone tell me how to implement as i am new to javascript also. As the change is made page should be reloaded and how to pass the information to php variable, value of the new selected option .
Last edited by Benjamin on Wed May 19, 2010 5:06 am, edited 1 time in total.
Reason: Added [syntax=html] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: onChange event

Post by Benjamin »

:arrow: Moved to Javascript
Miyako
Forum Newbie
Posts: 1
Joined: Wed May 19, 2010 12:18 pm

Re: onChange event

Post by Miyako »

It's impossible to do it without refreshing the page because javascript and php are two different type of languages.
PHP at the first place is a server-language, executed when the server has to send the page to the client.
Javascript is a client-language, which will be executed in the client itself.

So when a javascript is running, the PHP is already finished and cannot be changed until you're communicating with the server again (for example, by refreshing a page).

The best solution I can get is changing the window.location and adding the variable to the GET-parameters. This, however will refresh the page ;)

Code: Select all

<html>
<head>
<script type="text/javascript">
	function goSelect() {
		window.location = "?var=" + orderby.value;
	}
</script>
</head>

<body>

<?php echo $_GET["var"]; ?>

<select class="inputbox" onchange="goSelect()" name="orderby" id="orderby">
<option value="product_list">Select</option>
<option value="product_name" selected="selected"> Product Name</option>
<option value="product_price"> Price</option>
<option value="product_cdate"> Latest Products</option>
</select>

</body>

</html>
Post Reply