How do I pass two variables to the onclick parameter?

-1

If I put only one variable works, but I also need to pass $ city to onclick, but I do not know how to concatenate correctly. (deleteVaga function)

 while($registros = $querySelect->fetch_assoc()){
       $id = $registros['id'];
       $cidade = $registros['cidade'];
       $empresa = $registros['empresa'];
       $setor = $registros['setor'];
       $remuneracao = $registros['remuneracao'];
       $beneficios = $registros['beneficios'];
       $nivel_estagio = $registros['nivel_estagio'];
       $vinculo = $registros['vinculo'];
       $processo_sel = $registros['processo_seletivo'];
       $contato = $registros['contato'];   

      echo '<button onclick="deleteVaga('.$id.'.','.'.$cidade.')"><i class="far fa-trash-alt" style="font-size:36px;color:#f00;"></i></button>';
       echo "</tr>';
    
asked by anonymous 06.11.2018 / 19:54

1 answer

1

In doubt do not concatene, interpole (or format) the string :

echo sprintf('<button onclick="deleteVaga(%d, \'%s\')">...</button>', $id, $cidade);

Interpolating could stay:

echo "<button onclick='deleteVaga({$id}, \"{$cidade}\")'>...</button>";

The syntax you used, '.$id.'.','.'.$cidade.' , does not even make sense. You concatenate a string with $id , concatenate a dot character, then comes a comma that is left in the code, then another dot character, then concatenate with $cidade , which would have its value out of the quotes in JS, generating syntax error.

If you still have questions, review the generated HTML and see if it matches what you wanted to generate.

    
06.11.2018 / 20:02