Syntaxerror: unterminated string literal JAVASCRIPT

Asked

Viewed 315 times

1

I’m having trouble with this line from javascript, mount her dynamically taking 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; ?>');

While trying to mount this error appears in the console of javascript and the page does not load.

Syntaxerror: unterminated string literal

What could be the problem ?

  • 1

    Probably the value of <?php echo eglise_dataDeDMAParaAMD($vinculo->DAT_INICI_VINCU); ?>, <?php echo $w_COD_IDENT_PESSO; ?> or <?php echo $vinculo->COD_TIPOX_VINCU; ?> has a ' that is "closing" the string. Take a look at the source code of the page in the browser, you will have more information about the problem.

  • The string was not closed, it may be a problem in the function or before/after that code.

  • If you’re mixing php with javascript, I’ll ask you to add the php tag to your question.

  • 1

    An important tip: in addition to mixing code, you are passing data to the url. Consider using functions such as http_build_query and urlencode php.

3 answers

3


Buddy, I would do it this way to facilitate the visualization of 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 php in querystring data.

Then he would turn it:

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

For that reason:

nome=Wallace+Maxters&profissao=Programador+PHP

See the code working on IDEONE

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

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

PHP and JAVASCRIPT

According to the wish of the author of the question, I elaborated how the method mentioned above would be used together with the javascript desired by the same.

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,
            )
        )); 
    ?>
 });
  • Who would criticize the sprintf()? now it’s clean :D.

  • @rray some people criticize, speaking of performance. What is useful to decrease the memory and readability to make the maintenance take 2 weeks?

  • Your code is giving problem and this error appears in the console.log: ReferenceError: invalid assignment left-hand side

  • Then it would look like this: $('#btnSalvaDelete'). click(Function() { printf('fnc_eglise_ajaxGet("ajax/deletaPessoaVinculo.php? %s");', http_build_query(array( ’d' = > $link - > DAT_INICI_VINCU, 'p' = > 'PV', 'c' = > $link - > COD_TIPOX_VINCU )); });

  • @Renanrodrigues no. Hold on I’ll post another Ideone.

  • Now it was this error that gave Syntaxerror: Missing ; before statement

  • @Renanrodrigues worked? I saw that marked the question.

  • Yes it did work

Show 3 more comments

1

If you are taking data from the server to create a URL, you must "url-Encode" the data. For example, if the data you are searching for has a ', you will get an error like what you are getting, since the string will be finished before you want it. The function 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),
    '");' ?>
  • 1

    It may solve the problem, but still legibility is impaired.

  • Brother did not work, netbeans is giving error in the final line

-2

Hello, try it this way:

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 the /

Browser other questions tagged

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