J. Doe J. Doe - 2 months ago 5
Ajax Question

AJAX request to other page not working

Currently I am trying to add content to my page using AJAX. Since it is my first time using AJAX I am not really sure about what I am doing. I wrote a bit of code here:

JavaScript:

jQuery(function(){
jQuery("#nearby_customers_link").click(function(){
jQuery.ajax({
url: template_url + "/ajax/test.php",
data: "nearby=" + jQuery(this).data("city"),
success: function(data) {
jQuery(".test").html(data).slideDown();
}
});
});
});


index.php:

echo"<a href=\"#\" data-nearby=\"".$city['city']."\" id=\"customers_link\">Blablablablablablabla</a>";

echo"<div class=\"test\"></div>";


test.php:

$city = $wpdb->get_results("SELECT * FROM `cities` WHERE `key` = '".esc_sql($_GET['city'])."'", ARRAY_A);
$customers = $wpdb->get_results("SELECT * FROM `customers` WHERE `city` IN('".implode("','", array_map("esc_sql", unserialize($city[0]['nearby'])))."') ORDER BY `company_name` ASC", ARRAY_A);

if(!empty($customers))
{
echo"<div id=\"nearby_customers\">";
echo"<h3>Blablablablabla</h3>";

foreach($customers as $customer)
{
echo city_customer($customer);
}

echo"</div>";
}

echo"</div>";


Now I get an error saying this:

Warning: array_map(): Argument #2 should be an array in C:\wamp\www\ajax\test.php on line 7

How do I fix this error?

Also I am not really sure what the data field means or does and I can't find it anywhere on the internet. If someone could explain a bit that would be great!

Answer

It looks like there is a mistake in your code. in test.php you are using variable $_GET['city'], but in fact the variable is called $_GET['nearby']. Try fixing that and seeing if it works.