Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To validate the checkout form using Ajax and PHP on server side. User\'s input i

ID: 3748345 • Letter: T

Question

To validate the checkout form using Ajax and PHP on server side. User's input is validated on the fly. If error is found, appropriate information is displayed. All required fields should be checked for completion, and all data that are entered should be checked for 'correctness'. For example, buyer contact details (name, phone number, email address, etc.), delivery address (unit number/street name, city, postal code, etc.), delivery method selected and payment methods (credit card number, expiry date, card holder name, CVV, etc.) are fields expected in the checkout form to be validated. Regular expression is recommended (NOT compulsory) for pattern matching in PHP. You decide what validation is required and how the validation is performed. For instance, the required fields checked for completion the fields checked for a specific length, numeric data or a predetermined format the messages provided help the user fix the 'problem'

Explanation / Answer

<?php

$value = $_GET['query'];

$formfield = $_GET['field'];

// Check Valid or Invalid name when user enters name in Name input field.

if ($formfield == "name") {

if (strlen($value) < 20) {

echo "Must be max 20 letters";

} else {

echo "<span>Valid</span>";

}

}

// Check Valid or Invalid contact no when user enters contact no in Contact No input field.

if ($formfield == "con") {

if(!preg_match('/^[0-9]{10}+$/', $value)){

echo "Invalid contact no";

} else {

echo "<span>Valid</span>";

}   

// Check Valid or Invalid email when user enters email in email input field.

if ($formfield == "email") {

if (!preg_match("/([w-]+@[w-]+.[w-]+)/", $value)) {

echo "Invalid email";

} else {

echo "<span>Valid</span>";

}

}

if ($formfield == "address") {

if (!preg_match("/[A-Za-z0-9-\,.]+/", $value)) {

echo "Invalid address";

} else {

echo "<span>Valid</span>";

}

}

if ($formfield == "cc_number") {

if (!preg_match("[^0-9]", $value)) {

echo "Invalid number";

} else {

echo "<span>Valid</span>";

}

}

if ($formfield == "Expiry Date") {

if(!is_numeric($value) || $value < 1 || $value > 12){

echo "Invalid date";

} else {

echo "<span>Valid</span>";

}

}

if ($formfield == "Card Holder Name") {

if (strlen($value) < 20) {

echo "Must be max 20 letters";

} else {

echo "<span>Valid</span>";

}

}

?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote