Ajax code is not getting the value of inputs

Asked

Viewed 106 times

0

I have a problem with ajax, which is not taking the data entered in the email form, despite sending the emails.

Follow the form code:

      $(function(){
$('#nome, #email, #assunto, #message').on('keypress', function(e){
    if (e.keyCode == 13) {
        e.preventDefault();
        $('#envia').click();
    }
});

$("#envia").click(function(){
	var name = $("#nome").val(),
    	emai = $("#email").val(),
    	ass  = $("#assunto").val(),
   		mess = $("#message").val();
   	
    var campo = {
        nome: $("#nome").val(),
        email: $("#email").val(),
        assunto: $("#assunto").val(),
        message: $("#message").val()
    };

    if(!campo.nome || !campo.assunto || !campo.email || !campo.message){
        $.alert({
        title: 'Atenção',
        content: 'Todos os campos sao obrigatorios!',
        animation: 'bottom',
        icon: 'fa fa-warning',
        animationSpeed: 700,
        keyboardEnabled: true,
        columnClass: 'col-md-4 col-md-offset-4'
        });
        return;
    }
    
	$.ajax("email.php",{
		type: "POST",
		data:{'nome': name, 'email': emai, 'assunto': ass, 'mensagem': mess} }).done(function(){
			$.alert({
	            title: 'Sucesso',
	            content: 'E-mail enviado com sucesso!',
	            animation: 'bottom',
	            icon: 'fa fa-warning',
	            animationSpeed: 700,
	            keyboardEnabled: true,
	            columnClass: 'col-md-4 col-md-offset-4'
	            });
	            return;
		}).fail(function(){
			$.alert({
	            title: 'Opss..',
	            content: 'Ocorreu um erro durante o processo tente novamente!',
	            animation: 'bottom',
	            icon: 'fa fa-warning',
	            animationSpeed: 700,
	            keyboardEnabled: true,
	            columnClass: 'col-md-4 col-md-offset-4'
	            });
	            return;
		});
	});
});
<div class="form-group col-md-6">
  <input type="text" name="name" id="nome" class="form-control" required placeholder="Nome">
</div>
<div class="form-group col-md-6">
  <input type="email" name="email" id="email" required class="form-control" placeholder="E-mail">
</div>
<div class="form-group col-md-12">
  <input type="text" name="subject" id="assunto" required class="form-control" placeholder="Assunto">
</div>
<div class="form-group col-md-12">
  <textarea name="message" id="message" id="message" required class="form-control" rows="8" placeholder="Sua Mensagem. "></textarea>
</div>
<div class="form-group col-md-12">
  <button name="submit" id="envia" class="btn btn-primary pull-right">Enviar</button>
</div>

  • could give a little more context, which data are not being taken, post your form HTML. I think this will get a response :)

  • not being taken all the data I’m asking you to take

  • added the html part

  • Use the.log console and see how far your code is picking up data

  • gave a console.log(data) and it gave me this data is not defined error

  • try to remove single quotes from their variables inside the data

  • changed nothing continues in the same error

  • Changed the date to data: {&#xA; 'a' : name,&#xA; 'b' : emai,&#xA; 'c' : ass,&#xA; 'd' :mess&#xA; } ?

  • I see by the breakpoint ta strange he’s getting the values of the variables and ta picking

  • the problem may be in your email.php file too.

  • but it was right until I decided to do with ajax

  • Even changing your date the way I posted keeps giving error? If yes, inform which.

  • yes keeps giving the same error.

  • I made a test to see if it is php and not and I did not play the form there and filled the fields he send me the email straight

  • Where are you giving error then? If you are sending the email, it means that the data is being sent by ajax normally.

  • not I did the test by php only php to see if my php code was wrong and it is not wrong. the error is in ajax he is sending the email but not picking up the variables is not sending the fields filled the email he is sending intendeu?

  • ai ta dar esse erro de data is not defined

  • could help me I remade ajax and I managed to get the email now it is sending email only with the field filled and the other empty fields I will edit my code ajax must be some small mistake I’m missing if anyone can help

Show 13 more comments

1 answer

1


Change the json you’re manually populating in data inside the ajax and remove the additional comma:

Your code is like this:

 $.ajax({
             url: 'email.php',
             type: 'POST',
             data: {
               a: 'name',
               b: 'emai',
               c: 'ass',
               d: 'mess'
             },
           })

Switch to like this:

 $.ajax({
             url: 'email.php',
             type: 'POST',
             data: {
               a: name,
               b: emai,
               c: ass,
               d: mess
             }
           })

And to get the data on email.php use json indices as global variable input $_POST. Example:

$name = $_POST['a'];
$email = $_POST['b'];
$assunto = $_POST['c'];
$messagem = $_POST['d'];
  • and yes thanks guy but already I decided to mark as solved I realized now that I had to pass the name I gave us input and they had to match the encoding in php vlw ai man

  • @Leonardocosta Dispo :D. If you notice, I already answered this in the comments, look here http://answall.com/questions/97273/c%C3%B3digo-ajax-n%C3%A3o-est%C3%A1-catching-the-value-of-inputs/97291? noredirect=1#comment197288_97273

  • and I realized now but there where that a,b,c,d I was thinking I could come in with anything and not with the php post

Browser other questions tagged

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