How do I show a php error in jQuery?

Asked

Viewed 62 times

0

I have one (.html), one (.php) and my jQuery.

In my jQuery( I am sending the information to (.php) do CRUD and etc.

Transactions are occurring smoothly, but are not displaying error messages of the type ("id_person field cannot be empty).

How do I do that?

$('#cadastrar').on('click', function(){
	var opcaoCadastrar = $(this).attr("value");
	var nome = $('#nome').val();
	$.post('consulta.php',
	{opcaoCadastrarEnvia:opcaoCadastrar,nomeEnviarCadastrar:nome},function(data){
		
	});
	
});
if (isset($_POST['opcaoCadastrarEnvia'])){
				
		$nome = $_POST['nomeEnviarCadastrar'];
		$consulta = "INSERT INTO FUNCIONARIO (FUN_NOME)VALUES(:nome)";
		$resultado->$db_con->prepare($consulta);
		$resultado->BindParam(':nome',$nome,PDO::PARAM_STR);
		$resultado->execute();
	}

1 answer

0

I haven’t worked with jquery for a long time, but I think that’s how it is:

 $('#cadastrar').on('click', function(){
        var opcaoCadastrar = $(this).attr("value");
        var nome = $('#nome').val();
        $.post('consulta.php',
        {opcaoCadastrarEnvia:opcaoCadastrar,nomeEnviarCadastrar:nome},function(data){

        }).fail(function(response) {
          alert('Error: ' + response.responseText);
     });

    });

For older versions (pre-1.8) Use .error in place of .fail

  • Didn’t work, still no error messages =/

  • Let’s go in parts. Inside the post block, add console.log(date). What displays on the console?

  • $('#register'). on('click', Function(){ var optionCadastrar = $(this). attr("value"); var name = $('#name'). val(); $. post('query.php', {optionCast:optionCast,nameCast:name},Function(data){ console.log(data); }); });

  • Keep coming back nothing.

  • This means that not even the request is being made, you need to a) see the errors that are in the console and b) access your php script directly and see the error that appears

  • My script was on my index(working), but as all my other transactions were on mine (.jQuery), I decided to put it there too, it’s the only one that isn’t working. I’m sure they are by the fields not null in my database, however, I wanted these errors to be returned the same as they were in my index.

  • If I put a Try/catch in my php, it would solve the problem?

  • We can try :)

Show 3 more comments

Browser other questions tagged

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