inside mysql db which is table employe in coloumn id_employee, some data have a leading zero, etc :
id_employee : 0378192918
empy_name : Daniweb

i need to get id_employee for delete employe in my aplication, then i using ajax method like this :

if(id) {
      alert(id);
      $("#deleteBtn").unbind('click').bind('click',function(){
         $.ajax({
            url: 'pegawai/delpeg.php',
            type:'post',
            data: {emp_id : id},
            datatype: 'json',
            success:function(response) {

return value is 378192918, the first number 0 not showing in alert box. why ? then how to get entire value id_employe from db ?

Recommended Answers

My bet is your variable "id" is a number and numbers don't have leading zero's.
https://www.w3schools.com/js/js_type_conversion.asp gives example of reporting the typeof statement to confirm.

Since the code that created the value id isn't shown, you'll have to …

Jump to Post

Did you perform the typeof to check prior to line 2's alert? Is it just the alert? I tried this and had leading zeros.

id = '00123456'
alert(id)
Jump to Post

Taking a hint from https://stackoverflow.com/questions/7523929/jquery-ajax-with-webmethod-removes-zero-from-front try forcing it to be a string.
ie, line 10 change id to '' + id.

I did not test this.

Jump to Post

All 8 Replies

in mysql data type for id_employe is varchar
the leading zero only missing with ajax onclick function, if i retrieve data with looping it will show full 10 digit complete with leading zero..

Did you perform the typeof to check prior to line 2's alert? Is it just the alert? I tried this and had leading zeros.

id = '00123456'
alert(id)

leadingzero.png

yes , that was alert for make sure the value from db is return , as u can see the picture above , when i retrive data id_employee is show entirely , i cant delete this employee because she had id_employee with leading zero. its mean there is no data 378192918 in db , it should be 0378192918, below is code for button delete carry data id_employee from looping result.

<a type="button" data-toggle="modal" data-target="#delEmployModal" **onclick="delEmploy('.$row['nip'].')**">
    <span class="glyphicon glyphicon-trash"></span> Delete</a>
  </ul>
</div>';

and below is js code for catch id_employee from php

function delEmploy(id = null) {

    if(id) {
      console.log(id);
      alert(id);
      $("#deleteBtn").unbind('click').bind('click',function(){
         $.ajax({
            url: 'pegawai/delpeg.php',
            type:'post',
            data: {nip : id},
            datatype: 'json',
            success:function(response) {
                if(response.success == true) {

the delete employe cant run because ajax function return id_employee without leading zero ..

Since line 2 return value is missing zero, i guess it will be the same if i change in line 10. Btw thanks for give me support to this thread still no luck for now..

Line 2? I supplied a quick 2 line example and it was fine. I wonder if your IF statement changed the string id to a numeric id as JS tends to do.

If so, make a copy of id to a temp object and use that for the if test and then id would be unscathed.

the problem solved , it was the quotes that must surround all part of number. so reffer to article that i found in internet just add backslash.

$id = $row['nip']; // 0378192919
onclick="editEmploy(\''.$id.'\')"> //'0378192919'

btw many thanks for ur time and effort to help me out of here, the first clue that u ever said to me is The Quotes

commented: PK field as integer but stored as varchar because they wanted leading zeroes - genius! What a load of phaff. Glad it's sorted +6
commented: Thanks for sharing this. It was Javascript changing it to a number after all. +15