I can not call the javascript function by a button

1

Good morning, I have a problem on my page.

I created a Form with a comment field. When clicking the button it "should" call the function but does not call.

function gravaobs(pag_pedido, obs_boleto, stats)
            {
                if (obs_boleto.length == 0)
                {
                    alert('Por favor, preencha a observação');
                    document.formobs.observacao.focus();
                    return;
                }
                else
                {
                    var param = {'pag_pedido': pag_pedido, 'obs_boleto': obs_boleto, 'status': stats};
                    OpenWindowWithPost('tela_de_tratamento.php', '', 'NewFile1', param);
                }
            }
<form id="formobs" name="formobs">
                                        <table style='width: 1220px;'>
                                            <tr>
                                                <td align='center' style='padding:5px 0 10px 0; font: 12pt Trebuchet MS; color: #00471A; margin:0 auto;'>&nbsp;Inserir Obs.:</td>
                                                <td align='center' style='padding:5px 0 10px 0; font: 12pt Trebuchet MS; color: #00471A; margin:0 auto;'><input style='width: 975px;' id='observacao' name='observacao' type='text' maxlength='250' tabindex='4' size='137'/></td>
                                                <td style='font-size:14px; text-align:left;'><br/></td>
                                                <td align='center'><p class='botao'><a href='#' title='Gerar' onclick="javascript:gravaobs(<?php print $pag_pedido; ?>, document.formobs.observacao.value, <?php print $status; ?>);"></a></p></td>
                                            </tr>
                                        </table>
                                    </form>

All variables are normal and even (putting php echo variables) they appear normal, but I do not know why it does not work

Can anyone help me?

    
asked by anonymous 05.06.2015 / 16:05

1 answer

1

I think it's never wise to try to paste strings by hand - in this example, you have to worry about HTML escaping but also with JavaScript escaping. I think it's safer to do this.

<a href="#" title="Gerar" data-pag-pedido="<?= htmlspecialchars($pag_pedido) ?>" data-status="<?= htmlspecialchars($status) ?>" onclick="javascript:gravaobs(this.dataset.pagPedido, document.formobs.observacao.value, this.dataset.status)">

that works in IE 11+, or use this.getAttribute('data-pag-pedido') instead of this.dataset.pagPedido (id% pro status ) if you need to support IE 8+.

(You may need to ; return false at the end of onclick , to prevent the screen scrolling to the beginning)

    
05.06.2015 / 16:14