Doubt , how to resolve this conflict?

Asked

Viewed 171 times

-1

I have the following scripts , one for validation and one for sending to php :

$(document).ready(function(){
    $("input,textarea").blur(function(){
     if($(this).val() == "")
         {
             $(this).css({"border" : "1px solid #F00", "padding": "2px"});
         }
    });
    $("#butEnviar").click(function(){
     var cont = 0;
     $("#form input,textarea").each(function(){
         if($(this).val() == "")
             {
                 $(this).css({"border" : "1px solid #F00", "padding": "2px"});
                 cont++;
             }
        });
     if(cont == 0)
         {
             $("#form").submit();
         }
    });
});

This is for validation

$(document).ready(function() {
$("#butEnviar").click(function() {
    var nome  = $("input[name=nome]").val();
    var email =  $("input[name=email]").val();
    var telefone =  $("input[name=telefone]").val();
    var assunto = $("input[name=assunto]").val();
    var msg =  $("textarea[name=msg]").val();
    $.ajax({
        "url": "enviar.php",
        "dataType": "html",
        "data": {
            "nome" : nome,
            "email":email ,
            "telefone": telefone,
            "assunto": assunto,
            "msg" : msg
        },
        "success": function(response) {
            $("div#saida").html(response);
            $("#reset").click();

        }

And this one for shipping. But I think it’s giving conflict and is not sending the data to enviar.php, what should I do?

  • I guess you got -3 because it is not 100% clear in the question what you need. I put an answer, I hope it helps you.

2 answers

0

Your flow is wrong because you are submitting via HTML and not via Javascript/Ajax. I can imagine by your Ajax code that you want to send via Javascript and not do form Ubmit via HTML (which loads the page again).

To submit via Ajax you must capture the event submit form and stop it. Only with it canceled can you send the data to Ajax. For this changes

in the assessment code

$("#butEnviar").click(function(){

adds

$("#butEnviar").click(function(){
    e.preventDefault(); // para parar o submit

And in the code where you have Ajax changes

$("#butEnviar").click(function() {

for the event you will listen to to intercept the event you fire with $("#form").submit();. Thus changes

$("#form").on('submit', function(e) {
    e.preventDefault();

You can also make some improvements to your code, with less jQuery and no repetitions like this:

Appraisal:

$(document).ready(function() {
    function uiWarning() {
        return this.classList.toggle('incomplete', this.value == '');
    }
    $("input,textarea").blur(uiWarning);
    $("#butEnviar").click(function(e) {
        e.preventDefault();
        var cont = 0;
        var falhados = $("#form input,textarea").filter(uiWarning);
        if (falhados.length == 0) $("#form").submit();
    });
});

in the part of Ubmit:

$(document).ready(function() {
    $("#form").on('submit', function(e) {
        e.preventDefault();
        var data = {};
        ['nome', 'email', 'telefone', 'assunto', 'msg'].forEach(function(name) {
            data[name] = document.querySelector('[name="' + name + '"]').value;
        });

        $.ajax({
            "url": "enviar.php",
            "dataType": "html",
            "data": data,
            "success": function(response) {
                $("div#saida").html(response);
                $("#reset").click();
            }
        });
    });
});
  • I’ll do the following, I’ll leave the link of the page I’m doing, and then you give a look, I couldn’t put the form here kkk , In the example I showed at first, I was sending an email via php , and then I was right, but I put that validation and bugged , I know there, I only know that you are not doing anything , maybe it is little thing or not . newairdesign.com.br/new/contact1.html

  • @Renanmoreira you have a / the most on the page that’s been missing. And you forget to join e.preventDefault(); before var data = {};. By the way, I suggested it in the reply but then I didn’t put it in the final code... I fixed it now.

  • I modified how you said ,but it seems that he is not sending the email, the php page , is there on the server , all right, because I had used it on another site, which followed the same logic, but I did not use it . Because in the code, it sent a copy of the form to the user ,and a copy to me , in this case the domain email, but as a test had put one of gmail. and it’s not going. What can it be? The detail, I made another contact page, but using Formmail, however, it gets a little strange , open a different page from my site.

  • @Does the part that respects this question already work? That is, send the data to PHP?

  • So, I believe I’m not sending to php, because php would have to send an email to the emails, as I said above, but I don’t think I’m even running php. As I said, I had used it on another site, but I didn’t get to complete the site entirely, because the layout didn’t please me. And even when it was blank, he would send a message to emails

  • And on this site I hadn’t done validation or anything like that .

  • Check the Browser console to see if it is uploading to PHP. Or put echo var_dump($_REQUEST); in PHP to confirm.

  • If you’re talking about the F12 console, it didn’t say anything about sending to php . type so it looks like you just "upgraded"

  • @Renanmoreira on your page still has the wrong code. It should not be $("#butEnviar").click(function(e) { but yes $("#form").on('submit', function(e) { as I put it in the reply. It also brings together a CSS class .incomplete{ border: 1px solid #F00; padding: 2px;} to do the effector I had in jQuery.

  • @Renanmoreira thus: https://jsfiddle.net/zvakedvh/

  • I will test, and I was also taking a look at php, and I saw that it was like _REQUEST , should be _POST , right ? at least in this case, it should be right ?

  • @Renanmoreira the jQuery makes GET if you have nothing to say otherwise.

  • So I managed to get the data, however ,is not sending the email ,he is taking the data and such , but is not posting, and also the . incomplete is not going, do not understand why '-'

Show 8 more comments

0

Tip: Use the required html property for validations. It will greatly decrease this code, increasing readability.

<input type="text" required="required" name="name" />

It is possible to style inputs using pseudo classes:

input:required:invalid {}
input:required:valid {}

Source: See Here

Browser other questions tagged

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