Dropdown in moodleform & addelement html concat with variable

Re: Dropdown in moodleform & addelement html concat with variable

by Nilesh Pathade -
Number of replies: 0

In my form there are lot of fields. in that I want two dropdown which is depending on each other. 

1st drop down onchange event 2nd drop down option change. and I have done this by using addElement('html' .....) 

like. : 


$attributes = array();
$mform->addElement('selectwithlink', 'location', get_string('location', 'format_classroom'), $key, array('onchange' => 'javascript:myFunctionToDoSomething("'.$CFG->wwwroot.'",this.value);') , array('link' => $CFG->wwwroot.'/course/format/classroom/manage_location.php?cid='.$course->id, 'target' => '_blank', 'label' => get_string('addlocation','format_classroom')) );
$mform->addRule('location', get_string('required'), 'required', null, 'client');
$mform->addElement('html','<div class="form-group row  fitem" id="yui_3_17_2_1_1521544987967_544">
                <div class="col-md-3">
                    <span class="pull-xs-right text-nowrap">
                        <abbr class="initialism text-danger" title="Required"><i class="icon fa fa-exclamation-circle text-danger fa-fw " aria-hidden="true" title="Required" aria-label="Required"></i></abbr>
                    </span>
                    <label class="col-form-label d-inline " for="id_classroom">
                        Classroom Name
                    </label>
                </div>
                <div class="col-md-9 form-inline felement" id="classroom_dropdown" data-fieldtype="select" id="yui_3_17_2_1_1521544987967_543">
                    <select class="custom-select" name="classroom">');
$mform->addElement('html','<option value="">Select Classroom</option></select>
<div class="form-control-feedback" id="id_error_classroom" style="display: none;">
                    
</div>
</div>
</div>');
//JS File.
function myFunctionToDoSomething(path,value){

	$.ajax({
	  url: path + "/course/format/classroom/getClassroom.php?location_id="+value,
	  cache: false,
	  success: function(html){

	    $("#classroom_dropdown").html(html);
	  }
	});
}

There is JS file hase fuction which return html form getClassroom.php files

<?php
	require_once('../../../config.php'); 
	global $PAGE, $CFG, $DB, $OUTPUT;
	$location_id  = optional_param('location_id', 0, PARAM_INT);
	$classrooms = $DB->get_records_sql('select id,classroom from {classroom} where location_id= ? AND isdeleted != ?',array($location_id,0));
	echo "<select class='custom-select' name='classroom'><option value=''>Select Classroom</option>";
	foreach($classrooms as $classr){
	    echo "<option value=".$classr->id.">".$classr->classroom."</option>";
	}
	echo "</select>";

But this is not working when I am using same for edit same form.