Reloading the page (send form)

Elder

New Member
#1
I have a problem with reloading the page. After clicking submit form the page reloads. I click send the form and takes me to http: // localhost: 3000 / mail.php. And I would like the site not to be reloaded. (I use the validation form jquery plugin).This is my code: jquery
Mã:
  (function(){
    $("#contactForm").on('submit', function(e) {
          e.preventDefault();
          var data = {
            name = $('#field-name').val(),
            phone = $('#field-phone').val(),
            email = $('#field-email').val(),
            message = $('#field-message').val()
        };
        $.ajax({
            type: "POST",
            url: "mail.php",
            data: data,
            success: function(){
                console.log("jej");
            }
        });
        return false;
      });
  });
and php code
Mã:
<?php
    $to = 'name@gmail.com';
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email= $_POST['email'];
    $text = $_POST['message'];
    $subject = 'Nowy e-mail od ' . $name . ' (' . $email . ')';
    $message =  $name  . $phone .  $email . $text;
    $headers = 'From: ' . $name . "\r\n" .
    if(mail($to, $subject, $message, $headers)) {
      print "<p class='success'>Mail Sent.</p>";
    } else {
        print "<p class='Error'>Problem in Sending Mail.</p>";
    }
?>
 

Admin

Administrator
Thành viên BQT
#2
change part of your code
Mã:
$("#contactForm").on('submit', function(e)
to this...
Mã:
$("#contactFormButton").on('click', function(e)
and change your submit button to this...
Mã:
<button id="contactFormButton" type="button" class="form-control">Submit</button>
and here your php code...
Mã:
if(mail($to, $subject, $message, $headers)) {
      echo "Mail Sent!";
    } else {
        echo "Error!!";
    }
and here you can catch result of your php code...
Mã:
        success: function(data){
            console.log(data);
        }
 
OP
OP
E

Elder

New Member
#3
It works. However, fields from the form do not send out. Empty emails arrive :<
 

Admin

Administrator
Thành viên BQT
#4
can you show me html code???
if this helps you please mark me bro )))
 

Admin

Administrator
Thành viên BQT
#6
how did you know that your form fileds didnt sendout??

any error messaages in your browser console??
 
OP
OP
E

Elder

New Member
#7
Because I get a blank email message. No, in cosole: <p class='success'>Mail Sent.</p>
 

Admin

Administrator
Thành viên BQT
#12
try to remove validation if this works you may call these validations before sending ajax request in js code
 
OP
OP
E

Elder

New Member
#13
The message is no longer a putsa, but the page reloads. Only what prints php appears on the screen.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top