我需要从php调用javascript或jquery

时间:2016-05-18 08:19:52

标签: javascript php jquery ajax

我需要刷新页面,并根据第一个下拉列表中选择的值填充第二个下拉列表。

  1. 在PHP中,我选择下拉值。它调用Javascript函数reloadproject()并使用当前值重新加载页面。
  2. 然后我使用$ _GET获取项目ID的值。
  3. 重新加载后,我需要使用$ _GET获得的项目ID调用函数getblock(val)并填充第二个下拉列表。
  4. function reloadproject(val) {
    window.location = 'market?id=' + val;
    }
    
    function getblock(val) {
    $.ajax({
    	type: "POST",
    	url: "list_flat_view_dd.php",
    	data: 'project_id=' + val,
    	success: function(data) {
    		$("#block-list").html(data);
    	}
    });
    }
    
    function getflat(val) {
    $.ajax({
    	type: "POST",
    	url: "list_flat_view_dd.php",
    	data: 'block_id=' + val,
    	success: function(data) {
    		$("#flat-list").html(data);
    	}
    });
    }
    <div class="col-sm-3">
    <select name="project" style="width: 100%;" id="project-list" class="form-control select2" 
    onChange="reloadproject(this.value);">
    <option value="all">Select</option>
    <?php while ( $project=m ysqli_fetch_array ( $results_project ) ) { ?>
    <option value="<?php echo $project[" id "]; ?>"<?php if(isset($_GET[ 'id'])){if($project[ "id"]==$ filter_project_id){ ?>selected="selected"
    	<?php }} ?>>
    	<?php echo $project[ "name"]; ?>
    </option>
      <?php } ?>
    </select>
    <?php if(isset($_GET[ 'id'])){ ?>
    <input type="hidden" name="filter_project_id" id="filter_project_id" value="<?php echo $filter_project_id; ?>">
    <?php } ?>
    </div>
    
    <div class="col-sm-3">
      <select name="block" style="width: 100%;" id="block-list" class="form-control select2" onChange="getflat(this.value);">
    <option value="all">Select</option>
      </select>
    </div>
    
    <div class="col-sm-3">
      <select name="flat" style="width: 100%;" id="flat-list" class="form-control select2" onChange="">
    <option value="all">Select</option>
      </select>
    </div>

2 个答案:

答案 0 :(得分:0)

您是否尝试使用'document ready'来调用'getblock'函数?:

首先运行PHP:在文档中打印代码,然后使用jQuery $(document).ready(),您知道文档已打印,并且您已经使用GET ['id']隐藏了输入值:

<input type="hidden" name="filter_project_id" id="filter_project_id" value="<?php echo $filter_project_id; ?>">

你的剧本:

$(document).ready(function() {

    if ($('#filter_project_id').val().length) {
         getblock($('#filter_project_id').val());
    }

});

答案 1 :(得分:0)

首先,dropbox重新加载页面并使用$ _GET启用另一个具有project-id的输入框,这将调用ajax和jquery函数来填充第二个drop box而不重新加载。

        
      function reloadproject(val){
	  window.location='market?id='+val;
      }
	  $(function() {
	    if ($('#filter_project_id').val().length) {
	      var project_id = $("#filter_project_id").val();
	      if (project_id) {
	        $.ajax({
	          type: "POST",
	          url: "list_flat_view_dd.php",
	          data: 'project_id=' + project_id,
	          success: function(data) {
	            $("#block-list").html(data);
	          }
	        });
	      }
	    }
	  });
<div class="col-sm-3">
  <select name="project" style="width: 100%;" id="project-list" class="form-control select2" onChange="reloadproject(this.value);">
    <option value="all">Select</option>
    <?php while ( $project=m ysqli_fetch_array ( $results_project ) ) { ?>
    <option value="<?php echo $project[" id "]; ?>"<?php if(isset($_GET[ 'id'])){if($project[ "id"]==$ filter_project_id){ ?>selected="selected"
      <?php }} ?>>
      <?php echo $project[ "name"]; ?>
    </option>
    <?php } ?>
  </select>
</div>
<?php if(isset($_GET[ 'id'])){ ?>
<input type="hidden" name="filter_project_id" id="filter_project_id" value="<?php echo $filter_project_id; ?>">
<?php } ?>
<div class="col-sm-3">
  <select name="block" style="width: 100%;" id="block-list" class="form-control select2" onChange="getflat(this.value);">
    <option value="all">Select</option>
  </select>
</div>