简体   繁体   中英

Form in Internet Explorer always posting

I wonder why Internet Explorer (any version) always gives POST without calling the right function onSubmit . Works only if i refresh the page

Forms' content is generated dynamically by PHP/MySQL here:

<form id="formQtd" name="formQtd" onSubmit="return validaQtd();" method="post">
        <p class="cab">Por favor informe a quantidade de Ovos de P&aacute;scoa em estoque em sua loja neste momento:</p>
        <div id="produtos">
            <?
                if ($linhas2 == 0)
                    echo "<p style='margin-left: 15px'>A pesquisa foi cancelada pelo administrador</p>";
                else {
                    if ($linhas == 0)
                        echo "<p style='margin-left: 15px'>Nenhum ovo de p&aacute;scoa cadastrado</p>";
                    else {
                        while ($row = mysql_fetch_array($result)) {
                            $texto = "<div id='thumb'>
                            ";
                                $texto .= "<img width='100' height='100' src='img_pan/".$row["imagem"]."' />
                                ";
                                $texto .= "<p id='pNome'>".utf8_encode($row["nome"])."</p>
                                ";
                                $texto .= " <input type='text' value='' class='inputQtd num' name='".$row["id"]."_prod' id='qtd$row[id]' placeholder='Quant.' /> 
                                ";
                                $texto .= "</div>";
                                echo $texto;
                        }?>
        </div>
        <input type="submit" name="submit" id="submitQtd" value="Enviar" style="width:70px;height:25px;clear:both;float:right;border:1px solid #873305; background-color:#300300; color:white; margin:5px 0px 20px 0px" />
    </form>  
    <?
                            }
                }
    ?>

My function validaQtd:

function validaQtd() { 
  var inp = document.forms[0].getElementsByTagName('input');
  console.log(inp.length);
  for (var i = 0; i < inp.length; i++){
    if(inp[i].type == "text"){
        if(inp[i].value == ""){
            console.log(inp[i].value+" - "+inp[i].id);
            alert('Preencha as quantidades em branco');
            inp[i].focus();
            return false;
        }
    }
  }
}

My POST :

if ($_POST) {

    $time = microtime(true);
    $micro_time = sprintf("%06d",($time - floor($time)) * 1000000);
    $date = new DateTime( date('Y-m-d H:i:s.'.$micro_time,$time) );
    $dt = $date->format("Y-m-d H:i:s.u");
    $dtAtual = substr($dt, 0, -3);

    $upload = 0;                
    foreach ( $_POST as $nome => $valor ) {
        if($nome != "submit" && $valor != "") {
            $upload = 1;
            //separando o codigo do nome
            $prov = explode("_", $nome);
            $cdProd = str_replace("_","",$prov[0]); 

            //inserindo no banco    
            $queryIns = "INSERT INTO registro_ovos2013 (idusuario, idproduto, quantidade, data) VALUES ('".$cdUsu."', ".$cdProd.", ".$valor.", '".$dtAtual."')";
            $resultIns = mysql_query($queryIns);
        }   
    }
    if ($upload == 1) {
        ?>
        <script>
            alert("Quantidade cadastrada!");                        
            window.location = "?codigo=<?=$cdUsu;?>";
        </script>
        <?
    } 
    else {
        ?>
        <script>
            alert("Preencha os campos em branco");                      
            window.location = "?codigo=<?=$cdUsu;?>";
        </script>
        <?
    }
}

Do you mean that your steps are like this:

  • Load a page.
  • Enter valid data.
  • Submit (function is called)
  • Reload (POST processed, validating function not called)

If yes, that is quite normal. Page is loaded with some POST request and reloading it causes requesting again. And no javascript is involved.

That is why you always need to validate data on the server-side too. Right after if ($_POST) {

If your page is parsed as XML (as XHTML), you should use onsubmit as the attribute's name (instead of onSubmit ). XML is case sensitive.

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