Created
June 5, 2023 00:21
-
-
Save dantetesta/a35126d0d9446b7f864af6624d689071 to your computer and use it in GitHub Desktop.
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
<script> | |
jQuery(document).ready(function(){ | |
jQuery.ajax({ | |
url: '/wp-content/uploads/sistema-petshop/select-servicos.php', | |
type: 'GET', | |
dataType: 'json', // Tipo de dado esperado do servidor, nesse caso, um objeto JSON | |
success: function(data) { | |
var dropdown = jQuery("#servicos"); | |
// Adicionando um item padrão no início | |
dropdown.append(jQuery("<option />").val('').text('Selecione...').data('valor', '')); | |
jQuery.each(data, function() { | |
dropdown.append(jQuery("<option />").val(this.value).text(this.text).data('valor', this.valor)); | |
}); | |
}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
console.log(textStatus, errorThrown); | |
} | |
}); | |
jQuery('#servicos').change(function() { | |
var selectedOption = jQuery(this).find('option:selected'); | |
var valor = selectedOption.data('valor'); | |
jQuery('#valor').val(valor); | |
}); | |
}); | |
</script> | |
<?php | |
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' ); | |
global $wpdb; | |
$prefix = $wpdb->prefix; | |
$resultado = $wpdb->get_results("SELECT * FROM {$prefix}jet_cct_servicos ORDER BY nome ASC", ARRAY_A); | |
$data = array(); | |
if ( ! empty( $resultado ) ) { | |
foreach ( $resultado as $item ) { | |
$data[] = array( | |
'value' => $item['_ID'], // Substitua _ID com a coluna correta da sua tabela | |
'text' => $item['nome'], // Substitua 'nome' com a coluna correta da sua tabela | |
'valor' => $item['valor'], // Substitua 'valor' com a coluna correta da sua tabela | |
); | |
} | |
} | |
// Retornando dados no formato JSON | |
echo json_encode($data); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment