Reputation: 67
I found and adapted a php script that processes the content of a contact form and sends and/or display alerts that would appear on a Bootstrap 3 modal window. I manage to make work my script and display my errors, as well as to display my modal onload with non php in it, but as soon as I try to include my php code into the modal to display the errors within the modal, nothing displays.
I tried many ways : PHP in HTML, HMTL in PHP… And no combination worked. This is where I arrived so far :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>My title</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="css/animate.css" rel="stylesheet" />
<!-- Squad theme CSS -->
<link href="css/style.css" rel="stylesheet">
<link href="color/default.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php
// Clean up the input values
foreach($_POST as $key => $value) {
if(ini_get('magic_quotes_gpc'))
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
if(!$name) {
$errors[] = "Vous devez indiquer votre nom.";
} else {
$errors[] = "Votre nom doit faire au moins deux caractères de long.";
}
}
if(!$email) {
$errors[] = "Vous devez renseigner votre email.";
} else if(!validEmail($email)) {
$errors[] = "Merci de fournir une adresse email valide.";
}
if(strlen($message) < 10) {
if(!$message) {
$errors[] = "Vous devez saisir votre message.";
} else {
$errors[] = "Merci de laisser un message d'au moins dix caractères de long.";
}
}
if($errors) {
// Output errors and die with a failure message
$errortext = "";
foreach($errors as $error) {
$errortext .= "<li>".$error."</li>";
}
die("
<div class=\"modal fade\" id=\"fail-prompt\">
<div class=\"modal-dialog\">
<div class=\"modal-content\">
<div class=\"modal-header\">
<button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"true\">×</span><span class=\"sr-only\">Close</span></button>
<h4 class=\"modal-title\">Votre message n'a pas pu être envoyé.</h4>
</div>
<div class=\"modal-body\">
<p>Votre message n'a pas pu être envoyé</p>
<ul>". $errortext ."</ul>
</div>
<div class=\"modal-footer\">
<button type=\"button\" class=\"btn btn-primary\">Retour vers le formulaire</button>
</div>
</div>
</div>
</div>");
// Send the email
$to = "myemail";
$subject = "Contact Form: $name";
$message = "$message";
$headers = "From: $email";
mail($to, $subject, $message, $headers);
// Die with a success message
die("<div class=\"modal fade\" id=\"fail-prompt\">
<div class=\"modal-dialog\">
<div class=\"modal-content\">
<div class=\"modal-header\">
<button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"true\">×</span><span class=\"sr-only\">Close</span></button>
<h4 class=\"modal-title\">Message envoyé avec succès !</h4>
</div>
<div class=\"modal-body\">
<p>Votre message a été envoyé avec succès !</p>
</div>
<div class=\"modal-footer\">
<button type=\"button\" class=\"btn btn-primary\">Retour au site</button>
</div>
</div>
</div>
</div>");
// A function that checks to see if
// an email is valid
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
?>
</body>
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>-->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- Custom Script-->
<script type="text/javascript">
$(window).load(function(){
$('#fail-prompt').modal('show');
backdrop: 'static';
keyboard: false;
});
</script>
</html>
Upvotes: 0
Views: 6194
Reputation: 1025
You can create the modal calling the php code with jquery:
This is the modal:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h2 style="color:#f68a26;font-weight:500;" align="left">Eventos y Noticias</h2>
</div>
<div class="modal-body"><div id="content-php"></div></div>
<div class="modal-footer">
<a class="btn btn-lg btn-overlay" href="#" data-dismiss="modal">Cerrar</a>
</div>
</div>
</div>
</div>
Jquery script:
$("#icon_events_header").click(function(){
$("#content-php").load("./admin/content/eventsContent.php");
});
In the file you can manage all the action of the modal.
Hope works for you,
Upvotes: 1
Reputation: 136
PHP don't create a modal, because for lack includes of styles, js... Recommended "create a modal" using ajax.
Upvotes: 0