Ajax does not work in firefox and chrome works

2

I'm giving an insert in the database, in chrome it works, in firefox my request does not succeed. Has anyone gone through this?

I changed the serialize () function and passed the values in an invidual way, to see if that was it. I tested the passage of each value, all arrive on the other page the insert swirls. Only in firefox that does not do that.

$("#btnentrada").click(function(){
	var txtplaca = $("#txtplaca").val();
	var cmbtipo = $('#cmbtipo').val();
	var txtmarca = $("#txtmarca").val();
	var txtmodelo = $("#txtmodelo").val();
	var txtcor = $("#txtcor").val();
	var cmbcobranca = $("#cmbcobranca").val();
	var txtobs = $("#txtobs").val();	
	$.ajax({
		url: 'insere_entrada.php',
		method: 'post',
		data: {'txtplaca': txtplaca , 'cmbtipo': cmbtipo , 'txtmarca': txtmarca , 'txtmodelo' : txtmodelo , 'txtcor' : txtcor , 
			'cmbcobranca' : cmbcobranca , 'txtobs' : txtobs},
		success: function(data){
			alert(data);
		}
	})
});

php

//verifica se as sessions estão preenchidas
include_once("status_logado.php");
require_once('db.class.php');
$placa = $_POST['txtplaca'];
//nao permite carcateres especiais
if ( !empty( $placa) && preg_match( '/^[\w\n\s]+$/i' , $placa ) ){
    $tipo = $_POST['cmbtipo'];
    $marca = $_POST['txtmarca'];
    $modelo = $_POST['txtmodelo'];
    $cor = $_POST['txtcor'];
    $cobranca = $_POST['cmbcobranca'];
    $obs  = $_POST['txtobs'];       
    $objDb = new db();
    $link = $objDb->conecta_mysql();
    $sql = "INSERT INTO TBL_COBRANCA (COB_PLACA,COB_MARCA,COB_MODELO,COB_COR,COB_OBS,ID_TIPO) VALUES('$placa','$marca','$modelo','$cor','$obs',$tipo)";  
    if(mysqli_query($link,$sql)) {
        echo "Dados inseridos com sucesso!";
    }else {
        echo("Erro na operação, contatar o administrador do sistema.");

    }

} else {
    echo "Só são permitidos letras e números";
    die();
}
    
asked by anonymous 28.07.2018 / 01:05

1 answer

3

You are probably using a submit button to call Ajax and have not canceled the event. Chrome provides the event object in global scope, but Firefox does not.

By clicking the button in Chrome, Ajax will be rendered (displaying alert ) and then the page will be reloaded by submitting the form. In Firefox this will not happen and the form will be immediately submitted without processing Ajax in the function of the event.

Add the parameter event to the event handler and cancel the event with event.preventDefault(); :

$("#btnentrada").click(function(event){
    event.preventDefault();
    ...
});

Another difference is that if you do not supply the (event) parameter in the event handler, event will reference the native event object of Chrome, which is different from event object that jQuery passes to the handler .

Example:

$("#btnentrada").click(function(){ // sem o parâmetro event
    event.preventDefault();
    ...
});

It will work in Chrome because it will reference the native event object it has. Already in Firefox will return the error event is not defined because it does not have the native object.

So it's always good to use event.preventDefault(); with the (event) parameter in the handler, because even if you want to send the form via Ajax, it does not make sense to submit it by reloading the page.

    
28.07.2018 / 04:09