gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:23:31 27 4
gpt4 key购买 nike

我有一个表单需要发送到 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 即可,或者您也可以使用 curl将数据发送到 OneSignal 后,将数据 POSTajax.php

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

关于javascript - 如何将单个 html 表单发送到 OneSignal 服务器和另一个服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com