Uncaught ReferenceError

0

Good Day, I have a sweet alert feature:

function myIdNo() {
    swal("Ooops", "No existe!", "error");
}

And in an if I execute it like this:

if ($sql === FALSE) {
     echo '<script type="text/javascript">
           myIdNo();
           </script>';
}

but when I give to execute the query I get this error:

  

Uncaught ReferenceError: myIdNo is not defined       at Load.php: 2

Thanks in advance.

    
asked by Kygo 20.02.2018 в 23:57
source

2 answers

1

Hello, I recommend you do the following:

Use the exclamation mark that closes at the beginning of your variable, because if it is Boolean it is enough to add it to indicate that you are denying it

if(!$sql) {
     echo "<script>
            myIdNo();
           </script>"
}

It is also important to mention that if you are working with PHP as it is assumed by the tag of the question, your variable needs the sign of weights at the beginning.

    
answered by 21.02.2018 в 01:34
-1

You should have your conditional in this way;

if (sql === FALSE) {
     echo '<script type="text/javascript"> myIdNo();</script>';
}

not as you have it;

if (sql) === FALSE) {
     echo '<script type="text/javascript">
           myIdNo();
           </script>';
}

First of all, your if has a redundant parenthesis just after the "variable" sql and the chain of your echo should be on 1 line.

Based on the fact that the previous example did not work, I mention something I did based on your problem, declare the function before the code exited, I'll give you an example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        function myIdNo(){};
    </script>
</head>
<body>

</body>
<?= 
$sql = true;
if ($sql) {
     echo '<script type="text/javascript"> myIdNo();</script>';
}
 ?>
</html>

In the end the error is because the variable has not been declared and this is why the console throws you the error you mention;

    
answered by 21.02.2018 в 00:11