5 Replies - 1005 Views - Last Post: 30 September 2012 - 08:54 PM Rate Topic: -----

#1 biggmann  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 02-February 10

Ho to properly use strlen?

Posted 30 September 2012 - 10:17 AM

I am on the finishing end of my project and I need to verify that certain objects have a certain number of chars. and a number of digits. I am trying to use strlen and I believe it does that but it still outputs the error message even when the condition is met. I know it is some dumb mistake on my part but I cannot figure out where.
Starting at line 55


<?php
//Array to hold all of the province choices
$PROVINCES = array(		"--" => "---Please Select Provinces---",
						"nf"=>"Newfoundland",
						"pe"=>"PrinceEdwardIsland",
						"nb"=>"New Brunswick",
						"ns"=>"Nova Scotia",
						"qc"=>"Quebec",
						"on"=>"Ontario",
						"mb"=>"Manitoba",
						"sk"=>"Saskatchewan",
						"ab"=>"Alberta",
						"bc"=>"British Columbia",
						"nt"=>"Northwest Territories");
 
//array for all the possible error messages
$errorMessages = array ('Username cannot be empty', 'Email cannot be empty', 'Year cannot be empty',
'Status must be selected', 'Location must be selected', 'Username must contain at least 5 characters'
);

if ($_SERVER['REQUEST_METHOD'] == "GET") {
	$dispWelcomeMsg = TRUE;
}

if ($_SERVER['REQUEST_METHOD'] == "POST") {
	
	if (empty($_POST['uname']) == TRUE){
		$errorMessages[0]; 
	}
	if (empty($_POST['email']) == TRUE){
		$errorMessages[1]; 
	}
	
	if (empty($_POST['year']) == TRUE){
		$errorMessages[2]; 
	}
	if (empty($_POST['status']) == TRUE){
		$errorMessages[3]; 
	}
	if (empty($_POST['location']) == TRUE){
		$errorMessages[4]; 
	}
		
	if (count($errorMessages) > 0)  { 
		$dispErrorMsgs = TRUE;
	} else {
		foreach ($_POST as $key => $val){	
			$successMessages[$key] = $val;	
		}
		$dispSuccessMsg = TRUE;
	}
}


if (strlen($_REQUEST["uname"]) < 5) {
        echo "uname:" . $_REQUEST["uname"]; //check to see if username is at least 5 characters long
} else 
        $errorMessages[5];

