Reputation: 1
this is my contact form.
I need to display message in sweetalert after success/fail
submission of form. The error/success message is in php alert.
<form class="contact-form" action="" method="post" id="form_id">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Full Name" required>
</div>
<div class="form-group">
<input type="number" name="number" class="form-control" placeholder="Phone No." required>
</div>
<div class="form-group">
<input type="text" name="country" class="form-control" placeholder="Country" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Your Email Id" required>
</div>
<div class="form-group">
<input type="date" name="date" class="form-control" placeholder="Travel Date" required>
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Your Message" style="height:140px"></textarea>
</div>
<button type="submit" class="btn btn-primary" name="submit" value="send">SEND MESSAGE</button>
</form>
This is my php
code
use PHPMailer\PHPMailer\PHPMailer;
require_once 'phpmailer/Exception.php';
require_once 'phpmailer/PHPMailer.php';
require_once 'phpmailer/SMTP.php';
$mail = new PHPMailer(true);
$alert = '';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$country = $_POST['country'];
$message = $_POST['message'];
$package=$_POST['package'];
$date=$_POST['date'];
try{
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = ''; // Gmail address which you want to use as SMTP server
$mail->Password = ''; // Gmail address Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = '587';
$mail->setFrom(''); // Gmail address which you used as SMTP server
$mail->addAddress(''); // Email address where you want to receive emails (you can use any of your gmail address including the gmail address which you used as SMTP server)
$mail->isHTML(true);
$mail->Subject = 'Message Received';
$mail->Body = "<h3>Name : $name <br><br>Phone No: $number <br><br> Country: $country <br><br> Email: $email <br><br>Arrival Date: $date <br><br>Package: $package <br><br> Message: $message</h3>";
$mail->send();
$alert = 'alert-success';
} catch (Exception $e){
$alert = 'alert-error';
}
}
this is my javascript
code
alert-success
checksuccess:function(alert) {
if(alert == 'alert-success'){
swal("Good job!", "You clicked the button!", "success");
}
else swal("Something is missing!!!!!", "Try again","error");
}
Upvotes: 0
Views: 2962
Reputation: 1671
if not using ajax you can do this :
<script>
var alert = '<?php echo $alert;?>';
if(alert == 'alert-success'){
alert('yes');//for test
swal("Good job!", "You clicked the button!", "success");
}
else if(alert=='alert-error'){
alert('no');//for test
swal("Something is missing!!!!!", "Try again","error");
}
</script>
Upvotes: 1
Reputation: 66
you should have to be use ajax for this sweet alert operation.
$.ajax({
.... other settings you already have
error: function (xhr, ajaxOptions, thrownError) {
swal("success!", "Form has been submitted", "success");
}
});
Upvotes: 0