In the admin page of my WordPress plugin, I have a function that makes an ajax call to a PHP file. If everything goes well this PHP file should update the option in WordPress. But for some reason, it doesn't work when I try to use update_option.
Here is my activate.php:
if(isset($_GET['activate']) && $_GET['activate'] == "true") {
if(isset($_REQUEST['txtAC']) && isset($_REQUEST['txtKey'])) {
$ac = $_REQUEST['txtAC'];
$key = $_REQUEST['txtKey'];
if($ac != "" && $key != "") {
$api_url = "http://192.168.2.75/wouter/yii2/basic/web/cars/activate/" . $ac . "/" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $api_url);
$result = curl_exec($ch);
curl_close($ch);
$count = json_decode($result);
if($count == 1){
update_option("ac", $ac);
update_option("auth_key", $key);
echo "success";
} else {
echo "failed";
}
return;
} else {
echo "notSet";
return;
}
}
return;
}
So everything goes well until the update_option. When I put them in, nothing happens anymore and there's a server error.
What am I doing wrong?
EDIT
So here is my jquery ajax code:
function activatePlugin() {
jQuery("#acError").html("");
jQuery("#keyError").html("");
var txtAC = document.getElementById("txtAC").value;
var txtKey = document.getElementById("txtKey").value;
if(txtAC == "") {
jQuery("#acError").html("Vul een klantnummer in");
}
if(txtKey == "") {
jQuery("#keyError").html("Vul een key in");
}
if(txtAC != "" && txtKey != "") {
jQuery.ajax({
url: "../wp-content/plugins/autocommerce/admin/activatePlugin.php?activate=true",
method: "POST",
data: { txtAC : txtAC, txtKey: txtKey }
}).done(function(msg) {
alert(msg);
if(msg == "success") {
location.reload();
} else if(msg == "failed") {
jQuery("#activateError").html("Gegevens onjuist. Controleer uw gegevens en probeer het opnieuw.");
} else if(msg == "notSet") {
jQuery("#activateError").html("Een of meerdere velden zijn onjuist ingevuld.");
} else {
alert(msg);
jQuery("#activateError").html("Er is een fout opgetreden. Probeer het later opnieuw.");
}
});
}