I want to track the button click event which is Submit button in javascript to curl PHP side and the url in php file will send the event that I trigger and stored into the database.
Below are 2 files that I make, the 1st part is ajax, and then 2nd part is PHP.
May anyone help me please? Thank you.
(function($){
$("_form_44__submit").on('click', function () {
// fire the AJAX request on button click
$.ajax({
type: "POST",
url: 'curl.php',
dataType: 'json',
headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",},
data: {actid: "actid",
key: "key",
event: "Click_Submit_Button_Event",
visit: ["email"]
},
})
.done(function (response) {
console.log(response);
// if you want to do something on success
})
.fail(function (xhr, status, error) {
console.log('error');
// if you want to do something on error
});
});
})
<?php
$actid = $_POST['actid'];
$key = $_POST['key'];
$event = $_POST['event'];
$visit = $_POST['visit'];
$url = "https://trackcmp.net/event";
$curl = curl_init(); //open connection /
// changes the cURL session behavior with options POST
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'actid' => $actid,
'key' => $key,
'event' => $event,
'visit' => $visit,
));
//execute
$result = curl_exec($curl); //handle $curl_init
if ($result !== false) {
$result = json_decode($result);
if ($result->success) {
echo 'Success! ';
} else {
echo 'Error! ';
}
echo $result->message;
} else {
echo 'cURL failed to run: ', curl_error($curl);
}
?>