SyntaxError: unterminated string literal JAVASCRIPT

1

I'm having trouble with this line of javascript , I'm dynamically getting data from PHP .

fnc_eglise_ajaxGet('ajax/deletaPessoaVinculo.php?d=<?php echo eglise_dataDeDMAParaAMD($vinculo->DAT_INICI_VINCU); ?>&p=PV&a=1&pb=<?php echo $w_COD_IDENT_PESSO; ?>&c=<?php echo $vinculo->COD_TIPOX_VINCU; ?>');

When attempting to mount this error appears on the console of javascript and the page does not load.

  

SyntaxError: unterminated string literal

What could be the problem?

    
asked by anonymous 25.01.2016 / 18:39

3 answers

3

Buddy, I would do this to make it easier to see things in your code.

printf('fnc_eglise_ajaxGet("ajax/deletaPessoaVinculo.php?%s");', http_build_query(array(
     'd' => $vinculo->DAT_INICI_VINCU,
     'p' => 'PV',
     'c' => $vinculo->COD_TIPOX_VINCU
)));

Although some criticize the use of sprintf and printf , I prefer to have something that is more organized, and easy to maintain and understand.

The http_build_query will transform the array of php into querystring data.

So, it would transform this:

array('nome' => 'Wallace Maxters', 'profissao' => 'Programador PHP')

For this:

nome=Wallace+Maxters&profissao=Programador+PHP

See the code working at IDEONE

The result of my expression would result in the following PHP string:

'fnc_eglise_ajaxGet("ajax/deletaPessoaVinculo.php?d=1&p=PV&c=alguma+coisa");'

PHP and JAVASCRIPT

According to the desire of the author of the question, I elaborated on how the method mentioned above would be used together with the javascript you want.

It would look like this:

$('#btnSalvaDelete').click(function () { 
   <?php
        printf('fnc_eglise_ajaxGet("ajax/deletaPessoaVinculo.php?%s");',
            http_build_query(array(
                'd' => $vinculo-> DAT_INICI_VINCU,
                'p' => 'PV',
                'c' => $vinculo-> COD_TIPOX_VINCU,
            )
        )); 
    ?>
 });
    
25.01.2016 / 18:53
1

If you are getting data from the server to create a URL, you must "url-encode" the data. For example, if the data you are looking for has a ' , you will get an error like what you are getting, since the string will be terminated before you want it. urlencode is your friend here.

<?php echo 'fnc_eglise_ajaxGet("ajax/deletaPessoaVinculo.php?d=',
    urlencode(eglise_dataDeDMAParaAMD($vinculo->DAT_INICI_VINCU),
    '&p=PV&a=1&pb=',
    urlencode($w_COD_IDENT_PESSO),
    '&c=',
    urlencode($vinculo->COD_TIPOX_VINCU),
    '");' ?>
    
25.01.2016 / 18:49
-2

Hello, try doing the following:

fnc_eglise_ajaxGet('ajax\/deletaPessoaVinculo.php?d=<?php echo eglise_dataDeDMAParaAMD($vinculo->DAT_INICI_VINCU); ?>&p=PV&a=1&pb=<?php echo $w_COD_IDENT_PESSO; ?>&c=<?php echo $vinculo->COD_TIPOX_VINCU; ?>');

I put an escape in /

    
25.01.2016 / 18:43