I cannot call the javascript function by a button

Asked

Viewed 383 times

1

Good morning, I have a problem on my page.

I created a Form with a field for comment. 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 (by placing php echo variables) they appear normal, but I don’t know why it doesn’t work

Can someone help me?

  • Writes onClick all with small letter. So: onclick

  • I tried, and I got no results

  • This function is inside the <head> <script> tag and the Browser does not give any error, in the page link appears # after the page name but nothing happens, nor the message if the field is empty. The screen in the case is just the field to type and an image of a button, nothing more.

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 Javascript Escaping. I think it’s safer to do that

<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 on IE 11+, or use this.getAttribute('data-pag-pedido') in place of this.dataset.pagPedido (idem pro pro status) if you need to support IE 8+.

(maybe you need to put ; return false at the end of onclick, to prevent the screen from rolling to the beginning)

  • Straight and straight, it works man, OK!

Browser other questions tagged

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