The validation fails because the file containing the validation logic is never executed when the form is submitted.
The standard and most effective solution is to handle everything in one file. The form page should be responsible for:
Displaying the form.
Receiving the submitted data.
Validating the data.
If invalid, re-displaying the form with errors.
If valid, performing the final action (like sending an email).
You just need to move the email-sending logic from form.php into the else block of your validation file.
Here is the corrected and combined code. You can replace the entire contents of your first file with this. You will no longer need form.php at all.
<?php
// 1. SETUP
$user = ['name' => '', 'age' => ''];
$errors = ['name' => '', 'age' => ''];
$message = '';
$form_submitted_successfully = false; // A flag to know when to hide the form
// 2. PROCESS FORM IF SUBMITTED
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Validation filters
$validation_filters['name']['filter'] = FILTER_VALIDATE_REGEXP;
$validation_filters['name']['options']['regexp'] = '/^[A-z]{2,10}$/';
$validation_filters['age']['filter'] = FILTER_VALIDATE_INT;
$validation_filters['age']['options']['min_range'] = 16;
$validation_filters['age']['options']['max_range'] = 65;
$user_input = filter_input_array(INPUT_POST, $validation_filters);
// Create error messages
$errors['name'] = $user_input['name'] ? '' : 'Name must be 2-10 letters using A-z';
$errors['age'] = $user_input['age'] ? '' : 'You must be between 16 and 65';
// Sanitize the original POST data to redisplay it safely in the form
$user['name'] = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$user['age'] = filter_var($_POST['age'], FILTER_SANITIZE_NUMBER_INT);
// Check if there are any errors by joining all error messages
$invalid = implode($errors);
// 3. DECIDE WHAT TO DO NEXT
if ($invalid) {
// If there are errors, show an error message
$message = 'Please correct the following errors:';
} else {
// If data is valid, SEND THE EMAIL
$to = '[email protected]'; // Use a real email address
$subject = 'Contact Form Submission';
$msg = "Name: {$user['name']}\n" .
"Age: {$user['age']}\n";
$headers = 'From: [email protected]'; // It's good practice to set a From header
// The mail() function returns true on success, false on failure
if (mail($to, $subject, $msg, $headers)) {
$message = 'Thank you, your data has been sent!';
$form_submitted_successfully = true; // Set flag to true
} else {
$message = 'Sorry, there was an error sending your message. Please try again later.';
}
}
}
?>
<?php // include 'includes/header.php'; // Assuming you have this file ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Validation Form</title>
<style>
.error { color: red; font-size: 0.8em; display: block; }
body { font-family: sans-serif; }
input { margin-bottom: 10px; }
form { border: 1px solid #ccc; padding: 20px; max-width: 400px; }
.message { padding: 10px; background-color: #e0e0e0; margin-bottom: 15px; }
</style>
</head>
<body>
<h1>Contact Us</h1>
<?php if ($message): ?>
<p class="message"><?= $message ?></p>
<?php endif; ?>
<?php // Only show the form if it hasn't been submitted successfully
if (!$form_submitted_successfully): ?>
<form name="form" action="" method="POST">
Name: <input type="text" name="name" value="<?= htmlspecialchars($user['name']) ?>">
<span class="error"><?= $errors['name'] ?></span><br>
Age: <input type="text" name="age" value="<?= htmlspecialchars($user['age']) ?>">
<span class="error"><?= $errors['age'] ?></span><br>
<input type="submit" value="Submit">
</form>
<?php endif; ?>
</body>
</html>
AJAX (Asynchronous JavaScript and XML) lets you send data to a PHP script in the background, without the page reloading. This is what happens on sites like Twitter when you click "Like" or on Gmail when you archive an email.
Also, you need to create the PHP script that will reads the data and sends back a JSON response for the JavaScript to read.
You're assigning $addingTenMinutes but then using $addingFiveMinutes, which doesn't exist.
Also, strtotime('now() + 10 minute') is not a valid syntax for strtotime.
Here is a corrected version:
$addingTenMinutes = strtotime('+10 minutes');
$end_time = date('Y-m-d H:i:s', $addingTenMinutes);
There is a slight error in the second foreach loop where you check for extra SKUs. You are comparing the received count with the expected count, but you should be checking if the received count is greater than the expected count. Try replacing the second foreach statement with this:
foreach ($received_counts as $sku => $received_count) {
if (!isset($expected_counts[$sku])) {
$extra = array_merge($extra, array_fill(0, $received_count, $sku));
} elseif ($received_count > $expected_counts[$sku]) {
$extra = array_merge($extra, array_fill(0, $received_count - $expected_counts[$sku], $sku));
}
}
To integrate the WPForms shortcode into your custom WooCommerce "No products found" message, you can modify the code as follows:
add_action( 'woocommerce_no_products_found', function(){
remove_action( 'woocommerce_no_products_found', 'wc_no_products_found', 10 );
// WooCommerce custom message
$woocommerce_message = __( 'Adding parts to the site is an ongoing effort. The part you’re searching for may be within our range but not yet uploaded. If you are unable to find what you are looking for, please fill in our contact form below. Alternatively, email us at [email protected] , call us on 0044 1443 228329 or contact us via WhatsApp on 0044 732473727.', 'woocommerce' );
// WPForms shortcode
$wpforms_shortcode = do_shortcode('[wpforms id="1385"]');
// Output combined message and form
echo '
' . $woocommerce_message .'
' . $wpforms_shortcode .'
';
}, 9 );
// Assuming you have a PDO connection, replace it with your database connection
// Fetch data from the database
$query = $db->prepare("SELECT id, ranking, team FROM your_table");
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
// Initialize an array to store team scores
$teamScores = [];
// Process each person's ranking
foreach ($results as $row) {
$ranking = explode(',', $row['ranking']);
$id = $row['id'];
// Assign scores to teams based on their rankings
foreach ($ranking as $key => $team) {
$team = trim($team);
if (!isset($teamScores[$team])) {
$teamScores[$team] = 0;
}
$teamScores[$team] += $key + 1; // Assign scores based on the ranking
}
}
// Sort the teams based on their total scores
asort($teamScores);
// Output the sorted teams and their scores
foreach ($teamScores as $team => $score) {
echo "Team: $team, Total Score: $score<br>";
}
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.