【发布时间】:2014-05-17 16:41:19
【问题描述】:
<script type="text/javascript">
$(document).ready(function(){
});
function sendPushNotification(id){
var data = $('form#'+id).serialize();
$('form#'+id).unbind('submit');
$.ajax({
url: "send_message.php",
type: 'GET',
data: data,
beforeSend: function() {
},
success: function(data, textStatus, xhr) {
$('.txt_message').val("");
},
error: function(xhr, textStatus, errorThrown) {
}
});
return false;
}
</script>
<body>
<?php
include_once 'db_functions.php';
$db = new DB_Functions();
$users = $db->getAllUsers();
if ($users != false)
$no_of_users = mysql_num_rows($users);
else
$no_of_users = 0;
?>
<div class="container">
<h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
<hr/>
<ul class="devices">
<?php
if ($no_of_users > 0) {
?>
<?php
while ($row = mysql_fetch_array($users)) {
?>
<li>
<form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')">
<label>Name: </label> <span><?php echo $row["name"] ?></span>
<div class="clear"></div>
<label>Email:</label> <span><?php echo $row["email"] ?></span>
<div class="clear"></div>
<div class="send_container">
<textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
<input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/>
<input type="submit" class="send_btn" value="Send" onclick=""/>
</div>
</form>
</li>
<?php }
} else { ?>
<li>
No Users Registered Yet!
</li>
<?php } ?>
</ul>
</div>
</body>
在上面的代码中,我们将一个 id 发送到 java 脚本并发送 msg ..
所以想一次将 ID 的数组发送到 JAVASCRIPT 函数并在 javascript 函数中打印值。
不,我不需要整个表格..
但我想使用一个表单、一个提交按钮和一个文本字段发送到多个设备。
提前致谢……
这是我们接受消息的表单屏幕
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>
<script type="text/javascript"> $(document).ready(function(){ });
</script>
</head>
<body>
<form name="puski" method="post" action="send_message.php">
<input type="text" name="message"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
这是读取所有 id 并将消息发送到 GCM.php 的文件
<?php if (isset($_POST["message"])) {
$message = $_POST["message"];
include_once './GCM.php';
$gcm = new GCM();
include_once 'db_functions.php';
$db = new DB_Functions();
$users = $db->getAllUsers();
while ($row = mysql_fetch_array($users)) {
$gcm_array[]=$row['gcm_regid'];
}
$message = array("price" => $message);
$result = $gcm->send_notification($gcm_array, $message);
echo $result;
} ?>
我们有 GCM.php
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of GCM
*
* @author Ravi Tamada
*/
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
}
?>
我们仍然得到错误
Fatal error: Call to undefined function curl_init() in C:\wamp\www\gcm_server_php\GCM.php on line 41
请帮忙
【问题讨论】:
-
总之,您要发布整个表单吗?
-
不,我不需要整个表单......但我想使用一个表单、一个提交按钮和一个文本字段发送到多个设备。
-
没有得到答案仍然开放..
标签: javascript php curl