如何将单个 html 表单发送到 OneSignal 服务器和另一台服务器

How do I send a single html form to OneSignal server and another Server

我有一个表单需要发送到 OneSignal,然后发送到另一台服务器上的 mySql 数据库

<form action="AppPushUserOne.php" method="post">
    <textarea class="form-control" id="Message" name="Message" rows="5" placeholder ="Please insert your Incident and Deployment location here..."></textarea>
    <br>
    <button type="submit" class="btn btn-success btn-lg btn-block">Send Push Notification Message</button>
    <button onclick="postTimeEvent();" class="btn btn-success btn-lg btn-block">Send DB</button>
</form> 

提交按钮转到 OneSignal,第二个按钮转到另一台服务器上的 mySql 数据库。我将如何组合这两个按钮?我认为将 type="submit" 和 onclick="postTimeEvent(); 添加到同一个按钮是不明智的。

php我正在使用

    <?php                           
        function sendMessage($message){

            $content = array(
                "en" => $message
            );

            $headings = array(
                "en" => 'Pembina User here'
            );

            $fields = array(
                'app_id' => "be8a65fa-xxxx-xxxx-xxxx-xxxxxxxxxxx",
                'ios_badgeType' => 'SetTo',
                'ios_badgeCount' => '1',
                'included_segments' => array('All'),
                'headings' => $headings,
                'contents' => $content
            );

            $fields = json_encode($fields);

            print("\nJSON sent:\n");
            print($fields);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                )
            );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

            $response = curl_exec($ch);
            curl_close($ch);

            return $response; 
        }

        $response = sendMessage($_POST['Message']);

        $return["allresponses"] = $response;
        $return = json_encode( $return);

        print("\n\nJSON received:\n");
        print($return);
        print("\n");
    ?>

我正在使用的 js 表单

    <script>
        function postTimeEvent() {
            var event_log = document.getElementById("Message").value;
            var dataString = 'event_log1=' + event_log;
            if (event_log == '')
            {
                alert("Please Fill All Fields");
            }
            else
            {
                $.ajax({
                    type: "POST",
                    url: "ajax.php",
                    data: dataString,
                    cache: false,
                    success: function(html) {
                        alert(html);
                    }
                });
            }
            return false;
        }       
    </script>

理想情况下,AppPushUserOne.php 中的 PHP 代码应该负责将相同的消息发送到 MySQL 数据库。您可以通过简单地将 ajax.php 文件中的代码附加到 AppPushUserOne.php 来完成此操作,或者您可以使用 curlPOST 将数据添加到 ajax.php完成发送到 OneSignal。

但是,如果您希望通过 JavaScript 执行此操作,则必须在提交表单之前调用 postTimeEvent() 函数,否则 AJAX 调用 ajax.php 可能不会被触发。为此,请为您的 form(例如 myForm)设置 id,为您的按钮设置 type="button",一旦 AJAX 调用成功,submit 你的表格。

<form id="myForm" action="AppPushUserOne.php" method="post">
    <textarea class="form-control" id="Message" name="Message" rows="5" placeholder ="Please insert your Incident and Deployment location here..."></textarea>
    <br>
    <button type="button" onclick="postTimeEvent();" class="btn btn-success btn-lg btn-block">Send Push Notification Message and Send to DB</button>
</form>

<script>
    function postTimeEvent() {
        var event_log = document.getElementById("Message").value;
        var dataString = 'event_log1=' + event_log;
        if (event_log == '')
        {
            alert("Please Fill All Fields");
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: dataString,
                cache: false,
                success: function(html) {
                    alert(html);
                    $('#myForm').submit();
                }
            });
        }
        //return false;
    }       
</script>