简体   繁体   中英

Uncaught SyntaxError: missing ) after argument list ERROR ON JS

I'm building a table who have a button. When the user click this button the stat of row changes, in the table this represents a field with 0 or 1. Fine i'd made the button but i get an error in some rows, the error is Uncaught SyntaxError: missing ) after argument list, and i get very confused because the code works on like 80% of the time.

The problem wasn't with the database because i'd already searched for some field with problems.

function enviaAssociado(CTITULAR) {
    if (CTITULAR) {
        $("#CTITULAR").remove();
        $.ajax({
            url: 'php_action/enviaAssociado.php',
            type: 'post',
            data: {
                CTITULAR: CTITULAR
            },
            success: function() {
                manageTable.ajax.reload();
                //$("#CTITULAR").val(response.CTITULAR);
            }
        });
    }
    manageTable.ajax.reload();
}

PHP

`

$CTITULAR = $_POST['CTITULAR'];


$sql = "UPDATE importsmiles4e.tb_conv
SET    STAT = CASE
     WHEN  STAT = 1 THEN 0
     WHEN  STAT = 0 THEN 1
     ELSE STAT
   END
WHERE  substring(CTITULAR,2,6) = '$CTITULAR'
";
$query = $connect->query($sql);

$CTITULAR = $_POST['CTITULAR'];


$sql = "UPDATE importsmiles4e.tb_conv3
SET    STAT = CASE
     WHEN  STAT = 1 THEN 0
     WHEN  STAT = 0 THEN 1
     ELSE STAT
   END
WHERE  substring(CTITULAR,2,6) = '$CTITULAR'";
$query = $connect->query($sql);


// close the database connection
$connect->close();

echo json_encode($sql);

`

The Table

while ($row = $query->fetch_assoc()) {

$active = '';

if($row['EMAIL'] != '') {
        $active = '<label class="label label-success">Cadastrado</label>';
    } else {
        $active = '<label class="label label-danger">Cadastrar</label>'; 
    }


$botao = '<a type="button" class="btn btn-default" onclick="enviaAssociado('.$row['CTITULAR'].') ">Alterar</a>';

$status = '';

if($row['STAT'] == '0'){
    $status ='<label class="label label-warning">Não</label>';  
}else{
    $status ='<label class="label label-success">Sim</label>';
}

$output['data'][] = array(
    $row['NOME'],
    $row['CPF'],
    $row['CEPRES'],
    $row['NROPROPOSTA'],
    $row['DTADMISSAO'],
    $row['DEPENDENTES'],
    $row['VLSMENS'],
    $status,
    $botao,
);

https://i.stack.imgur.com/KnD7w.png => error Log

https://i.stack.imgur.com/MhsDh.png => Return when error

The return when success is the same the only change is the value key and i've already check if the values are broken.

The problem is buried away in one of your screenshots:

enviaAssociado(0516542.00)

That leading 0 combined with the fact that there are no 8 or 9 digits in it (yes, really ) makes that a legacy octal integer literal in loose mode. Since it's an integer literal, it can't have a decimal point, so as of the . it's a syntax error.

If that's meant to be a number, have your PHP code strip any leading 0 s from the number. If it's meant to be a string, have your PHP code output it in quotes.

If you output data to JavaScript code from PHP, you're usually best off doing so via json_encode . If you're doing it within an attribute (which is not best practice), you also want htmlspecialchars . So:

$botao = '<a type="button" class="btn btn-default" onclick="enviaAssociado('.htmlspecialchars(json_encode($row['CTITULAR'])).') ">Alterar</a>';

Sometimes, the number will contain an 8 or a 9 and so it'll work (that's why the example you gave in a deleted comment on this answer, 0655178.00 , worked). This works in loose mode, for instance:

enviaAssociado(0655178.00)

Notice the 8 . (A 9 would do the same thing.) It works because the 8 is in the part of the number prior to the . , so the JavaScript parser knows that it's not a legacy octal integer literal, instead it's a (try not to laugh here) non-octal decimal integer literal , which is a specific kind of a decimal integer literal , which means it can be the first part of a decimal literal , so the . isn't a syntax error. This is one of JavaScript's deep dark corners. (And a good reason to use strict mode, which disallows non-octal decimal integer literals.)

But this is just a specific case of where the real problem is not outputting from PHP to JavaScript code correctly (and not properly escaping something being put in an HTML context, specifically in this case an attribute). So although this legacy octal integer literal thing is a deep dark corner of JavaScript, that's not the real issue. The real issue is ensuring you encode things properly when outputting from PHP to JavaScript like that, via json_encode (and in this case, htmlspecialchars ).

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