array_walk_recursive($_POST, function (&$val) //Trim all the white space from strings
{ 
    $val = trim($val); 
});
?>
<html>
<head>
<title>Comp10065 - Lab 2: Simple Form Validation</title>
<style type="text/css">
body {
	margin: 0;
	padding: 0;
	font: 80%/1.5 Arial, Helvetica, sans-serif;
	color: #111;
	background-color: white;
}
h2 {
	margin: 0px;
	padding: 10px;
	font-family: Georgia, "Times New Roman", Times, serif;
	font-size: 200%;
	font-weight: normal;
	color: #FFF;
	background-color: #CCC;
	border-bottom: #BBB 2px solid;
}
h1 {
	color:#006600
}
p#copyright {
	margin: 20px 10px;
	font-size: 90%;
	color: #999;
}
div.form-container {
	margin: 10px;
	padding: 5px;
	background-color: #FFF;
	border: #EEE 1px solid;
}
p.legend {
	margin-bottom: 1em;
}
p.legend em {
	color: #C00;
	font-style: normal;
}
div.errors {
	margin: 0 0 10px 0;
	padding: 5px 10px;
	border: #FC6 1px solid;
	background-color: #FFC;
}
div.errors p {
	margin: 0;
}
div.errors p em {
	color: #C00;
	font-style: normal;
	font-weight: bold;
}
div.success {
	margin: 0 0 10px 0;
	padding: 5px 10px;
	border: #FC6 1px solid;
	background-color: #FFC;
}
div.success p {
	margin: 0;
	color:#006633
}
div.success li {
	list-style-type: none;
}
div.form-container form p {
	margin: 0;
}
div.form-container form p.note {
	margin-left: 170px;
	font-size: 90%;
	color: #333;
}
div.form-container form fieldset {
	margin: 10px 0;
	padding: 10px;
	border: #DDD 1px solid;
}
div.form-container form legend {
	font-weight: bold;
	color: #666;
}
div.form-container form fieldset div {
	padding: 0.25em 0;
}
div.form-container label, div.form-container span.label {
	margin-right: 10px;
	padding-right: 10px;
	width: 150px;
	display: block;
	float: left;
	text-align: right;
	position: relative;
}
div.form-container label.error, div.form-container span.error {
	color: #000;
}
div.form-container select.error, div.form-container div.error {
	background-color: #FFC;
}
div.form-container label em, div.form-container span.label em {
	position: absolute;
	right: 0;
	font-size: 120%;
	font-style: normal;
	color: #C00;
}
div.form-container input.error {
	border-color: #C00;
	background-color: #FFC;
}
div.form-container input:focus, div.form-container input.error:focus, div.form-container textarea:focus {
	background-color: #FFC;
	border-color: #FC6;
}
div.form-container div.controlset {
	margin: 3px
}
div.form-container div.controlset label, div.form-container div.controlset input {
	display: inline;
	float: none;
}
div.form-container div.controlset div {
	margin-left: 170px;
}
div.form-container div.buttonrow {
	padding-left: 400px;
	border: 1px solid #CCCCCC
}
div#wrapper {
	width: 700px;
	margin-left: auto;
	margin-right: auto
}
pre {
	color:black;
	font-size: 10pt;
	font-weight: bold;
	margin-left: 30px
}
p#welcomeMsg {
	font-size:larger;
	color:green;
	font-style:italic;
	font-weight: bold
}
table#submissionTable {
	border-collapse: collapse
}
table#submissionTable tr td, table#submissionTable tr th {
	vertical-align:top;
	font-size: smaller;
	border: 1px solid #CCC;
	padding: 3px
}
</style>
</head>

<body>
<div id="wrapper">
  <h1> Form Validation Lab !!!!!</h1>
  <div class="form-container">
    <? if ($dispWelcomeMsg): ?>
    <p id="welcomeMsg">Please fill in the form........</p>
    <? endif; ?>
    <? if ($dispErrorMsgs): ?>
    <div class="errors">
      <p><em>Oops... the following errors were encountered:</em></p>
      <ul>
        <? foreach($errorMessages as $index => $errorMessage):    ?>
        <li>
          <?=  $errorMessage ?>
        </li>
        <?  endforeach;   ?>
      </ul>
      <p>Please correct the errors and re-submit this form.</p>
    </div>
    <? endif; ?>
    <? if ($dispSuccessMsg) { ?>
    <div class="success">
      <p>Thank you for your submission. You entered the following values:</p>
      <table id="submissionTable">
        <? foreach($successMessages as $index => $successMessage): ?>
        <tr>
          <th><?=  $index ?></th>
          <td><?= $successMessage ?></td>
        </tr>
        <? endforeach; ?>
      </table>
    </div>
    <? }  ?>
    <form action="<?= $_SERVER['PHP_SELF']  ?>" method="post">
      <input type="hidden" name="act" value="post">
      <div class="buttonrow">
        <input type="submit" value="Submit This Form " class="button" />
        <a href="<?= $_SERVER['PHP_SELF']  ?>" >Start Again</a> </div>
      <p class="legend"><strong>Note:</strong> Required fields are marked with an asterisk (<em>*</em>)</p>
      <fieldset>
        <legend>User Details</legend>
        <div>
          <label for="uname" >User Name <em>*</em></label>
          <input id="uname" type="text" name="uname"  value="<?=   $_POST['uname']; ?>"  />
          (must be greater than 5 chars)</div>
        <div>
          <label for="email">Email Address </label>
          <input id="email" type="text" name="email" value="" />
        </div>
      </fieldset>
      <fieldset>
        <legend>Submission</legend>
        <div>
          <label for="year">Year (YYYY) <em>*</em></label>
          <input id="year" type="text" name="year" 
				value="" size="4" maxlength="4"  />
          (4 digit number)</div>
        <div>
          <label for="date">Month (MM)</label>
          <input id="date" type="text" name="month" value="" size="4" maxlength="2" />
          (number ranging from 1-12)</div>
      </fieldset>
      <fieldset>
        <legend>Preferences</legend>
        <div>
          <label for="type" >Province (Multiple Select) <em>*</em></label>
          <select name = "province[]" multiple size = "12" id="type"  >
            <? foreach($PROVINCES as $shortName => $longName):   ?>
            <option value="<?= $shortName ?>" >
            <?= $longName ?>
            </option>
            <? endforeach; ?>
          </select>
        </div>
        <div class= 'controlset'   > <span class="label">Status (Mult Select)<em>*</em></span>
          <input name="status[]" id="approved" value="approved" type="checkbox"  />
          <label for="approved">Approved</label>
          <input name="status[]" id="pending" value="pending" type="checkbox"  />
          <label for="pending">Pending Application</label>
          <input name="status[]" id="actives" value="actives" type="checkbox"  />
          <label for="actives">Active Service</label>
        </div>
        <div class= 'controlset'    > <span class="label"> Location <em>*</em></span>
          <input name="location"   value="Garage" type="radio" />
          <label for="radio1">Garage</label>
          <input name="location"   value="Attic" type="radio" />
          <label for="radio2">Attic</label>
          <input name="location"   value="House" type="radio" />
          <label for="radio3">House</label>
        </div>
      </fieldset>
    </form>
    <hr>
    <p>This debug info is here to help you. You are only required to display the POST array</p>
    <pre>$TPL = Array
