Quotes inside quotes to send string parameter

Asked

Viewed 3,389 times

4

The PHP code line below should be displayed on the website and is called using Javascript.

        echo "
        <div class='tipoPagamento' onclick='SelecionarTipoPagamento({$row['CODIGO']}, '{$row['TIPO']}');'>
            <font color='#fff'>
                <h4 style='margin-top: -15px;'>{$row['ESPECI']}</h4>
            </font>
        </div>
    ";

In the second line, in the tag div.. I have to call the function 'Selectpopagamento(int, str)'. As I indicated, it has an integer parameter and another string.. so I put the variable $Row['TYPE'] in quotes. But the function is not being called, because it is in error.

Removing the string parameter all works well and the code looks like this:

            echo "
        <div class='tipoPagamento' onclick='SelecionarTipoPagamento({$row['CODIGO']});'>
            <font color='#fff'>
                <h4 style='margin-top: -15px;'>{$row['ESPECI']}</h4>
            </font>
        </div>
    ";

I believe I do not know how to use the quotation marks correctly, but from what I saw on the Internet is correct... which would be the mistake?

I’m using the following scripts: (I’m specifying because I’ve heard that some do not accept single quotes or something like that)

<script src="js/jquery.min.js"></script>
    <script src="js/database.js"></script>
    <script src="js/jquery.poptrox.min.js"></script>
    <script src="js/jquery.scrolly.min.js"></script>
    <script src="js/jquery.scrollex.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="js/skel.min.js"></script>
    <script src="js/init.js"></script>
    <script src="js/jquery.mask.js"></script>
    <noscript>
        <link rel="stylesheet" href="css/skel.css" />
        <link rel="stylesheet" href="css/style.css" />
        <link rel="stylesheet" href="css/style-wide.css" />
        <link rel="stylesheet" href="css/style-normal.css" />
    </noscript>
    <script src="js/jquery-1.11.2.min.js"></script>
    <script src="js/jquery.maskedinput.min.js"></script>

The function 'Selector()' is this:

function SelecionarTipoPagamento(cod, tipo){
alert("oi");
alert(cod);
alert(tipo);
codTipoPagamento = cod;}

2 answers

4

I already solved the problem, after much thinking I realized that the problem is not in php itself, but in the string that HTML runs, because HTML would look like this:

onclick='funcao('str');'

And this generates simple quotes inside simple quotes, to work then the code was:

echo "
        <div class='tipoPagamento' onclick=\"SelecionarTipoPagamento({$row['CODIGO']}, '{$row['TIPO']}');\">
            <font color='#fff'>
                <h4 style='margin-top: -15px;'>{$row['ESPECI']}</h4>
            </font>
        </div>
    ";

1

Close the quotes in the function and then open again. Really, this could be the mistake.

echo "<div class='tipoPagamento' onclick='".SelecionarTipoPagamento($row['CODIGO'])."'>
            <font color='#fff'>
                <h4 style='margin-top: -15px;'>{$row['ESPECI']}</h4>
            </font>
        </div>
    ";
  • It didn’t work. it seems that it calls the function instead of the general HTML with the string... since it returns the error: "Call to Undefined Function Selectpopagamento() in ... "

Browser other questions tagged

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