How do I pass two variables to the onclick parameter?

Asked

Viewed 169 times

-1

if I put only one variable works, but I also need to pass $city to the onclick, but I don’t know how to concatenate in the right way. (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>';

1 answer

1

In doubt do not concatenate, interpole (or format) a 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.', nor makes sense. You concatenate a string with $id, concatenates a dot character, then comes a comma that is left in the code, then another dot character, to then concatenate with $cidade, that would have its value out of quotes in JS, generating syntax error.

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

  • I need the ajax in another file to receive these two parameters

  • but keeps making a mistake

  • I am using single quotes to open the echo and double quotes to open the functions

  • Analyzed the generated HTML as suggested? What did you conclude?

  • yes, it generates the results in the table, but does not delete

  • Function deleteVaga(deleteid,city){ var conf = confirm('Do you really want to delete this wave?'); if(conf==true){ $.ajax({ url: "banco_de_data/delete.php", type: "post", date: {deleteid: deleteid, city: city}, Success: Function(data, status){ viewVagas(); } }); }&#Xa} }

  • sorry the code, came out as common text

  • If the generated HTML is correct and the function is called correctly, your question has been answered. If jQuery is also in trouble, I suggest opening another question.

Show 3 more comments

Browser other questions tagged

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