我是php和javascript的新手。我想知道这个函数是如何工作的。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
function sendPushNotification(id){
echo ('I am in Send Push Nottification');
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>
Q)我的问题是这个函数如何发送数据php文件像(http://uitdevelopers.site40.net/ClientServer/?name=qasim)
编辑这里是send_message.php
<?php
if (isset($_GET["regId"]) && isset($_GET["message"])) {
$regId = $_GET["regId"];
$message = $_GET["message"];
include_once 'gcm.php';
$gcm = new GCM();
$registatoin_ids = array($regId);
$message = array("price" => $message);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
}
?>
这是我的php文件,我想执行它,但我不能理解这个函数
编辑这是我的index。html。我想在哪里调用这个函数但这行不通。它是静态工作,但当我动态发送数据时不能工作
<!DOCTYPE html>
<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(){
});
function sendPushNotification(id){
echo ('I am in Send Push Nottification');
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>
</head>
<body>
<?php
require_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( $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="sendPushNotification( $row["id"])"/>
</div>
</form>
</li>
<?php }
} else { ?>
<li>
No Users Registered Yet!
</li>
<?php } ?>
</ul>
</div>
</body>
</html>
$.ajax
表示从对象$
调用ajax方法
此方法将使用http GET方法,这意味着数组data
将被附加到URL send_message.php
后问号?
只要url不以/
或协议(如http://),当前协议)开头,就会使用主机名和路径,因此最终字符串将是PROTOCOL://HOST[:PORT]/PATH/send_message.php?form_field_name=form_field_value&form_field_name2=form_field_value2
success
,结果为
它收集数据从你的表单在客户端使用的jquery的$('selector').serialize();
和得到所有的数据在你的php然后如果你的服务器端没有错误,它抛出回你的客户端显示所有的数据通过使用success
如果它是成功的或error
如果它失败了。Ajax用于在不重新加载页面的情况下查询结果。