0

我创建了 2 个文件: 1. post.php 2. ajax.php

我想选择 2 选择框来显示相关图表。选择框在 post.php 中编码。数据查询在 ajax.php 文件中。我想在 ajax.php 中编写选择框逻辑,以便使用 ajax 调用将选择的选择框数据发送到 post.php。

但是,我不知道如何调用 ajax.php 中选择框的变量 selected 值来运行该函数。

任何人都可以帮助我吗?

这是来自 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;

        }

这是获取所选数据值的 ajax.php 文件

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;
        }
 }

我可以用这个吗?

$(document).ready(function() {
        $('select[name="post-filter"]').change(function(){
        var select1 = $(this).val();
        $.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: {select1: select1},
                dataType: 'php'
                 });
            });
        });
4

1 回答 1

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 将与您所做的相同。

于 2017-08-29T07:00:35.640 回答