$valid_payment_methods = array('credit card', 'debit card', 'netbanking', 'PayPal'); $user_payment_method = $_POST['payment_method']; if(in_array($user_payment_method, $valid_payment_methods)){ // process payment } else{ // show error message }
$db = new mysqli('localhost', 'username', 'password', 'my_db'); $user_shipping_option = $_POST['shipping_option']; $stmt = $db->prepare("SELECT * FROM shipping_options WHERE option_name = ?"); $stmt->bind_param("s", $user_shipping_option); $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows > 0){ // proceed with shipping } else{ // show error message }This example illustrates a more advanced validation technique of querying a database for valid shipping options. The script first creates a database connection using mysqli. It then gets the user's shipping option input from $_POST global variable. The script prepares a SELECT SQL statement that looks for a row in a shipping_options table that matches the input value. The statement uses the bind_param() method to bind the input value to a placeholder in the SQL query. The script then executes the statement and gets the result using the get_result() method. If there is at least one matching row, the script proceeds with the shipping. Otherwise, it displays an error message. Package Library: The package/library that can help with validating order ways in PHP is the Validation library from Laravel. This package provides a comprehensive set of validation rules and methods that can be used to validate almost any type of input value. The Laravel Validation library can be installed via Composer and is compatible with any PHP application.