简体   繁体   中英

converting integer to string through ajax

i have the following ajax code to update the password in my database table,

$.ajax({
    type: 'POST',
    url: root_url + '/services/services.php?method=updatesubpwd',
    data: { 
        'sid': ptcid, 
        'pwd': prtpwd // <-- the $ sign in the parameter name seems unusual, I would avoid it
    },
    //data: "sid=" + ptcid + "&pwd=" + prtpwd,
    async: true,

    success: function (data) {
        alert(prtpwd);
        if ($('.dynapwdblock') && $('.dynapwdblock').css('display')=="none") {
            $('#prepwd').val(prtpwd);
            $('.prtpwd').val(prtpwd);
            $('#prtpwdval').val(prtpwd);

            $('.apfnp').remove();
            $('.dynapwdblock').show(200);
            //alert(data);
        }

    },
    ...

everything seems working fine when i give the values for my prtpwd field in data of ajax, But in case of numbers when i give the numbers starting with "0" or starting with series of "0"s the first 0's are not storing in the database that means it is storing as Integer, can anyone tell me how should i change the function so as to accept the starting 0's to store in DB

 prtpwd=$('#prtpwd').val();

this is from where the ajax is getting the prtpwd value from.

and this is the function used to set the data into the DB

function updateSubPassword($subid, $pwd)
    {
        $query       = "UPDATE Sub SET Sign_pw=".$pwd." WHERE Sub_id=" . $subid;
        $queryresult = mysql_query($query);

        if ($queryresult) {
            return 1;
        } else
            return 0;
    }

the leading zero's are not saving in the DB though the data type is Varchar

If you have the column in you database set to 'integer' the zeros will get when you try and save to the db. change the column type to varchar or text or something similar (depending on what db you are using). Also make sure you are passing a string to the ajax call and not an integer. An easy way of doing that .....

var myString = ""
myString = myString + variableIThinkMightBeAnInteger

You can change the column in your database to fill it with zeros. just set the type to INT with the Length of max digits and attribute to UNSIGNED ZEROFILL .

Example:

ALTER TABLE `tablename`
CHANGE `pwdfield` `pwdfield` INT( 8 ) UNSIGNED ZEROFILL NOT NULL

This makes your password field a max of 8 digits and if only 4 are given, it is padded with zeros, ie if you get a number 1234 to put in the database, the cell will fill it to 00001234 . That's what the UNSIGNED ZEROFILL is for

When doing the query for updating the password, put single quotation marks around $pwd :

$query       = "UPDATE Sub SET Sign_pw='$pwd' WHERE Sub_id=$subid";

This should force the value to be a string. It may be worth to do the same with $subid too.

(Also, why don't you get advantage of the PHP double quotation mark notation?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM