无法从下拉列表中将选定值提取到php变量中,并使用该变量的值来获取其他数据

时间:2018-02-09 04:43:29

标签: javascript php jquery ajax drop-down-menu

我有一个包含一些文本字段和2个下拉列表的表单,从第一个下拉列表中我需要获取所选值并将其存储在PHP变量中,以便我可以使用该变量来获取另一个集合数据进入第二个下拉列表。我不想使用提交按钮。 我尝试使用ajax,我在控制台中获取了所选的值,但是无法将其提取到PHP变量中。

//1st Drop-down
<dl>
  <dt>Project ID</dt>
  <dd> 
  <!-- Function to select the dropdown value -->
  <script type="text/javascript" src="<?php echo 
  url_for('/public/js/jquery.js'); ?>"></script>
          <script type="text/javascript">
          function fetch_select(val)
          {
             console.log(val);
             $.ajax({
               type: "POST",
               url: "/flat_booking_again/adminsAccess.php",
               data: {
               get_option:val
               }, success: function(response){
                   console.log(response);
                   document.getElementById("new_select").innerHTML=response; 
                   //alert(get_option);
               }
            });
          }

          </script>    
          <select name = "project_id" style ="width:150px" onchange="fetch_select(this.value);">
          <option>  
          <?php 
               $result = find_projects_without_admins();

               foreach($result as $row) { ?>
                 <option value="<?php echo $row['project_id']; ?>" selected="selected"><?php echo $row['project_id']; ?></option>
               <?php }?>

          </option>
          </select>        
          </dd>
          </dl>
      <dl>
      //I need to use the fetched data from above to populate this drop-down
      <dt>Block ID</dt>
      <dd>
      <select id="new_select" name="block_id" style ="width:150px"> 
      <option> 
      <?php
       echo $_POST['get_option'] . "hellllllllllllo"; //Not able to fetch value
        if(isset($_POST['get_option'])){
        $projectId = $_POST['get_option'];
        $result = find_blocks_without_admins($projectId);
        foreach($result as $row) { ?>
             <option value="<?php echo $row['block_id']; ?>" selected="selected"><?php echo $row['block_id']; ?></option>
           <?php }
        }
      ?>
      </option>
      </select> 

我是PHP和AJAX的新手,请原谅我任何愚蠢的错误。我知道这可能听起来像是一个重复的问题,但我长期坚持这个bug,任何帮助都非常感激。谢谢!

1 个答案:

答案 0 :(得分:-1)

用此更改您的代码。

<dl>
  <dt>Project ID</dt>
  <dd> 
  <!-- Function to select the dropdown value -->
  <script type="text/javascript" src="<?php echo url_for('/public/js/jquery.js'); ?>"></script>
  <script type="text/javascript">
      function fetch_select(val)
      {
          console.log(val);
          $.ajax({
            type: "POST",
            url: "/flat_booking_again/adminsAccess.php",
            data: {
            get_option:val
            }, success: function(response){
                console.log(response);
                document.getElementById("new_select").innerHTML=response; 
                //alert(get_option);
            }
        });
      }

      </script>    
    <select name = "project_id" style ="width:150px" onchange="fetch_select(this.value);">
          <option>  
          <?php 
               $result = find_projects_without_admins();

               foreach($result as $row) { ?>
                 <option value="<?php echo $row['project_id']; ?>" selected="selected"><?php echo $row['project_id']; ?></option>
               <?php }?>

          </option>
          </select>        
          </dd>
    </dl>
      <dl>
      //I need to use the fetched data from above to populate this drop-down
      <dt>Block ID</dt>
      <dd>
      <select id="new_select" name="block_id" style ="width:150px"></select> 

      <?php
       //echo $_POST['get_option'] . "hellllllllllllo"; //Not able to fetch value
        if(isset($_POST['get_option'])){
          $projectId = $_POST['get_option'];
          $result = find_blocks_without_admins($projectId);
          $html = '';
          foreach($result as $row) { ?>
             $html.="<option value='".$row['block_id']."' selected='selected'>".$row['block_id']."</option>";
          }
          echo $html;
        }
      ?>