Delete result with JS and PHP

0

I have a script in js to delete sql results with PHP and JS ...

LINK

<a href="javascript:;" onClick="deletar(<?php echo $exibe['matricula']; ?>);">

JS

function deletar(matricula) {
    if (confirm("Tem certeza que deseja apagar?")) {
        $.ajax({
            type: "GET",
            url: "index.php?pg=associados",
            data: "&acao=deletar&matricula=" + matricula,
            success: function() {
                location.href = 'associados';
            }
        });
    } else {
        return false
    }
}

PHP

if($_GET['acao'] == "deletar"){
    $deletar = mysqli_query($conecta, "DELETE FROM socios WHERE matricula='".$_GET['matricula']."'");
}

What is happening is as follows, the column "enrolls" it is INT (5) so that the numbers are displayed in 5 numbers (00001). Until then the code works fine, however when I try to delete results above 00010 they do not delete.

Someone to help me?

    
asked by anonymous 15.12.2017 / 17:29

1 answer

2

First, I recommend that you use the str_pad in PHP for this display effect, as follows

str_pad($matricula, 5, "0", STR_PAD_LEFT);

The problem is happening because you did not put quotation marks around the link:

<a href="javascript:;" onClick="deletar(<?php echo $exibe['matricula']; ?>);">

And JavaScript interprets the number with octal

Test in your browser: If you run console.log (000010) you will see that it returns 8. So you need the console.log ('000010') quotes , so it is interpreted as a string.

See more at:

link

"Never write a number with a leading zero (like 07). Some JavaScript versions interpret numbers as octal if they are written with a leading zero. "

English:

"Never write a number with an initial zero (like 07)." Some versions of JavaScript interpret numbers as octal if they are written with an initial zero. "

    
15.12.2017 / 18:19