0

I created 2 files: 1. post.php 2. ajax.php

i want to select 2 select box to show the related chart. the select box is coded in post.php. The data query is in the ajax.php file. i want to code the select box logic in ajax.php so that the chosen select box data will be sent to post.php using ajax call.

but, I don't know how to call the variable selected value of select boxes in ajax.php to run the function there.

Anyone can help me?

this is the code from post.php

            //combo box options to select post filter
             echo 'Posts of : ';
             echo '<select id="post-filter">';
             echo '<option value="0" selected="selected"> Select </option>';
             echo '<option value="1">Job</option>';
             echo '<option value="2">Internship</option>';
             echo '</select>';


             echo '&nbsp;&nbsp;&nbsp;';

            //combo box options to select group filter
             echo 'Category : ';
             echo '<select id="field-filter">';
             echo '<option value="0" selected="selected"> Select </option>';
             echo '<option value="1">Company</option>';
             echo '<option value="2">Location</option>';
             echo '<option value="3">Jobs Category</option>';
             echo '<option value="4">Salary</option>';
             echo '<option value="5">Experience</option>';
             echo '<option value="6">Level of Education</option>';
             echo '</select>';
            ?>

function change1() {
        var listbox1 = document.getElementById("post-filter");
        var selIndex1 = listbox.selectedIndex;
        var selValue1 = listbox.options[selIndex1].value;
        var selText1 = listbox.options[selIndex1].text;

        }


    function change2() {
        var listbox2 = document.getElementById("post-filter");
        var selIndex2 = listbox.selectedIndex;
        var selValue2 = listbox.options[selIndex2].value;
        var selText2 = listbox.options[selIndex2].text;

        }

this is the ajax.php file to get the chosen data value

if (selValue1 == '1') {
     if (selValue2 == '1') {

                x = CompanyData;
                y = optionsCompany;

                }

     if (selValue2 == '2') {
                   x = LocationData;
                    y = optionsLocation;
                }

      if (selValue2 == '3') {
          x = CategoryData;
          y = optionsCategory;
        }


      if (selValue2 == '4') {
          x = SalaryData;
          y = optionsSalary;
        }

      if (selValue2 == '5') {
          x = ExperienceData;
          y = optionsExperience;
        }


        if (selValue2 == '6') {
          x = LevelData;
          y = optionsLevel;
        }

 }


 elseif (selValue1 == '2') {

    if (selValue2 == '1') {

                x = CompanyData;
                y = optionsCompany;

                }

     if (selValue2 == '2') {
                   x = LocationData;
                    y = optionsLocation;
                }

      if (selValue2 == '3') {
          x = CategoryData;
          y = optionsCategory;
        }


      if (selValue2 == '4') {
          x = SalaryData;
          y = optionsSalary;
        }

      if (selValue2 == '5') {
          x = ExperienceData;
          y = optionsExperience;
        }


        if (selValue2 == '6') {
          x = LevelData;
          y = optionsLevel;
        }
 }

can i use this?

$(document).ready(function() {
        $('select[name="post-filter"]').change(function(){
        var select1 = $(this).val();
        $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {select1: select1},
                dataType: 'php'
                 });
            });
        });
joun
  • 656
  • 1
  • 8
  • 25
  • Add your post.php and ajax.php file code to your question, also add what you have tried so far and what problem you face. Until then we can't help you. – Saroj Aug 29 '17 at 04:26
  • What I have understood, you want to show a chart by choosing two select box value? – Saroj Aug 29 '17 at 06:48
  • yes, just my concern right now, i cannot take the select box value to the ajax.php to be used. the whole code would be quite lengthy. – joun Aug 29 '17 at 06:52

1 Answers1

0

Post.php

 <?php
                 //combo box options to select post filter
                 echo 'Posts of : ';
                 echo '<select id="post-filter">';
                 echo '<option value="0" selected="selected"> Select </option>';
                 echo '<option value="1">Job</option>';
                 echo '<option value="2">Internship</option>';
                 echo '</select>';


                 echo '&nbsp;&nbsp;&nbsp;';

                //combo box options to select group filter
                 echo 'Category : ';
                 echo '<select id="field-filter">';
                 echo '<option value="0" selected="selected"> Select </option>';
                 echo '<option value="1">Company</option>';
                 echo '<option value="2">Location</option>';
                 echo '<option value="3">Jobs Category</option>';
                 echo '<option value="4">Salary</option>';
                 echo '<option value="5">Experience</option>';
                 echo '<option value="6">Level of Education</option>';
                 echo '</select>';
   ?>
   <script>
      $(document).ready(function() {
        // for post-filter
        $('#post-filter').on('change',function(){
        var select1 = $(this).val();  // Post filter value
        var select2 = $("#field-filter").val(); // Field Filter value
        $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {selValue1 : select1,selValue2 :select2 },
                success: function(result){
                   console.log(result); // what ever the ajax call response we got from ajax.php
                }
            });
        });
     // we need to do the same for field filter value.
     $('#filed-filter').on('change',function(){
        var select2 = $(this).val();  // Field filter value
        var select1 = $("#post-filter").val(); // post Filter value
        $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {selValue1 : select1,selValue2 :select2 },
                success: function(result){
                   console.log(result); // what ever the ajax call response we got from ajax.php
                }
            });
        });
   </script>

ajax.php will be the same as you have done.

Saroj
  • 1,343
  • 4
  • 15
  • 31
  • You can do the change as you did or on change as I did. – Saroj Aug 29 '17 at 07:19
  • i already put your code, and when i select the combo box, the ajax posted in network. so when i refresh the ajax.php it should have the value right? – joun Aug 29 '17 at 07:34
  • i just notice i change the variable name 'select1'..so already edit to selValue1 but still not working. should be select1 is selValue1 in ajax.php right? – joun Aug 29 '17 at 08:04
  • Yes select1 should be selValue1 – Saroj Aug 29 '17 at 08:32