How to pass two arguments in Javascript’s onclick function, programming in PHP?

Asked

Viewed 730 times

1

I have a button that Gero with AJAX and this same button has an event onclick, and I want to pass two arguments to him, but I’m having bugs with him.

PHP code:

 echo ' <button type="button" class="btn btn-link btn-comentario-reposta" id="botao-esconde-resposta-'.$_POST['id'].'"
            onclick="acao_botao_esconde_resposta('.$_POST['id'].','. $btn_mostrar_resposta .')">Esconder respostas</button>';

That shows you where the button should be Hide answers.

  • that echo is the result of your ajax ?

  • echo will be returned to ajax to write to html.

2 answers

3


Your problem is that the '' of his arguments, correcting his code he gets like this :

echo ' <button type="button" class="btn btn-link btn-comentario-reposta" id="botao-esconde-resposta-'.$_POST['id'].'"
            onclick="acao_botao_esconde_resposta(\''.$_POST['id'].'\',\''. $btn_mostrar_resposta .'\')">Esconder respostas</button>';

How it works :

To escape some special character like "" the quotation marks or '' simple quotes we use to pass arguments within the functions we use the \ backslash so that in the code it considers the character as part of the string, an example that can best sclacer is the following :

$id = $_POST['id'];
$email = $_POST['email'];
$html = '<button id="btn-'.$id.'" onclick="func_teste('.$id.','.$email.')"></button>';
echo $html;

This example would be your case where the error happens and why ? Well when this code goes to html it will be generated this way for example:

<!-- código html -->
<button id="btn-1" onclick="func_teste(1,[email protected])"></button>
<!-- código html -->

As we can see the parameter passage is wrong and so your code is printing )">Esconder respostas to avoid this problem has to put the \"\" quotation marks or quotes \'\' Simple quotes to make your escape and so they become literal.

$id = $_POST['id'];
$email = $_POST['email'];
$html = '<button id="btn-'.$id.'" onclick="func_teste(\''.$id.'\',\''.$email.'\')"></button>';
echo $html;

And the result will be like this :

<!-- código html -->
<button id="btn-1" onclick="func_teste('1','[email protected]')"></button>
<!-- código html -->
  • change the two .$_POST['id']. to . $_POST["id"].

  • That’s right, we missed the simple quotes. + 1

  • What would be the meaning of using the backslash and single quotes?

  • @Murilohass I edited to reply to your comment, sorry if the n edition got very good but is q I did by cell phone since I’m not near a computer

  • I understood thanks for the help.

1

Personally I think that in your case it is simpler to write html as html and put only what comes from php with <?=

<?php
$id = $_POST["id"];
?> 
<button type="button" class="btn btn-link btn-comentario-reposta" 
    id="botao-esconde-resposta-<?=$id?>"             
    onclick="acao_botao_esconde_resposta('<?=id?>','<?=$btn_mostrar_resposta?>')">
          Esconder respostas
</button>

Browser other questions tagged

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