pimpace pimpace - 6 months ago 46
jQuery Question

I want to use a modal to send mail, via a php file and I want the pop closed once submit is closed

I have a simple form that loads nicely via bootstrap modal once the page loads. I want to process and send the once I click submit and the modal should close immediately without refreshing the page. Below are my codes. It is not working. Please I need help. With a php file it sends the mail but it takes me to the php file and gets stuck there. I believe it's a small thing but I can't wrap my head around it.



$(function() {
$(window).load(function() {
$('#Modal').modal('show');

});

$('#btn_send').click(function(){
var name = $(":input[name='name']").val();
var email = $(":input[name='email']").val();

var varData = 'name=' + name + '&email=' +email;

$.ajax({
type: 'POST',
url: 'index.php',
data: varData,
success: function(){
alert('Mail sent. Thank you.');
}
});
});
});

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" id="form_pp">
<div id="Modal" class="modal fade in" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
</button>
<div class="modal-header" style="margin-top:120px;">
<div class="modal-body">

<div class="form-group">
<input class="form-control" placeholder="YOUR FULL NAME" name="name" type="text" required autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="YOUR EMAIL ADDRESS" name="email" type="email" required>
</div>

<br>
<button type="submit" class="btn btn-default" style="color:#c13b01" id="btn_send">SEND</button>
<button type="button" class="btn btn-default" style="color:#c13b01" name="send" id="btn_cancel" data-dismiss="modal">CANCEL</button>
</div>
</div>
</div>
</div>
</div>





php file
if (isset($_POST['name']) && isset($_POST('email))) {
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = "Results from Online contact form:\n\n";

$email_from = $email;
$email_to = 'mail@mail.com';

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email. "\n\n" . 'Subject: ' . $subject;

@mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

}

Answer

with the help of @Eric Dohmen, I figured I missed out quotes and the correct block parenthesis for the email variable. Here it is:

if (isset($_POST['name']) && isset($_POST['email'])) {
 $name = @trim(stripslashes($_POST['name'])); 
     $email = @trim(stripslashes($_POST['email'])); 
     $subject = "Results from Online contact form:\n\n";

    $email_from = $email;
    $email_to = 'mail@mail.com';

    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email. "\n\n" . 'Subject: ' . $subject;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

}
else{
    echo alert('Message did not send');
}