Home › Forums › Backend Issues (wp-admin) › Plugin for ACF – Using Ajax JSON query states and cities
Good Afternoon , I am developing a plugin for the ACF that will query the database will mount with a SELECT OPTION listing the states , when you select the state will generate a SELECT with the cities , the PHP functions are all normal , I used the die () ; out and not run another AJAX WORDPRESS , could enter the field in the ACF typically generates the SELECT with the states , but in time to generate the SELECT with cities , AJAX gives an error : 500 (Internal Server Error) , I’m having trouble accessing the JSON , because I have a PHP script that generates the query into JSON , also tried putting in a function but without success , the file got getJson JavaScript seem to get the PHP file that generates the JSON and makes search and thus assembles the SELECT , below is my sample code :
Put this in the constructor :
add_action ( ' init ', array ( & $ this , ' init ' ) ) ;
if ( is_admin ()) {
add_action ( ' wp_ajax_nopriv_bairros_ajax ', array ( & $ this , ' bairros_ajax ' ) ) ;
add_action ( ' wp_ajax_bairros_ajax ', array ( & $ this , ' bairros_ajax ' ) ) ;
}
I’m in class :
public function init ( ) {
wp_enqueue_script ( ' acf - neighborhoods ' , plugin_dir_url ( __FILE__ ) . 'js / scripts.js ', array ( ' jquery ' ) ) ;
wp_localize_script ( ' acf - neighborhoods ', ' acf_bairros ', array (
' ajaxurl ' = > ADMIN_URL ( 'admin - ajax.php ' )
) );
}
so he can localixar admin – ajax.php to work with ajax and also sets my JS file , this is the function that generates my JSON : or resources may be in a PHP file as well:
bairros_ajax public function ( ) {
header (' Cache -Control : no- cache ' ) ;
header (' Content-type : application / xml ; charset = " utf - 8 " ', true ) ;
global $ wpdb ;
# $ uf = mysql_real_escape_string ( $ _REQUEST [' uf '] ) ;
$ cidadesArray = array ( ) ;
$ cities = $ wpdb - > get_results ( " SELECT * FROM { $ wpdb - > prefix} acf_cidades WHERE uf = 'GO ' ORDER BY city " ) ;
foreach ( $ towns $ city) :
$ cidadesArray [ ] = array (
' city_id ' = > $ city - > city_id ,
' uf ' = > $ city - > uf ,
'city' = > utf8_encode ( $ city - > city)
);
endforeach ;
# var_dump ( $ cidadesArray ) ; exit;
echo json_encode ( $ cidadesArray ) ;
Die ();
}
Follow my JS fetching JSON and mounts the SELECT cities :
jQuery (function ( ) {
jQuery ( ' . loading ' ) . hide () ;
jQuery ( '# uf ' .) change ( function () {
if ( jQuery (this) . val ( ) ) {
jQuery ( '# city ' ) . hide () ;
jQuery ( ' . loading ' ) . show () ;
jQuery.getJSON ( ' . . / wp -content / plugins / acf - neighborhoods / acf_bairros_ajax.php ? search = ' { uf : jQuery (this) . val ( ) , ajax : 'true' } , function ( j ) {
var options = ' <option> value=""> < / option> ' ;
for (var i = 0; i < j.length : i + +) {
options + = ' + <option> value="' j[i].id_cidade '"> + ' + j [ i ] . town + ' < / option> ' ;
}
jQuery ( '# city ' ) . html (options ) . show () ;
jQuery ( ' . loading ' ) . hide () ;
});
} Else {
jQuery ( '# city ' ) . html ( " <option> value=''> --- Choose a state --- < / option> " ) ;
}
});
});
within this PHP file acf_bairros_ajax.php have code that generates the JSON , everything goes well , generates the SELECT state , when I select the status of the script error and can not generate the cities in the state , please help me in this matter ? takes a lot Thanks ….
Sorry, but this question is a bit out of scope for the ACF support forum.
Are you having any specific ACF issues regarding a bug or function problem?
Or is this a general ‘how to’ question regarding WP AJAX?
Thanks
E
Thanks for replying, I have a problem with AJAX to interact with field of type CHOICE ACF, created the plugin for ACF but in time to interact with AJAX and JSON it’s a 500 error (Internal Server Error)
Good Day, I will summarize, I am developing a PLUGIN for the ACF, when I put the method create_field ($ field) I can save just one input field, how do I save multiple vampos INPUT and then fetch the values of the fields to update?
when I put the method create_field ($ field) I can save just one input field
– What do you mean? The create_field will render 1 field. What code are you using, what is the result, what are you expecting, what is going wrong?
how do I save multiple vampos INPUT and then fetch the values of the fields to update?
– Sorry, I don’t understand this question
Thanks
E
The topic ‘Plugin for ACF – Using Ajax JSON query states and cities’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.