Page 1 of 1

onChange event

Posted: Tue May 18, 2010 5:08 am
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 .

Re: onChange event

Posted: Wed May 19, 2010 5:06 am
by Benjamin
:arrow: Moved to Javascript

Re: onChange event

Posted: Wed May 19, 2010 12:28 pm
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>