Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP array returns nothing (OOP newbie needs some help)
    primarykey
    data
    text
    <p>I am trying to create an form validator class that will check to make sure that people have typed a particular number of characters into specified fields. Here is my page:</p> <pre><code>if ($_SERVER['REQUEST_METHOD'] == 'POST') { require_once('functions.php'); require_once('formValidator.php'); $validator = new FormValidator(); $validator-&gt;setFirst($_POST['first']); $validator-&gt;setLast($_POST['last']); $validator-&gt;setEmail($_POST['email']); $errors = array(); $validator-&gt;checkLength($errors); echo $errors; if (!empty($errors)) { echo '&lt;p&gt;' . $message . '&lt;/p&gt;'; } } </code></pre> <p>// form here (code above is in PHP tags, of course)</p> <p>Here is my class: </p> <pre><code>class FormValidator { public $first; public $last; public $email; public $fields_with_lengths; public function setFirst($first) { $this-&gt;first = $first; } public function setLast($last) { $this-&gt;last = $last; } public function setEmail($email) { $this-&gt;email = $email; } function setLengths() { $this-&gt;fields_with_lengths = array('first' =&gt; 2, 'last' =&gt; 2, 'email' =&gt; 8); } function checkLength($fields_with_lengths) { $length_errors = array(); foreach($fields_with_lengths as $fieldname =&gt; $maxlength) { if (strlen(trim($_POST[$fieldname])) &gt; $maxlength) { $length_errors[] = $fieldname; } } if (!empty($length_errors)) { $message = 'There were no errors'; } else { $message = 'There were errors'; } return $message; } } $validator = new FormValidator(); $validator-&gt;setFirst($_POST['first']); $validator-&gt;setLast($_POST['last']); $validator-&gt;setEmail($_POST['email']); </code></pre> <p>Can anyone see what I am doing wrong? I'd be grateful for some help. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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