Delete result with JS and PHP

Asked

Viewed 80 times

0

I have a js script 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 the following, the column "matricula" it is INT(5) so that the numbers are displayed in 5 numbers (00001). Until then the code works well, but when I try to erase results above 00010 they do not erase.

Someone to help me?

  • Number started with zero is octal, you should send the number without zeros if you need to format from a padding (add zeros to the left).

  • What do you mean, Everson, I don’t understand you

  • 1

    Didn’t solve little brother

1 answer

2


First point, I recommend using the function str_pad in PHP for this display effect, as follows

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

The problem is happening because you didn’t put quotes on the link:

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

And Javascript interprets the number as being octal

Test in your browser: Run yourself console.log(000010) you will see that it returns 8. That’s why you need the quotes console.log('000010'), thus interpreted as a string.

See more in:

https://www.w3schools.com/js/js_numbers.asp

"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."

Português:

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

  • Very good brother, I knew this case of using str_pad more for viewing and not on the bench, I wanted to try otherwise but it did not work, They fight for the attention Bruno, brother success!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.