简体   繁体   中英

How to prevent error with quotation marks in JavaScript?

I´m sending my data through a JavaScript like this:

function: enviaEditar('.$row->id.',\' '.$row->nombre.' \')'

As you can see I'm using /' in order to seen a string (text) but I have an error if the value of $row->nombre contain ' or ". Is there a way to prevent both of them.

while($row = $result->fetch_object()) {
    echo '
    <tr style="font-size:12px">
        <td>'.$row->nombre.'</td>
        <td style="text-align: center;">
            <button type="button" data-toggle="modal" data-target=".ModalEditar" onclick="enviaEditar('.$row->id.',\''.$row->nombre.'\');">
            </button>
        </td>
    </tr>';
}

You need to url encode the PHP code. Change the line to this:

<button type="button" data-toggle="modal" data-target=".ModalEditar" onclick="enviaEditar('.urlencode( $row->id ) .',\''. urlencode( $row->nombre ) .'\');">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM