0

I know this going to be a very basic question, but please be patient with me as I have just started using js and jquery.

Here is my part of code -

<html>
<head>
</head>
<body>
<form method="post" action="module.php">
 <select name="dateRange" size="1" onchange="window.location=this.options[this.selectedIndex].value">
       <option value > Select Duration </option>
       <option value="/module.php/?dateRange=1d">Last 24 Hours</option>
       <option value="/module.php/?dateRange=2d">Last 2 Days</option>
       <option value="/module.php/?dateRange=1w">Last Week</option>
       <option value="/module.php/?dateRange=2w">Last 2 Weeks</option>
       <option value="/module.php/?dateRange=1m">Last Month</option>
       <option value="/module.php/?dateRange=3m">Last 3 Months</option>
       <option value="/module.php/?dateRange=6m">Last 6 Months</option>
       <option value="/module.php/?dateRange=1y">Last Year</option>
       <option value="/module.php/?dateRange=all">All</option>
</select>
</form>
</body>
</html>

now I am not understanding how to write a onchange function where I can do all my query and dispplay my result when user selects any value from the dropdown list. And after that I would like to store the selected value in a variable and use that value in the same page ?

0

4 Answers 4

2

Lets say you give an id called mySelect to your select input.

Then you can define:

<script type="text/javascript">

$('#mySelect').on('change', function () {
    // do your thing
});

</script>

The above attaches an onchange event (jQuery) to your element and will be triggered every time a user changes the selection.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried doing the same ... I created a fiddle It runs once and than gives error there.. but on browser it dose not show alert msg and moreover when I select any of the option from the dropdown list it changes the url but again shows "select duration"
@Satish Are you sure? Your fiddle works perfectly :)
@Satish Only thing that happens is that your selection fires the value. What exactly do you want to present ?
no fiddle is not working properly, and atleast with fiddle it is showing alert msg but on browser it does not do that even
@Satish unfortunately php is outside my scope of expertise, but to get the value you should write $('#mySelect').val() where I wrote "do your thing".
|
1

you will use a handler

 <html>
 <head>
 </head>
 <body>

 <form method="post" action="module.php">
   <select name="dateRange" size="1" onchange="window.location=this.options[this.selectedIndex].value">
      <option value > Select Duration </option>
      <option value="/module.php/?dateRange=1d">Last 24 Hours</option>
      <option value="/module.php/?dateRange=2d">Last 2 Days</option>
      <option value="/module.php/?dateRange=1w">Last Week</option>
      <option value="/module.php/?dateRange=2w">Last 2 Weeks</option>
      <option value="/module.php/?dateRange=1m">Last Month</option>
      <option value="/module.php/?dateRange=3m">Last 3 Months</option>
      <option value="/module.php/?dateRange=6m">Last 6 Months</option>
      <option value="/module.php/?dateRange=1y">Last Year</option>
      <option value="/module.php/?dateRange=all">All</option>
 </select>
</form>

 <script>
 $( document ).ready(function() {

 $( "select" )
    .change(function () {
   alert('OnChange happened');
    //you can add your stuff here
    });
  })

 });
  </script>
</body>
</html>

1 Comment

what part is confusing? i tried to create the handler using the same form code given in the question (i guess it will make it easier for the user to understand where / how he can add the handler code), but i can edit it if you prefer
0

I have created you a fiddle :)

it uses

$('#duration').on('change', function () {
  var selectedEl = $('#duration').val();
    console.log(selectedEl);
});

To detect a change in select and val() to get the selected option.

Comments

0
Instead of this :
   <select name="dateRange" size="1" onchange="window.location=this.options[this.selectedIndex].value">
 Try This :
   <select name="dateRange" size="1" onchange="submit();">

And in 'MODULE.php' get the selected value using :
  $var = $_POST['dateRange'];
  echo $var;

And then write your query..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.