我想在javascript中跟踪表单上的提交按钮单击事件并将提交按钮单击事件发送到curl PHP端,curl php文件中的url(https://trackcmp.net/event)将发送我触发的事件并存储到数据库中(ActiveCampaign)。
该表单由 ActiveCampaign 创建并嵌入到 Wordpress 网站中。
下面是我制作的 2 个文件,第 1 部分是 ajax 文件,第 2 部分是 PHP 文件。
有人可以帮我吗?谢谢你。
(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);
}
?>