0

I am trying to get the values for the next drop-down from a database, which will be dependent on two previous drop-downs. The values for first two drop-downs are listed in the file itself. I want the second drop-down to be enable after selecting values from first, and similarly for third after second. Kindly help. HTML code below:

<form>
<select id="branch" name="branch" class="form-control" onchange="showUser(this.value)">
        <option value="">Select</option>
        <option value="1">Civil</option>
        <option value="2">Computer</option>
        <option value="3">Mechanical</option>
        <option value="4">Electronics and Telecommunications</option>
        <option value="5">Electrical and Electronics</option>
        <option value="6">Information Technology</option>
</select>
<select id="semester" name="semester" class="form-control" disabled>
      <option value="1">I</option>
      <option value="2">II</option>
      <option value="3">III</option>
      <option value="4">IV</option>
      <option value="5">V</option>
      <option value="6">VI</option>
      <option value="7">VII</option>
      <option value="8">VII</option>
</select>
</form>

jquery is:

<script>

     $(document).ready(function(){
      document.cookie = "s =hello" ;

     console.log('hello');

         $("#semester").attr("disabled", true);
         $("#branch").change(function(){
             $("#semester").attr("disabled", false); 

              $b = $('#branch').val();


              $("#semester").change(function(){
                $s = $('#semester').val();
                $("#sub_code").attr("disabled", false);
                console.log($s);

                if($s!=1||$s!=2)
                $s = $b+$s;

                <?php 

                 $s= $_COOKIE['s'];

                 $sql = "SELECT * FROM subjects WHERE sem_code=`$s`";

                ?>

             });   
         });
     });
     </script>

I did not run the query since it is not assigned properly yet.

5
  • 2
    post relevant code here, what u have tried Commented May 8, 2014 at 11:20
  • Post your code i will modify whatever require Commented May 8, 2014 at 11:23
  • 2
    I reckon you might have mistaken SO for a freelancing site. Btw, a hint for you - Lookup these following terms in google: Ajax+jquery+php and I'm sure you'll find a dozen tutorials Commented May 8, 2014 at 11:28
  • I think you need this - thesoftwareguy.in/multiple-dropdown-with-jquery-ajax-and-php Commented May 8, 2014 at 11:40
  • @RajSf It helped me.. I needed almost same thing. Thank you Commented May 9, 2014 at 5:37

1 Answer 1

1

You can't include php code in javascript , the first is executed on the server side, the second is executed on the client side which means that you can't re-execute only if you resend a request to the server, obviously this is usually done by submitting forms, BUT sometimes -like in your case- we don't want to reload the whole page for each request ! and for this purpose we use AJAX ajax sends post/get request to a specified php page which does the desired server-side tasks (updating data in the database, selecting some results from database, dealing with sessions maybe, etc...) try something like this:

var pathurl='/path/to/your/php/file';
var params={}; //the parameters you want to send
params['semester']=$s;
var requestData= $.ajax({
        type: 'POST',
        url: pathurl,
        cache: 'false',
        data: params,
        dataType: "json",
        beforeSend: function () {
            //here you can begin an animation or anything...
        },
        complete: function () {
            //here you can stop animations ...

        },
        success: function (response) {
            console.log(response); //check your console to verify the response
            //loop other elements inside response
            $.each(response, function (index, resultArray) {
            //do something : in your case append to dropdown a new option
            });
        }
 });
   requestData.error(function () {
   alert("error");
});

you should create a php file with the path specified in the above code, and there you can extract what you need from the database table, store values in an array and finally use:

echo json_encode($resultArray);

hope this was helpful

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

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.