Admin Ajax Chain Select
-
I want to implement an ajax chained select list on the Admin post page.
The idea is to relate the post to a country and city. However, when I try to run the script, it keeps giving me a security error and the ajax doesn’t work.
Can someone help? Here’s the code:
FUNCTIONS.PHP
=============function related_country(){
global $post;
$product = $custom[“country”][0];
?><p><label>country: <select id=”country” name=”country”>
<option value=”United Kingdom”>United Kingdom</option>
<option value=”United States”>United States</option>
<option value=”Australia”>Australia</option>
<option value=”Japan”>Japan</option>
</select></label>
<label>city: <select id=”city” name=”city”>
</select></label></p>
<?php
}function plugin_admin_head_js() {
print “
<script type=’text/javascript’ src=’chainjs.js’></script>”;
print “
<script type=’text/javascript’>
window.onload=initCs;
</script>
“;
}
add_action(‘admin_head’, ‘plugin_admin_head_js’);THE JS FILE:
===========var request = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e2) {
request = false;
}
}
@end @*/
if (!request && typeof XMLHttpRequest != ‘undefined’) {
request = new XMLHttpRequest();
}function fillSelect(country) {
var url = “ajax.php?country=” + escape(country);
request.open(“GET”, url, true);
request.onreadystatechange = go;
request.send(null);
}function go() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
var list=document.getElementById(“city”);
var cities=response.split(‘|’);
for (i=1; i<cities.length; i++) {
var x=document.createElement(‘option’);
var y=document.createTextNode(cities[i]);
x.appendChild(y);
list.appendChild(x);
}
}
}
}function initCs() {
var country=document.getElementById(‘country’);
country.onchange=function() {
if(this.value!=””) {
var list=document.getElementById(“city”);
while (list.childNodes[0]) {
list.removeChild(list.childNodes[0])
}
fillSelect(this.value);
}
}
fillSelect(country.value);
}———————
The error console reports it as a security error accessing ajax.php from WordPress Admin.
- The topic ‘Admin Ajax Chain Select’ is closed to new replies.