<?php
if (isset($_POST['submitted'])) { // Handle the form.
// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);
$errors = array(); // Initialize an error array.
// Check for a first name:
if (preg_match('/^[A-Z \'.-]{2,20}$/i', $trimmed['full_name'])) {
$fn = mysqli_real_escape_string($dbc, $trimmed['full_name']);
} else {
$errors[] = 'You forgot to enter your name.';
}
// Check for an email address:
if (preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
$e = mysqli_real_escape_string($dbc, $trimmed['email']);
} else {
$errors[] = 'You forgot to enter a valid email address.';
}
if (empty($errors)) {
// Make sure the email address is available:
$q = "SELECT user_id FROM jmc_cal_users WHERE email='$e'";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_num_rows($r) == 0) { // Available.
// Create the activation code:
$a = md5(uniqid(rand(), true));
// Add the user to the database:
$q = "INSERT INTO jmc_cal_users (email, full_name, active, registration_date) VALUES ('$e', '$fn', '$a', NOW() )";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
// Send the email:
$body = "Thank you for registering your email please click on this link:\n\n";
$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
mail($trimmed['email'], 'Email Confirmation', $body, 'From: admin@sitename.com');
?>
<script>
$(function () {
$("#calc_submit").click(function () {
$(".submit-cont").hide("slow");
$(".calculator-statement").show("slow");
});
});
</script>
<?php
} else { // If it did not run OK.
echo '<p class="error">Your email could not be registered due to a system error. We apologize for any inconvenience.</p>';
}
} else { // The email address is not available.
echo '<p class="error">That email address has already been registered.</p>';
}
} else { // Report the errors.
echo '<h1>Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
} // End of if (empty($errors)) IF.
mysqli_close($dbc);
} // End of the main Submit conditional.
?>
<html>
<form action="?" method="post">
<div class="calculator-submit">
<p>
<input type="text" name="full_name" size="25" maxlength="120" value="<?php if (isset($trimmed['full_name'])) echo $trimmed['full_name']; ?>" placeholder="Name"/>
<br />
<input type="email" name="email" size="25" maxlength="65" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" placeholder="Email"/>
</p>
<p>
</p>
</div>
<div class="submit-cont">
<input type="submit" name="submit" id="calc_submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />
</div>
</form>
<div>
<!-- END Calculator Submit Form -->
<div class="calculator-statement" style="display:none;">
<h2>Check your email</h2><br />
</div>
</html>
<?php
if (isset($_POST['submitted'])) { // Handle the form.
// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);
$errors = array(); // Initialize an error array.
// Check for a first name:
if (preg_match('/^[A-Z \'.-]{2,20}$/i', $trimmed['full_name'])) {
$fn = mysqli_real_escape_string($dbc, $trimmed['full_name']);
} else {
$errors[] = 'You forgot to enter your name.';
}
// Check for an email address:
if (preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
$e = mysqli_real_escape_string($dbc, $trimmed['email']);
} else {
$errors[] = 'You forgot to enter a valid email address.';
}
if (empty($errors)) {
// Make sure the email address is available:
$q = "SELECT user_id FROM jmc_cal_users WHERE email='$e'";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_num_rows($r) == 0) { // Available.
// Create the activation code:
$a = md5(uniqid(rand(), true));
// Add the user to the database:
$q = "INSERT INTO jmc_cal_users (email, full_name, active, registration_date) VALUES ('$e', '$fn', '$a', NOW() )";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
// Send the email:
$body = "Thank you for registering your email please click on this link:\n\n";
$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
mail($trimmed['email'], 'Email Confirmation', $body, 'From: admin@sitename.com')
} else { // If it did not run OK.
$errors[] = 'Your email could not be registered due to a system error. We apologize for any inconvenience.';
}
} else { // The email address is not available.
$errors[]= 'That email address has already been registered.';
}
} else { // Report the errors.
echo json_encode($errors);
} // End of if (empty($errors)) IF.
mysqli_close($dbc);
} // End of the main Submit conditional.
?>
<html>
<body>
<form action="?" method="post" id="calc" on>
<div class="calculator-submit">
<p>
<input type="text" name="full_name" size="25" maxlength="120" value="<?php if (isset($trimmed['full_name'])) echo $trimmed['full_name']; ?>" placeholder="Name"/>
<br />
<input type="email" name="email" size="25" maxlength="65" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" placeholder="Email"/>
</p>
<p>
</p>
</div>
<div class="submit-cont">
<input type="submit" name="submit" id="calc_submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />
</div>
</form>
<div>
<!-- END Calculator Submit Form -->
<div class="calculator-statement" style="display:none;">
<h2>Check your email</h2><br />
</div>
<script>
$(function () {
$("#calc").on('submit', function(e) {
e.preventDefault();
});
$("#calc_submit").click(function () {
$.ajax({
url: '',
data: $("#calc").serialize(),
method: 'post',
success: function(d) {
// d will hold the json representation of $errors
if (d.length) {
// there are errors
console.dir(d);// output to he console to see what they are
}
else {
// success!
$(".submit-cont").hide("slow");
$(".calculator-statement").show("slow");
}
}
});
});
});
</script>
</body>
</html>
$("#calc_submit").click(function () {
$.ajax({
url: 'http://jaymillerconsulting.com/ltv_email_ajax.php',
data: $("#calc").serialize(),
method: 'post',
success: function (d) {
// d will hold the json representation of $errors
if (d.length) {
// there are errors
$( "div.errors").html(function() {
var errormessage = d;
return errormessage;
});
} else {
// success!
$(".submit-cont").hide("slow");
$(".calculator-statement").show("slow");
}
}
success: function(d) {
// d will hold the json representation of $errors
if (d.length) {
// there are errors
for (err in d) {
$("#errors").append("<p class='error'>"+err+"</p>");
}
}
else {
// success!
$(".submit-cont").hide("slow");
$(".calculator-statement").show("slow");
}
}
$("#calc_submit").click(function () {
$.ajax({
url: 'http://jaymillerconsulting.com/ltv_email_ajax.php',
data: $("#calc").serialize(),
method: 'post',
dataType: 'JSON', /ADD THIS
success: function (d) {
// d will hold the json representation of $errors
if (d.length) {
// there are errors
$.each(d, function(index, item) {
// DO SOMETHING WITH ERROR MESSAGE HERE
});
} else {
// success!
$(".submit-cont").hide("slow");
$(".calculator-statement").show("slow");
}
}
});
});
Also take a look at the docs for JQuery .ajax . The success call back is being deprecated in favour of then
Maybe the best design would be one that has both divs in the HTML. The jQuery script could have a success function that changes the visibility. So rather than sending some JavaScript after the PHP action script has taken control of the process, you could send just a signal from the PHP action script, and the jQuery success function could change the visibility of the divs based on the information gotten from the server-side script. Just a thought.