Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: form
<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 30-Mar-13 2:58am
bmw318mt1.3K
Edited 3-Apr-13 8:53am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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>>
<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,
  Permalink  
Comments
bmw318mt at 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 at 3-Apr-13 16:09pm
   
Are u asking about sending SMS Notification?
bmw318mt at 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 at 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?
bmw318mt at 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)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100