0

我想在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);
    }


?>

4

1 回答 1

0

我假设 PHP curl 适合你,并且你有数据来填充 ajax 帖子中的值,所以:

为了使数据到达以 $_POST 形式发布 PHP,您需要像这样发送它,例如 $.ajax 代码:

$.ajax({
    type: "post",
    url: "curl.php",
    data: "actid=actid&key=somekey&event=Click_Submit_Button_Event&visit=email@at.com",
    success: function (data) {
        console.log('Submission was successful.');
        console.log(data);
    },
    error: function (data) {
        console.log('An error occurred.');
        console.log(data);
    },
});

看看 data: 的构建方式......你当然可以这样做:"actid="+actid+"&key="+key+ ... etc

这样您的 PHP 文件将能够读取表单数据。

如果你要打印你的 $_POST 它看起来像:

Array
(
    [actid] => actid
    [key] => somekey
    [event] => Click_Submit_Button_Event
    [visit] => email@at.com
)

在这里编辑是一个完整的版本,带有表单点击和输入:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<html>
    <body>
        <form id="contactForm1" action="curl.php" method="post">
            Actid <input type="text" name="actid" /><br>
            Key <input type="text" name="key" /><br>
            Event <input type="text" name="event" /><br>
            Visit <input type="text" name="visit" /><br>
            <input type="submit" />
            <!-- Form input fields here (do not forget your name attributes). -->
        </form>
    </body>
</html>



<script type="text/javascript">
    var frm = $('#contactForm1');

    frm.submit(function (e) {

        e.preventDefault();
        console.log(frm.serialize()); /*show the data being sent from the form*/
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                console.log('Submission was successful.');
                console.log(data);
            },
            error: function (data) {
                console.log('An error occurred.');
                console.log(data);
            },
        });
    });
</script>

对于你 PHP,我认为你误认为数组发送数据......你应该尝试这样的事情: curl.php:post 数据将变成一个 json,我怀疑这是你需要发送到你的端点的......它看起来像这样:

{"actid":"actid","key":"key","event":"Click_Submit_Button_Event","visit":"someEmail@at.com"}

<?php
$post = json_encode($_POST);
print_r($post);

$url = "https://trackcmp.net/event";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = json_decode(curl_exec($ch),true);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

echo '<pre>';
print_r($result);
于 2021-12-22T16:43:32.097 回答