Hi,
I've created a PHP form which links to a MySQL database, however trouble is I've got all the fields validating and submitting except for the radio buttons and checkbox field. I have an idea where it is going wrong, but just dont know how to fix.
For the radio buttons, I need it to submit "Yes" or "No" to the database. Trouble is the validation (I think) needs to go in the "value" field which is also where I would have throught the yes and no would need to be. Any ideas how I should be coding it?
Futhermore, with the checkbox I need to see if it is checked and return "Agreed" to the database if checked. Both fields have validation on to make sure they have been selected.
All the code is below, radio buttons and checkbox are in the last few rows in the table.
<?php
require_once('db.php');
require_once('functions.php');
// date
$DATE = date(cleanInput("Y-m-d", $conn));
$errors = array();
// If request is a form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$NAME = cleanInput($_POST['NAME'], $conn);
$EMAIL = cleanInput($_POST['EMAIL'], $conn);
$COMMENTS = cleanInput($_POST['COMMENTS'], $conn);
$OVER18 = cleanInput($_POST['OVER18'], $conn);
$TERMS = cleanInput($_POST['TERMS'], $conn);
// Validation
// Check NAME is not less than 2 characters
if (strlen($NAME) < 2) {
$errors['NAME'] = "Your name is not long enough";
}
// Check TELEPHONE is valid
if (0 === preg_match("/^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/", $_POST['TELEPHONE'])) {
$errors['TELEPHONE'] = "Please enter valid phone number";
}
// Check EMAIL is not less than 2 characters
if (strlen($EMAIL) < 2) {
$errors['EMAIL'] = "Your email address is not long enough";
}
// Check COMMENTS is not less than 3 characters
if (strlen($COMMENTS) < 3) {
$errors['COMMENTS'] = "Please enter a comment";
}
// Check OVER 18
if ($OVER18 < 18) {
$errors['OVER18'] = "You must be 18 or over";
}
// Check TERMS have been agreed
if ($TERMS == "No") {
$errors['TERMS'] = "It is required of you to agree to the terms before continuing";
}
// If no validation errors
if (0 === count($errors)) {
// Sanitise details
$NAME = cleanInput($_POST['NAME'], $conn);
$TELEPHONE = cleanInput($_POST['TELEPHONE'], $conn);
$EMAIL = cleanInput(trim($_POST['EMAIL']), $conn);
$COMMENTS = cleanInput($_POST['COMMENTS'], $conn);
$OVER18 = cleanInput($_POST['OVER18'], $conn);
$TERMS = cleanInput($_POST['TERMS'], $conn);
// Insert user into the database
$query = "
INSERT INTO
testform (
DATE
, NAME
, TELEPHONE
, EMAIL
, COMMENTS
, OVER18
, TERMS
) VALUES (
'$DATE'
, '$NAME'
, '$TELEPHONE'
, '$EMAIL'
, '$COMMENTS'
, '$OVER18'
, '$TERMS'
)";
// for debugging
print_r($_POST);
$result = mysqli_query($conn, $query) or die(mysqli_error($conn) . $query);
if ($result != FALSE) {
// Form submitted successfully
header("Location: thankyou.php");
exit;
}
}
} else {
// DEBUGGING ONLY - DISABLE IN PRODUCTION SITE
// echo "<br/><br /> MySQLi Error: " . mysqli_error($conn);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table class="form">
<tr class="<?php echo form_row_class("NAME", $errors); ?>">
<th><label for="NAME">Telephone</label></th>
<td><input name="NAME" id="NAME" type="text" value="<?php echo isset($_POST['NAME']) ? hsc($_POST['NAME']) : ''; ?>" />
<?php echo error_for("NAME", $errors); ?></td>
</tr>
<tr class="<?php echo form_row_class("TELEPHONE", $errors); ?>">
<th><label for="TELEPHONE">Telephone</label></th>
<td><input name="TELEPHONE" id="TELEPHONE" type="text" value="<?php echo isset($_POST['TELEPHONE']) ? hsc($_POST['TELEPHONE']) : ''; ?>" />
<?php echo error_for("TELEPHONE", $errors); ?></td>
</tr>
<tr class="<?php echo form_row_class("EMAIL", $errors); ?>">
<th><label for="EMAIL">Email Address</label></th>
<td><input name="EMAIL" id="EMAIL" type="text" value="<?php echo isset($_POST['EMAIL']) ? hsc($_POST['EMAIL']) : ''; ?>" />
<?php echo error_for("EMAIL", $errors); ?></td>
</tr>
<tr class="<?php echo form_row_class("COMMENTS", $errors); ?>">
<th><label for="COMMENTS">Comments</label></th>
<td><textarea name="COMMENTS" id="COMMENTS"><?php echo isset($_POST['COMMENTS']) ? hsc($_POST['COMMENTS']) : ''; ?></textarea>
<?php echo error_for("COMMENTS", $errors); ?></td>
</tr>
<tr class="<?php echo form_row_class("OVER18", $errors); ?>">
<th><label for="OVER18">Tick box to agree to terms and conditions</label></th>
<td colspan="2">
<label for="OVER18_YES">Yes</label>
<input type="radio" name="OVER18_NO" id="OVER18" value="<?php echo isset($_POST['OVER18']) ? hsc($_POST['OVER18']) : ''; ?>" />
<label for="OVER18_NO">NO</label>
<input type="radio" name="OVER18_NO" id="OVER18" value="<?php echo isset($_POST['OVER18']) ? hsc($_POST['OVER18']) : ''; ?>" />
<?php echo error_for("OVER18", $errors); ?></td>
</tr>
<tr class="<?php echo form_row_class("TERMS", $errors); ?>">
<th><label for="TERMS">Tick box to agree to terms and conditions</label></th>
<td colspan="2">
<input type="checkbox" name="TERMS" id="TERMS" value="<?php echo isset($_POST['TERMS']) ? hsc($_POST['TERMS']) : ''; ?>" />
<?php echo error_for("TERMS", $errors); ?></td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" value="Go!" /></td>
</tr>
</table>
</form>
</body>
</html>
Or to narrow it down for ease of finding, this part specifically.....
<th><label for="OVER18">Tick box to agree to terms and conditions</label></th>
<td colspan="2">
<label for="OVER18_YES">Yes</label>
<input type="radio" name="OVER18_NO" id="OVER18" value="<?php echo isset($_POST['OVER18']) ? hsc($_POST['OVER18']) : ''; ?>" />
<label for="OVER18_NO">NO</label>
<input type="radio" name="OVER18_NO" id="OVER18" value="<?php echo isset($_POST['OVER18']) ? hsc($_POST['OVER18']) : ''; ?>" />
<?php echo error_for("OVER18", $errors); ?></td>
</tr>
<tr class="<?php echo form_row_class("TERMS", $errors); ?>">
<th><label for="TERMS">Tick box to agree to terms and conditions</label></th>
<td colspan="2">
<input type="checkbox" name="TERMS" id="TERMS" value="<?php echo isset($_POST['TERMS']) ? hsc($_POST['TERMS']) : ''; ?>" />
<?php echo error_for("TERMS", $errors); ?></td>
</tr>
I tried this with a drop down list, however that was fine as it has a "Select" field. Thanks in advance.