0

Im trying to send a values of "customer ID" to javascript function when I clicked any checkbox of customer's name.

It worked fine for most of the IDs, but some of the ID return a 'weird number' and im sure that's not a number which come from my database(json), more like just a random number.

Pict 1 is the correct output https://i.sstatic.net/u4tIc.png

Pict 2 is the wrong output https://i.sstatic.net/PEaIY.png

There's the code of the checkbox that will do passing the variable into the javascript function(checkFunction) :

Checkbox that return "customer ID" to javascript function (checkFunction(check, no))

<tbody>
  <?php for($i=0;$i<$length;$i++){ ?>
    <tr>
      <td scope="row"><input id="<?php echo $i; ?>" type="checkbox" 
onclick="checkFunction(<?php echo $json1[$i]['nik'];?>,
<?php echo $i; ?>,
<?php echo $json1[$i]['verify']; ?>)" >
</td>
      <td><?php echo trim($json1[$i]['nik']); ?></td>
      <td><?php echo $json1[$i]['nama']; ?></td>

    </tr>
<?php } ?>
  </tbody>

Here's the javascript function that will alert the length of customer ID and the ID (*ps : Please don't mind the if else condition because the ID did show fine with that condition too)

function checkFunction(check,no) {
  var checkBox = document.getElementById(no);
  var nik = check;
var verify = 1;
  if (checkBox.checked == true){
            if(nik.toString().length==5){ 
                alert(nik.toString().length + " " + nik + " " + verify);
            }
            else if(nik.toString().length==6){ 
                alert(nik.toString().length + " " + nik + " " + verify);
            }
            else if(nik.toString().length==7){ 
                alert(nik.toString().length + " " + nik + " " + verify);
            }

  }

Even some of the IDs that below the "0144214 DIRHAM" customer worked fine (I mean the error do skipping, and i cant find any different of the customer ID which would cause the error)

Sorry for my bad english and thanks

1
  • also in general try not to do this whenever possible. Use PHP to make an API that is consumed by the front end via JSON objects. There are plenty of tutorials about this that you can look up. Commented Aug 7, 2019 at 4:33

1 Answer 1

1

0144214 is valid octal, so is interpreted as 51340 decimal, on the other hand, 0143439 is not valid octal, so is interpreted as 143439

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

2 Comments

It solved!! so I just did convert the oct to string as you said, like octdec(decoct($json1[$i]['nik'])) and it worked fine Thank you so much
@CarinaNatalia that conversion doesn’t make much sense; all you really had to do was include quotes in the output you are generating, so that JS sees '0123' instead of 0123: onclick="checkFunction('<?php echo $json1[$i]['nik'];?>',…

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.