-3

I want to use the javascript variable in php. When a select option is selected from dropdown it triggers change function and its value is obtained.i want to use this value in php to check the database.

Below is the code:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<script type="application/javascript">
$(document).ready(function(){$('select[name="dropdown"]').change(function(){

    var a=$(this).val() ;
        alert(a);



});
});



</script>

</head>

<body>

<select name="dropdown" size=1>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>
</html>
</body>

If Aneyone could help?

1 Answers1

2

use ajax like that :-

$.post('file.php', {variable: variableToSend});

code like that :-

    <script type="application/javascript">
    $(document).ready(function(){$('select[name="dropdown"]').change(function(){

        var a=$(this).val() ;
        $.post('file.php', {variable: a});
    });
});

On your server, you would need to receive the variable sent in the post:

$variable = $_POST['variable'];
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20