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

时间:2019-01-04 03:31:19

标签: javascript php onesignal

我有一个表单,该表单需要先发送到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> 

Submit按钮转到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>

1 个答案:

答案 0 :(得分:2)

理想情况下,AppPushUserOne.php中的PHP代码应该负责将相同的消息发送到MySQL数据库。您可以通过将代码从ajax.php文件附加到AppPushUserOne.php来完成此操作,也可以使用curl将数据POST ajax.php插入postTimeEvent()完成了将其发送到OneSignal。

但是,如果您希望通过JavaScript进行操作,则必须在提交表单之前调用ajax.php函数,否则可能不会触发对id的AJAX调用。为此,将form赋予myForm(例如type="button"),为按钮设置submit,一旦AJAX调用成功,<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> 您的表格。

{{1}}