Jump to content
Stef's Coding Community

Valdate A Checkbox Using Php


Recommended Posts

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="utf-8">

<title>Web Form Design and Development :: Lesson 8</title>

<meta name="description" content="A Tuts+ Course">

<meta name="author" content="Adi Purdila">

 

<!-- Stylesheets -->

<link rel="stylesheet" href="css/normalize.css" />

<link rel="stylesheet" href="css/styles.css" />

 

<!--[if lt IE 9]>

<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

<![endif]-->

</head>

<body>

<header class="demo-header">

<hgroup>

<h1>Web Form Design and Development</h1>

<h3>Lesson 8: Server-side validation (the classic method)</h3>

</hgroup>

</header>

 

<div class="demo-lesson-nav">

<a href="lesson7.html" class="demo-lesson-nav-prev">← Previous Lesson</a>

<a href="lesson9.html" class="demo-lesson-nav-next">Next Lesson →</a>

</div> <!-- end demo-lesson-nav -->

 

<div class="demo-container">

<form action="scripts/lesson 8/validate.php" method="POST" id="form-new-account" novalidate>

<fieldset>

<legend>Create a New Account</legend>

<p>

<label for="name">Name:</label><br />

<input type="text" name="name" id="name" maxlength="25" class="validate-locally" />

<span class="demo-input-info">E.g. John Smith, must be between 3 and 25 characters, letters and spaces only</span>

<span class="demo-errors"></span>

</p>

 

<p>

<label for="username">Username:</label><br />

<input type="text" name="username" id="username" maxlength="15" class="validate-locally" />

<span class="demo-input-info">E.g. johnsmith, must be between 3 and 15 alphanumeric characters</span>

<span class="demo-errors"></span>

</p>

 

<p>

<label for="username">Gender:</label><br />

<select name="gender" id="gender">

<option value="0">- Select a Value -</option>

<option value="1">Male</option>

<option value="2">Female</option>

</select>

<span class="demo-errors"></span>

</p>

 

<p>

<label for="email">Email Address:</label><br />

<input type="email" name="email" id="email" class="validate-locally" />

<span class="demo-input-info">E.g. john@company.com</span>

<span class="demo-errors"></span>

</p>

 

<p>

<label for="password">Password:</label><br />

<input type="password" name="password" id="password" class="validate-locally" />

<span class="demo-input-info">Must be 6 characters minimum, alphanumeric</span>

<span class="demo-errors"></span>

</p>

 

<p>

<label for="confirm-password">Confirm Password:</label><br />

<input type="password" name="confirm-password" id="confirm-password" class="validate-locally" />

<span class="demo-errors"></span>

</p>

 

<p>

<input type="checkbox" id="subscribe" name="subscribe" value="on"/>

 

 

<label for="subscribe">Subscribe to newsletter</label>

</p>

 

<p>

<input type="submit" value="Create Account" name="submit" />

</p>

 

<p><small><em>Note: All fields are required!</em></small></p>

</fieldset>

</form>

 

</div> <!-- end demo-container -->

 

<footer class="demo-footer">

<p><small>a <a href="#">FORM</a> course by Ant brom</small></p>

</footer>

 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script src="scripts/lesson 8/validate.js"></script>

 

</body>

</html>

 

tryed messing with from aswell as the php validate page

**************************************************************************************************************************************

 

<?php

ini_set('error_reporting', E_ALL);

ini_set('display_errors', '1');

 

 

/***********************************************************************************************/

/* If nothing is posted then we exit */

/***********************************************************************************************/

 

 

if (!$_POST) {

die("This file cannot be accessed directly!");

}else{

 

if($_POST['$subscribe'] == 'subscribe') {

// The Cheque checkbox was checked.

} else {

// The Cheque checkbox was NOT checked.

}

 

 

}

 

 

 

 

/***********************************************************************************************/

/* Define regular expression patterns */

/***********************************************************************************************/

$expEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';

$expLettersOnly = '/^[a-zA-Z ]+$/';

$expLettersNumbers = '/^[a-zA-Z0-9]*$/';

 

 

 

/***********************************************************************************************/

/* Define the function for checking the field length */

/***********************************************************************************************/

function validateLength($fieldValue, $minLength) {

return (strlen(trim($fieldValue)) > $minLength);

}

 

 

 

/***********************************************************************************************/

/* Get the posted field values and validate each field */

/***********************************************************************************************/

$name = $_POST["name"];

$username = $_POST["username"];

$gender = $_POST["gender"];

$email = $_POST["email"];

$password = $_POST["password"];

$confirmPassword = $_POST["confirm-password"];

$subscribe = $_POST["subscribe"];

 

$errorExists = false;

$errors = "Errors: <ul>";

 

// Name

if (!validateLength($name, 2)) {

$errorExists = true;

$errors .= "<li>The name is too short!</li>";

}

if (preg_match($expLettersOnly, $name) !== 1) {

$errorExists = true;

$errors .= "<li>The name can only contain letters and spaces!</li>";

}

 

// Username

if (!validateLength($username, 2)) {

$errorExists = true;

$errors .= "<li>The username is too short!</li>";

}

if (preg_match($expLettersNumbers, $username) !== 1) {

$errorExists = true;

$errors .= "<li>The username can only contain alphanumeric characters!</li>";

}

 

// Gender

if ($gender === "1") {

$gender = "Male";

} else if ($gender === "2") {

$gender = "Female";

} else {

$errorExists = true;

$errors .= "<li>Please select a gender!</li>";

}

 

// Email

if (preg_match($expEmail, $email) !== 1) {

$errorExists = true;

$errors .= "<li>The email address format is invalid!</li>";

}

 

// Password

if (!validateLength($password, 5)) {

$errorExists = true;

$errors .= "<li>The password is too short!</li>";

}

if (preg_match($expLettersNumbers, $password) !== 1) {

$errorExists = true;

$errors .= "<li>The password can only contain alphanumeric characters!</li>";

}

 

// Confirm Password

if ($confirmPassword !== $password) {

$errorExists = true;

$errors .= "<li>The passwords don't match!</li>";

}

 

 

// If no errors, echo the results

if (!$errorExists) {

echo "<h3>Success! The form has been submitted!</h3>"

. "<p>Details:</p>"

. "<ul>"

. "<li>Name: $name</li>"

. "<li>Usernname: $username</li>"

. "<li>Gender: $gender</li>"

. "<li>Email: $email</li>"

. "<li>Subscribe to newsletter: $subscribe</li>"

. "</ul>";

 

} else {

echo "<h3>Error! Please address the following issues:</h3>"

. $errors;

}

?>

and now i do not know what i am or what i am doing been on it all day and think i have made it alot worse than it was at first

Edited by talos
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...