Jump to content
Larry Ullman's Book Forums

My Form Is Not Working Correctly When I Use Strip_Tags, The Trim Function With If And Else If Statement.


Recommended Posts

Below is the Xammp information:

 

XAMPP Version: 7.1.10

XAMPP Control Panel Version: V3.2.2

PHP version: 7.1.10

 

Windows version:

64 bit, Windows 10 

And Xampp is installed on my C drive.

 

I wanted to practice chapter 6 validation functions, if, if else statements, and security functions in PHP.  Every time I type in my first name in the form and hit the submit button, the contact.php script gives me the error Please enter your first name, even though I'm using the empty() validation function. My else statement never even fires off.

 

I'm not sure what I'm doing wrong here, if the quotes I'm using, or if something is not declared correctly in my code. In the contact.php lines, 21-27 are my variables I created from my contact.html form. The if else statement starts on lines 35-39. I only did one statement so far, because every variable I've tried fails, just like first one.

 

Below I will paste my HTML, PHP, and CSS for the form. The form looks good with the CSS, but it just doesn't work properly :mellow: Thanks in advance for any help.

 

 

HTML:

<!doctype html>
  <html>
    <head>
	  <meta charset = "utf-8">
	  <title>Contact Us</title>
	  <meta name="viewport" content="width=device-width, initial-scale=1">
	  <link rel="stylesheet" type="text/css" href="css/normalize.css">
	  <link rel="stylesheet" href="css/styles.css?v=e031e80c3d8b">
	  <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700%7COswald" rel="stylesheet">
	  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	</head>
	<body>
		<div class="container">
			<form action="contact.php" method="POST">
				<label for="first_name">First Name:</label>
				<input type="text" id="first_name" name="first_name">

				<label for="last_name">Last Name:</label>
				<input type="text" id="last_name" name="last_name">

				<label for="email">Email:</label>
				<input type="email" id="email" name="email">

				<label for="password">Password:</label>
				<input type="password" id="password" name="password">

				<label for="confirm_password">Confirm Password</label>
				<input type="password" id="confirm_password" name="confirm_password">

				<label for="year">Birth Year:</label>
				<input type="number" id="year" name="year">

				<label for="interest">Interest:</label>
				<select name="hobbies" id="interest">
					<option value="html">HTML</option>
					<option value="css">CSS</option>
					<option value="programming">Programming</option>
					<option value="photoshop">Photoshop</option>
				</select>
				<label for="color">Color:</label>
				<select name="color" id="color">
					<option value="red">Red</option>
					<option value="yellow">Yellow</option>
					<option value="green">Green</option>
					<option value="blue">Blue</option>
				</select>
				<label for="comments">Comments:</label>
				<textarea name="comments" id="comments" name="comments"></textarea>
				<label for="terms">Terms And Conditions</label>
				<input type="checkbox" id="terms" name="terms" value="Yes"><label class="yes">Yes</label>
				<input type="submit" id="submit" name="submit" value="submit">
			</form>
	  </div>
	</body>
  </html>

Contact PHP:

<!doctype html>
  <html>
    <head>
	  <meta charset = "utf-8">
	  <title>Contact Us</title>
	  <meta name="viewport" content="width=device-width, initial-scale=1">
	  <link rel="stylesheet" type="text/css" href="css/normalize.css">
	  <link rel="stylesheet" href="css/styles.css?v=e031e80c3d8b">
	  <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700%7COswald" rel="stylesheet">
	  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	</head>
	<body>
		<h1>Registration Results</h1>
		<?php
		  /* Error Reporting */
		  ini_set('display_errors', 1);
		  error_reporting(E_ALL);
			
			// Declaring the variable from the form and applying security to the for

			$first_name = strip_tags(trim($_POST['first_name']));
			$last_name = strip_tags(trim($_POST['last_name']));
			$email = strip_tags(trim($_POST['email']));
			$password = strip_tags(trim($_POST['password']));
			$confirm_password = strip_tags(trim($_POST['confirm_password']));
			$year = strip_tags(trim($_POST['year']));
			$comments = nl2br(strip_tags($_POST['comments']));

		  // Flag variable to track success
		  $okay = true;

		  /* Setting the first conditional to make sure $first_name isn't empty
		  and printing a message with $first_name if the user did. */
      
		 if (empty($_POST['$first_name'])) {
		 	print '<p class="error">Please enter your first name</p>';
		 } else {
		 	print "<p>$first_name</p>";
		 }
		  // If there were no errors, print a success message:

		  if ($okay) {
		  	print '<p>You have been succesfully registered (but not really).</p>';
		  }
		  
		?>
	</body>
  </html>

CSS:

/* Font family selection

  font-family: 'Oswald', sans-serif;
  font-family: 'Open Sans Condensed', sans-serif;
  Oswald can be 300 and 700 for font weight.

*/

body {
	background-color: rgba(110,202,233, .5);
}

.container {
	background: linear-gradient(to bottom, rgba(195,217,255,1) 20%,rgba(152,176,217,1) 99%); 
	max-width: 45em;
	margin: 0 auto;
	text-align: center;
}

label,
#submit {
	display: block;
	font-family: 'Oswald', sans-serif;
	font-size: 1.2em;
	font-weight: 700;
	padding: .45em 0;
}

input,
select,
textarea {
	font-family: 'Open Sans Condensed', sans-serif;
	width: 15em;
}

.yes {
	display: inline;
}

#terms {
	width: 5%;
}

#submit {
	width: 4em;
	margin-left: 2em;
}

/* coloring the errors in the if statements */

.error {
	color: red;
}
Link to comment
Share on other sites

 

if (empty($_POST['$first_name'])) {
You need to get rid of that dollar sign there before "first_name".

 

Thank you, Larry, for the quick reply. I have a couple of questions, I did try the first_name without the dollar sign before I posted my question, and the script did work. I'm just kinda lost on how this script works without the dollar sign in the if statement if I use $first_name like this: 

$first_name = strip_tags(trim($_POST['first_name']));

I did the correct thing up above correct, by declaring $first_name, and then my functions? I just don't get why to use first_name instead of $first_name in my if statement.

Link to comment
Share on other sites

So anything that's of the form $whatever is a variable, such as $first_name and $_POST. $_POST is a specific variable type, an array, so $_POST['whatever'] refers to a specific element in that array, where "whatever" is the key or index for that array element. When you use $_POST['$first_name'] it's literally looking for the element within the $_POST array that's indexed at $first_name, and there isn't one. This is why $_POST['first_name'] works but $_POST['$first_name'] doesn't.

 

Moreover, since you already defined $first_name above, you should just use 

if empty($first_name)) {

as your conditional. 

Link to comment
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...
 Share

×
×
  • Create New...