Mismatch PHP and JAVASCRIPT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Mismatch PHP and JAVASCRIPT

Post by highjo »

Hello guys i was building an application that sends SMS from a page.I thought everything was cooland smouth before i realized that the javascript function that count character and its php server side doesn't do the same thing.i mea a total mismatch in counting character.
this is my javascript code that is triggered by onKeyDown and onKeyUp of the text area.this is the function

Code: Select all

 
function counter(field,maxlen, sms_2, sms_1)
{
   
    var obj = document.getElementById("indic");
    var smsobj = document.getElementById("smsno");
    
    var textArea   = field.value.length;
 
    if(textArea > maxlen)
    {
        field.value=field.value.substring(0, maxlen);
        smsobj.innnerHTML = "3/3";
    }
    else if(textArea <= maxlen && textArea > sms_2)
    {
        obj.innerHTML = maxlen - textArea;
        smsobj.innerHTML = "3/3";
    }
    else if(textArea <= sms_2 && textArea > sms_1)
    {
        obj.innerHTML = sms_2 - textArea;
        smsobj.innerHTML = "2/3";
    }
    else if (textArea <= sms_1 && textArea > 0)
    {
        obj.innerHTML =sms_1- textArea;
        smsobj.innerHTML = "1/3";
    }
    else
    {
               //obj, smsobj are div that show either character number and sms page number
        obj.innerHTML  = ' ';
        smsobj.innerHTML = ' ';
    }
}
 
the html textarea is like this

Code: Select all

 
<textarea name="txtMessage" id="txtMessage"  cols="40" rows="8"
                onblur="validate(this.value, this.id)" onkeydown="counter(this, 444, 291, 145);"  onkeyup="counter(this, 444, 291, 145);"><?php echo trim($_SESSION['values']['txtMessage']);?></textarea>
 
and this is the server side checking

Code: Select all

 
function checkSMS($msg, $maxlen,$sms2, $sms1)
            { 
                                //we append the company name for advertising
                $msg = $msg . ".From Mycompany";
                $msglength= strlen($msg);
                if($msglength > $maxlen)
                {
                    $msg = substr($msg,0,$maxlen - strlen($msg));
                    $sms['num'] = 3;
                }
        
                if($msglength > $sms2 && $msglength <= $maxlen)
                {
                    //set the array the number of sms count
                     $sms['num'] = 3;
                
                }
                elseif($msglength > $sms1 && $msglength <= $sms2)
                {
                    $sms['num'] = 2;
                
                }
                elseif($msglength > 0 && $msglength <= $sms1)
                {
                    $sms['num'] = 1;
                    
                }
                $sms['msg'] = $msg;
                return $sms;
            }
 
What i've noticed is that it has something to do with the enter key which is pressed to avoid maybe the horizontal scrollbar.i think there is a relationship between the number of time the enter key is pressed and the difference between the count.i mean the one from javascript and the one from php.
Does somebody has a better concept or can actually save my time coz very overwhelmed by this project
Post Reply