Note that there are some explanatory texts on larger screens.

plurals
  1. POchecking the state of checkbox using PHP (For t's & c's)
    text
    copied!<p>I am abit stuck on getting the state of a checkbox via PHP. I have spent the past 2 hours on google trying different methods but nothing seems to work...</p> <p>Basically I need the logic to say "if it is not checked, add an error".</p> <p>I have tried the following methods:</p> <pre><code>if(!isset($_POST['terms_and_conditions'])){ $this-&gt;addError("You must agree to our terms and conditions"); } if(isset($_POST['terms_and_conditions']) &amp;&amp; $_POST['terms_and_conditions'] == 'true'){ $this-&gt;addError("You must agree to our terms and conditions"); } if(!isset($_POST['terms_and_conditions']) &amp;&amp; !$_POST['terms_and_conditions'] == 'true'){ $this-&gt;addError("You must agree to our terms and conditions"); } if($_POST['terms_and_conditions'] == "false"){ $this-&gt;addError("You must agree to our terms and conditions"); } if($_POST['terms_and_conditions'] == false){ $this-&gt;addError("You must agree to our terms and conditions"); } </code></pre> <p>and here is the code for the checkbox:</p> <pre><code>&lt;div id="field"&gt; &lt;input type="checkbox" value="false" name="terms_and_conditions" id="terms_and_conditions"/&gt;&amp;nbsp;&lt;label for="terms_and_conditions"&gt;By clicking this you agree to this site's &lt;a href="terms_and_conditions.php"&gt;Terms and Conditions&lt;/a&gt;&lt;/label&gt; &lt;/div&gt; </code></pre> <p>am I being stupid and going wrong somewhere? It will only put the error out using this method and if the box IS checked (which is needed):</p> <pre><code>if(isset($_POST['terms_and_conditions'])){ $this-&gt;addError("You must agree to our terms and conditions"); } </code></pre> <p>But then if I try make it a negative, ie: with an exclamation mark "!" it displays nothing?</p> <p>What would be my best way to get around this?</p> <p>Thanks for your time!</p> <p><strong>UPDATE</strong></p> <p>Here is the register.php file:</p> <pre><code>&lt;?php require 'core/init.php'; include_once 'includes/files/overall/header_main.php'; $error_array = array(); if(Input::exists()) { if(Token::check(Input::get('token'))) { $validate = new Validate(); $validation = $validate-&gt;check($_POST, array( 'username' =&gt; array( 'required' =&gt; true, 'min' =&gt; 2, 'max' =&gt; 20, 'unique' =&gt; 'users'), 'password' =&gt; array( 'required' =&gt; true, 'min' =&gt; 3), 'password_again' =&gt; array( 'required' =&gt; true, 'matches' =&gt; 'password'), 'name' =&gt; array( 'required' =&gt; false, 'min' =&gt; 2, 'max' =&gt; 50) )); if($validation-&gt;passed()) { $user = new User(); $bcrypt = new Bcrypt(); try { $user-&gt;create(array( 'username' =&gt; Input::get('username'), 'password' =&gt; $bcrypt-&gt;hash(Input::get('password')), 'name' =&gt; Input::get('name'), 'joined' =&gt; date('Y-m-d H:i:s'), 'group' =&gt; 1 )); Session::flash('home', 'You have been registered and can now log in!'); Redirect::to('index.php'); } catch(Exception $e) { die($e-&gt;getMessage()); } } else { foreach($validate-&gt;errors() as $error) { echo $error, '&lt;br&gt;'; } } } } ?&gt; &lt;section id="aside"&gt; &lt;aside class="register"&gt; &lt;/aside&gt; &lt;/section&gt; &lt;section id="main_content"&gt; &lt;h1&gt;Sign Up&lt;/h1&gt; &lt;p class="bigger"&gt;Join 1000's of &lt;b&gt;docimes&lt;/b&gt; and meet new companions - it's easy!&lt;/p&gt; &lt;?php if(!empty($error_array)){ echo '&lt;ul&gt;'; foreach($error_array as $error){ echo '&lt;li&gt;'. $error .'&lt;/li&gt;'; } echo '&lt;/ul&gt;'; } ?&gt; &lt;form action="" method="post"&gt; &lt;div id="field"&gt; &lt;label for="name"&gt;Name&lt;/label&gt; &lt;input type="text" name="name" value="&lt;?php echo escape(Input::get('name')); ?&gt;" placeholder="name" /&gt; &lt;/div&gt; &lt;div id="field"&gt; &lt;label for="surname"&gt;Surname&lt;/label&gt; &lt;input type="text" name="surname" value="&lt;?php echo escape(Input::get('surname')); ?&gt;" placeholder="Surname" /&gt; &lt;/div&gt; &lt;div id="field"&gt; &lt;label for="email"&gt;Email&lt;/label&gt; &lt;input type="text" name="email" value="&lt;?php echo escape(Input::get('email')); ?&gt;" placeholder="Email" /&gt; &lt;/div&gt; &lt;div id="field"&gt; &lt;label for="username"&gt;Username&lt;/label&gt; &lt;input type="text" name="username" value="&lt;?php echo escape(Input::get('username')); ?&gt;" placeholder="Username" /&gt; &lt;/div&gt; &lt;div id="field"&gt; &lt;label for="password"&gt;Password&lt;/label&gt; &lt;input type="password" name="password" value="" placeholder="Password" /&gt; &lt;/div&gt; &lt;div id="field"&gt; &lt;label for="password_again"&gt;Password again&lt;/label&gt; &lt;input type="password" name="password_again" value="" placeholder="Password again" /&gt; &lt;/div&gt; &lt;input type="hidden" name="token" value="&lt;?php echo Token::generate(); ?&gt;"/&gt; &lt;input type="submit" value="Signup" /&gt; &lt;div id="field"&gt; &lt;input type="checkbox" value="" name="terms_and_conditions" id="terms_and_conditions"/&gt;&amp;nbsp;&lt;label for="terms_and_conditions"&gt;By clicking this you agree to this site's &lt;a href="terms_and_conditions.php"&gt;Terms and Conditions&lt;/a&gt;&lt;/label&gt; &lt;/div&gt; &lt;/form&gt; &lt;/section&gt; &lt;?php include_once 'includes/files/overall/footer_main.php'; ?&gt; </code></pre> <p>and the validate.php class:</p> <pre><code>&lt;?php class Validate { private $_passed = false, $_errors = array(), $_db = null; public function __construct() { $this-&gt;_db = DB::getInstance(); } public function check($source, $items = array()) { foreach($items as $item =&gt; $rules) { foreach($rules as $rule =&gt; $rule_value) { $value = trim($source[$item]); if($rule === 'required' &amp;&amp; $rule_value === true &amp;&amp; empty($value)) { $this-&gt;addError("{$item} is required."); } else if (!empty($value)) { switch($rule) { case 'min': if(strlen($value) &lt; $rule_value) { $this-&gt;addError("{$item} must be a minimum of {$rule_value} characters."); } break; case 'max': if(strlen($value) &gt; $rule_value) { $this-&gt;addError("{$item} must be a maximum of {$rule_value} characters."); } break; case 'matches': if($value != $source[$rule_value]) { $this-&gt;addError("{$rule_value} must match {$item}."); } break; case 'unique': $check = $this-&gt;_db-&gt;get('users', array($item, '=', $value)); if($check-&gt;count()) { $this-&gt;addError("{$item} is already taken."); } break; case 'accepted': if($rule === 'accepted' &amp;&amp; $rule_value === true){ $this-&gt;addError("You must agree to our terms and conditions"); } break; } } } } if(empty($this-&gt;_errors)) { $this-&gt;_passed = true; } return $this; } protected function addError($error) { $this-&gt;_errors[] = $error; } public function passed() { return $this-&gt;_passed; } public function errors() { return $this-&gt;_errors; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload