PHP modal after submit not working

750 views Asked by At

Question about @ss contact form. After submitting the form, bootstrap popup should appear with a confirmation, but apparently it's not showing up.

Here's my code :

<?php 

if(isset($_POST['submit'])) {     
// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "info@***.com";

// EDIT THE 2 LINES BELOW AS REQUIRED
$sender = $_POST['email'];

$name = $_POST['name']; // required
$email = $_POST['email']; // required
$object = $_POST['object'];
$message = $_POST['message'];


$email_message = "Form details below.\n\n";

$email_subject = "Contact details - $name";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "object: ".clean_string($object)."\n";
$email_message .= "Message: ".clean_string($message)."\n";


// create email headers
$headers = 'From:'.$sender."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
$sent= @mail($email_to, $email_subject, $email_message, $headers); 

if($sent){
//         echo "<script>alert('Thank you for submitting your details, We will be in touch as per your request.')
//         location.replace('contact.html')
// </script>";
    echo "<script>$('#thankyouModal').modal('show')</script>";
}
else {
echo "<script>alert('Sorry! Something went wrong')
        location.replace('contact.html')
</script>";

  }
}
?>

<html>
    <head>  
    <title></title>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </head>
    <body>

<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h2 class="modal-title text-center" style="font-weight: 600;">Thank you for Contact Us!</h2>
      </div>
      <div class="modal-body text-center" style="background-color: #59ABE3;">
        <p style="font-weight: 600; color: #fff; font-size: 18px;">Thanks for getting in touch!</p>
      </div>
      <div class="modal-footer" style="background-color: #59ABE3; border-top: 0px solid #fff;">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

    </body>
</html>

Form is sending me to cont.php through the submit button and after that I get blank cont.php page.

I hope that someone has the answer. Thanks in advance guys!

1

There are 1 answers

11
harisdev On BEST ANSWER

Try adding (document).ready function to your JS snippet.

echo "<script>
$(document).ready(function(){
$('#thankyouModal').modal('show');
});
</script>";

and move this whole PHP script before </head> tag since you don't want to call functions before jQuery is initialized.

Then you should have your modal loaded:

Modal