Last active
May 22, 2019 16:26
-
-
Save Jany-M/91a5991a00821bfc9481ed79dc3e89bb to your computer and use it in GitHub Desktop.
[WP] Select con Comuni per x Regione italiana, in Ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Richiede la creazione ed il popolamente delle tabelle https://gist.github.com/Jany-M/7b75ac99f46aaf13ef04587e5e9dd882 | |
// Richiede la libreria JS Bootstrap Select https://github.com/silviomoreto/bootstrap-select | |
/* -------------------------------------------------------------------------------- | |
* | |
* functions.php | |
* | |
-------------------------------------------------------------------------------- */ | |
if(!defined('PREFISSO_TABELLA')) { | |
$prefisso = ''; // le tabelle non avranno nessun prefisso e saranno: regioni, province, comuni - altrimenti scegliere qualcos'altro tipo $wpdb->prefix.'rpc_' | |
define('PREFISSO_TABELLA', $prefisso); | |
} | |
function load_comuni_js() { | |
wp_enqueue_script( 'comuni_js', get_stylesheet_directory_uri().'js/comuni.js', array('jquery') ); // personalizzare il path | |
wp_localize_script( 'comuni_js', 'comuni_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); | |
} | |
if(!is_admin()) { | |
add_action('wp_enqueue_scripts', 'load_comuni_js', 99); | |
} | |
// Popola i Comuni dopo aver selezionato la Provincia | |
function custom_comuni(){ | |
global $wpdb; | |
$cod_provincia = $_POST['cod_prov']; | |
$sql = "SELECT * FROM ".PREFISSO_TABELLA."comuni WHERE cod_provincia = '".$cod_provincia."' ORDER BY comune"; | |
$results = $wpdb->get_results($sql); | |
$comuni = ''; | |
if(count($results) > 0) { | |
foreach($results as $result) { | |
$cod_comune = esc_html($result->cod_comune); | |
$nome_comune = esc_html(utf8_encode( $result->comune)); | |
$comuni .= '<option data-tokens="'.$cod_comune.'" value="'.$cod_comune.'">'.$nome_comune.'</option>'; | |
} | |
echo $comuni; | |
} | |
die(); | |
} | |
add_action( 'wp_ajax_comu', 'custom_comuni' ); | |
add_action( 'wp_ajax_nopriv_comu', 'custom_comuni' ); | |
/* -------------------------------------------------------------------------------- | |
* | |
* comuni.js | |
* | |
-------------------------------------------------------------------------------- */ | |
// togliere questa riga dal file .js ?><script> | |
$(document).ready(function(){ | |
var provincia = ''; | |
var provincia_testo = ''; | |
var comuni_testo=''; | |
$("#provincia_wrap").change(function(){ | |
provincia = $('#provincia_wrap li.selected a').attr('data-tokens'); | |
provincia_testo = $('#provincia_wrap li.selected .text').text(); | |
$("#comuni_wrap button").attr('title', 'Carico i Comuni…'); | |
$("#comuni").attr('title', 'Carico i Comuni…'); | |
$.post( | |
comuni_ajax.ajaxurl, { | |
action: "comu2", | |
cod_prov:provincia | |
}, | |
function(data) { | |
$("select#comuni").removeAttr("disabled"); | |
$("select#comuni").html(data); | |
$('#comuni').selectpicker('refresh'); | |
$("#comuni_wrap button").attr('title', 'Scegli…'); | |
$("#comuni_wrap button .filter-option").text('Scegli…'); | |
$("#comuni").attr('data-title', 'Scegli…'); | |
$("#comuni .bs-title-option").text('Scegli…'); | |
} | |
); | |
}); | |
$("#comuni_wrap").change(function(){ | |
comuni_testo=$("#comuni_wrap li.selected .text").text(); | |
$("input.comu_mail").attr('value',comuni_testo+' ('+provincia_testo+')'); | |
}); | |
}); | |
</script> <?php // togliere questa riga dal file .js | |
/* -------------------------------------------------------------------------------- | |
* | |
* file_di_template.php | |
* | |
-------------------------------------------------------------------------------- */ | |
?> | |
<!-- Provincia --> | |
<div class="form-group" id="provincia_wrap"> | |
<?php | |
// Carica Province Toscane | |
global $wpdb; | |
global $province_toscane; | |
$codice_regione = 09; // cercare il codie nella tabella del db https://gist.github.com/Jany-M/7b75ac99f46aaf13ef04587e5e9dd882 | |
$sql = "SELECT * FROM ".PREFISSO_TABELLA."province WHERE cod_regione = '".$codice_regione."' ORDER BY provincia"; | |
$results = $wpdb->get_results($sql); | |
if(count($results) > 0) { | |
foreach($results as $result) { | |
$province_toscane[] = array( | |
'cod_provincia' => $result->cod_provincia, | |
'provincia' => $result->provincia | |
); | |
} | |
} ?> | |
<label class="col-sm-2 control-label text-left">Provincia</label> | |
<div class="col-sm-10"> | |
<select id="province" name="submit_provincia[]" class="selectpicker show-tick form-control" data-live-search="false" data-title="Scegli…"> | |
<?php | |
foreach ($province_toscane as $val) { | |
?> | |
<option data-tokens="<?php echo esc_html($val['cod_provincia']); ?>" value="<?php echo esc_html($val['cod_provincia']); ?>"><?php echo esc_html($val['provincia']); ?></option> | |
<?php | |
} | |
?> | |
</select> | |
</div> | |
</div> | |
<!-- Comune --> | |
<div class="form-group" id="comuni_wrap"> | |
<label class="col-sm-2 control-label text-left">Comune</label> | |
<div class="col-sm-10"> | |
<select id="comuni" name="submit_comune[]" class="selectpicker show-tick form-control" data-live-search="true" data-title="Seleziona prima la Provincia" disabled></select> | |
</div> | |
</div> | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment