How to display quotes in echo in PHP?

I am developing an application that uses PHP + AJAX and came across a annoying problem. AJAX only recognizes the function if it looks like this:

onclick="remover_musica_ftp('0','Deposito de bebida'
);"

I'm using PHP to "print" these values in the browser like this:

echo "<a href='#excluir' class='button icon-trash' onclick=remover_musica_ftp('0','".$nomes."');  title='Excluir'></a>";

I have to use php because I'm doing a foreach. Until then it's okay, the problem is that when the word contains spaces, ajax simply does not respond, because the code would need to be like in the first example, only when I add the quotation mark " automatically PHP becomes invalid, because the same echo started with a quotation mark. Does anyone have any idea how I should proceed in this case? If I open echo with a ' it will give error the same way, as I would need to reverse the ajax onclick that would not allow ' in functions. Give me a suggestion!

 6
Author: Cassiano José, 2014-08-16

2 answers

You need to escape ".

$nomes = 'nomes...';
echo "<a href=\"#excluir\" class=\"button icon-trash\" onclick=remover_musica_ftp( 0 , \"$nomes\" ); title=\"Excluir\">link</a>";

Output

<a href="#excluir" class="button icon-trash" onclick=remover_musica_ftp( 0 , "nomes..." ); title="Excluir">link</a>


If the argument before the name is numeric, you don't need to use '

remover_musica_ftp( integer , "string" )
 5
Author: Papa Charlie, 2014-08-16 08:03:16

When you want to show the same type of quotation marks that you are using to enclose the string, you need to escape it using a slash \

$string = 'aspas simples: \' e aspas dupla: " ';
$string = "aspas simples: ' e aspas dupla: \" ";

A situation that can occur is you have to escape a quote inside an already escaped string, then you will need to put a escaped bar, as in the ex. below:

echo '<a onclick="remover_musica_ftp(0,\'It\\\'s Name Is\');"></a>';

// retorno:
<a onclick="remover_musica_ftp(0,'It\'s Name Is');"></a>
 6
Author: Jader A. Wagner, 2014-08-17 03:06:05