Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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(){

        });
        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>
</head>
<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"  önclick=""/>
                            </div>
                        </form>
                    </li>
                <?php }
            } else { ?>
                <li>
                    No Users Registered Yet!
                </li>
            <?php } ?>
        </ul>
    </div>
</body><code><pre><pre lang="Javascript">
Posted
Updated 3-Apr-13 7:53am
v3

1 solution

Hello,

There are many things you can do like move the form, textarea outside of the while loop. Add ability to select users to whom message will be sent etc, I have modified the code which will allow you send message to selected users only. I have not changed the way data gets sent to the send_message.php. However by adding a checkbox your send_message.php will be able to find out the users to whom message needs to send. The post data should look something like shown below.
chk_0=chk_0&user_0=john435&user_1=admin&chk_2=chk_2&user_2=operator

The modified code is shown below.
HTML
<html>>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="gcm_style.css">
<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) {
    var data = $('#frmMain').serialize();
    $.ajax({
        url: "send_message.php",
        type: 'GET',
        data: data,
        beforeSend: function() {
        },
        success: function(data, textStatus, xhr) {
            $('#taMesage').val("");
        },
        error: function(xhr, textStatus, errorThrown) {
        }
    });
    return false;
 }
</script> 
</head>
<body>
<?
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;

if ($no_of_users > 0) {
    $cntr = 0;
?>
<form id="frmMain" name="frmMain" method="post" onsubmit="return sendPushNotification()">
<?
    while ($row = mysql_fetch_array($users)) {
        $userId = $row[0];         // Assuming that first field represents the user id
        $chkId = "chk_" . $cntr;
        $userFldId = "user_" . $cntr;
?>
    <div>
        <label>Name: </label><span class="textbox"><?=$row[1]?></span><br/>
        <label>Send Email :</label><input type="checkbox" id="<?=$chkId?>" value="<?=$chkId?>"/>
        <input type="hidden" name="<?=$userFldId?>" value="<?=$userId?>"/>
    </div>
<?
        $cntr = $cntr + 1;
    }?>
    <div>
        <label>Email:</label> 
        <textarea rows="3" name="message" cols="25" class="txt_message" id="taMesage"></textarea>
    </div>
    <input type="submit" class="send_btn" value="Send"/>
</form>
<?
} else { ?> 
No Users Registered Yet!
</body>
</html>


Regards,
 
Share this answer
 
Comments
datt265 3-Apr-13 13:58pm    
Thanks for your reply, I have modified a bit my question as, the php part in the html was not showing properly and missed important parts. Can you help me out please?

There are other questions related to my question but no answers available.
Googling "php code send notification to all devices GCM" I can see 2 other question.
Prasad Khandekar 3-Apr-13 16:09pm    
Are u asking about sending SMS Notification?
datt265 4-Apr-13 5:45am    
not exatly sms, push notification. The above code works, though if i want to send the same message to multiple users i have to do it one by one
Prasad Khandekar 4-Apr-13 9:21am    
How you are planning to deliver this message? For SMTP you can use BCC and send only one message. It depends on the delivery medium. What do you mean by push notification?
datt265 4-Apr-13 12:10pm    
here is a good example
ttp://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900