(
    [dispWelcomeMsg] => 1
)
</pre>
  </div>
</div>
</body>
</html>


This post has been edited by biggmann: 30 September 2012 - 10:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Ho to properly use strlen?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: Ho to properly use strlen?

Posted 30 September 2012 - 10:26 AM

Most likely (I've given up reading code that mixes languages) you're checking a value that hasn't been submitted yet. That's the problem with mixing PHP and other languages like HTML, CSS, and Javascript. It's easy to forget the order in which things happen!
Was This Post Helpful? 0
  • +
  • -

#3 biggmann  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 02-February 10

Re: Ho to properly use strlen?

Posted 30 September 2012 - 10:31 AM

I agree that mixing the two languages is very confusing, I go in and out of HTML and then throw some PHP into it it is very confusing especially when just starting out. I like your post on having HTML on one page and PHP on the other, once I make it through this I will have to try and use that method
Was This Post Helpful? 0
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 928
  • View blog
  • Posts: 3,212
  • Joined: 19-January 10

Re: Ho to properly use strlen?

Posted 30 September 2012 - 12:08 PM

On line 55, you're checking if the submitted name is less than 5. You need to turn that < (less than) into a > (more than).
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Ho to properly use strlen?

Posted 30 September 2012 - 03:27 PM

Uhm, these lines?

	if (empty($_POST['uname']) == TRUE){
		$errorMessages[0]; 
	}
	if (empty($_POST['email']) == TRUE){
		$errorMessages[1]; 
	}
	
	if (empty($_POST['year']) == TRUE){
		$errorMessages[2]; 
	}
	if (empty($_POST['status']) == TRUE){
		$errorMessages[3]; 
	}
	if (empty($_POST['location']) == TRUE){
		$errorMessages[4]; 
	}


They don't do anything of use.
Was This Post Helpful? 1
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Ho to properly use strlen?

Posted 30 September 2012 - 08:54 PM

It's also kind of redundant to do if (empty(...) == TRUE). It's exactly the same thing as doing just: if (empty(...)). The == TRUE is implied in all boolean checks. - The only place where you need to compare something to TRUE is if you are doing strict comparisons, using three equal signs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1