0

I'm trying to create a small chat application but for the sake of minifying the bytes being transferred is there any other way on writing this javascript that is less heavy than this code?

Here is my javascript:

function sendChatText() {

                if (sendReq.readyState == 4 || sendReq.readyState == 0) {
                    sendReq.open("POST", 'includes/getChat.php?last=' + lastMessage, true);
                    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    sendReq.onreadystatechange = AjaxRetrieve();  
                    var param = 'message=' + document.getElementById('txtA').value;
                    param += '&name='+user;
                    param += '&uid='+uid;
                    param += '&rid='+document.getElementById('trg').value;
                    sendReq.send(param);
                    document.getElementById('txtA').value = '';
                }                           
            }

Can this also be done on a JSON format too? because I think some says that json is lighter.. not sure though

here is my php code

$con = new PDO("mysql:host=". db_host .";dbname=chat_db", db_username , db_password);
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM users WHERE id = :rid LIMIT 1";
$stmt=$con->prepare($sql);
$stmt->bindValue( 'rid',$_POST['rid'], PDO::PARAM_STR);
$stmt->execute();
    foreach($stmt->fetchAll()as $res)
        {
            $usern = $res['username'];
            $user_lvl = $res['ulvl'];
        }
$text=$_POST['message'];
$sql4 = "INSERT INTO $tblname2(msgid,username,message_content,message_time,recipient)VALUES(:aid,:a,:b,NOW(),:c) ";
                    $stmt5 = $con2->prepare($sql4);
                    $stmt5->bindParam(':aid',$tblpre,PDO::PARAM_STR);
                    $stmt5->bindParam(':a',$_POST['name'],PDO::PARAM_STR);
                    $stmt5->bindParam(':b',$text,PDO::PARAM_STR);
                    $stmt5->bindParam(':c',$usern,PDO::PARAM_STR);
                    $stmt5->execute();
2
  • Yes it can be done in JSON.. but what are you expecting from us.. Writing completed code??? Commented Apr 7, 2014 at 6:38
  • @RiteshChandora I'm not asking for a completed code I'm just asking for a criticism and suggestion on my code for me to make my application lighter.. Commented Apr 7, 2014 at 6:45

2 Answers 2

1

As user2401175 saies. Why not use a framework, thats what they are here for.

jQuery is really simple and easy to understand.

You could try adding this, just before your "" tag.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Under this include of jQuery, you may now use the jQuery Post method to do your ajax request.

Sign up to request clarification or add additional context in comments.

Comments

0

In html Use

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

then Create javascript object like this

var changePacket = {
date1:value1,
data2:value2
}

And send Ajax request

$.ajax({
    url: "phpFile.php",
    dataType: 'json',
    type: 'POST',
    data: {json:JSON.stringify(changePacket)},
    success: function(response) {
     alert('hip hip hurray');
    },
    error: function(response) {
     alert('some thing wrong happend');
    }
});

In php

$json = $_POST['json'];
$data = json_decode($json);

now user your variable like this $date->data1 and $date->data2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.