z.ana
z.ana

Reputation: 1

function trim doesn't working right php

I have a problem with trim, it doesnt work as i expected, when the user writes only spaces in username form ("studentname") it should write you didint fill all fields ("niste izpolnili vsa polja") and i dont know how to achieve that, sorry if question is duplicate but i didnt find the answer to fix my problem here is the code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>vaja 5: PHP</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
	<link rel="stylesheet" type="text/css" href="style.css">
    </head>
<body>

<div class="container">
  <form name='form' method='post'>
  <label class="registracija">Registracija</label>
  
    <div class="form-group row">
      <label for="username" class="col-sm-2">Vnesi ime</label>
	   <div>
        <input type="text" id="studentname" name="studentname">
       </div>
    </div>
         
<div class="form-group row">
      <label for="password" class="col-sm-2">Vnesi geslo</label>
      <div>
        <input type="password" id="password1" name="password1">
      </div>
    </div>
	
	<div class="form-group row">
      <label for="password" class="col-sm-2">Geslo še enkrat</label>
      <div>
        <input type="password" id="password2" name="password2">
      </div>
    </div>	
	
		 <div>
        <input type="submit" value="Pritisni me" name="button">
      </div>
      </form>
</div>

<p>
<div class="container">
	<?php
if (isset($_POST["button"]))
{
echo $_POST['studentname']; 
} 
?>
<br>
<?php
if (isset($_POST["button"])) {
	$studentname = trim( $_POST['studentname'] );
	$password1 = $_POST ['password1'];
	$password2 = $_POST ['password2']; }

	
if ($_POST['studentname'] == "" || $_POST['password1'] == "" || $_POST['password2'] == "") {
        echo "Niste izpolnili vsa polja";
	} else	
if ($_POST['password1']!= $_POST['password2']) {
		echo "Gesli se ne ujemata";
	} else {
        echo "Registracija uspela";
    }
	

?>

<div>
</p>


  
	<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

Upvotes: 0

Views: 139

Answers (3)

Matthew Poer
Matthew Poer

Reputation: 1682

I would enhance the following to trim all input fields, not just the studentname

if (isset($_POST["button"])) {
  $studentname = trim($_POST['studentname']);
  $password1 = trim($_POST['password1']);
  $password2 = trim($_POST['password2']);
}

Then make sure write your conditions on the variables, not the original POSTed values. This is the main issue in your current code.

I also rewrote your else/if to be a bit more clear.

// ensure that user submitted all fields
if (
  studentname == ''
  || $password1 == ''
  || $password2 == ''
) {
  echo "Niste izpolnili vsa polja";
}

// ensure that passwords match
elseif($password1 != $password2) {
  echo "Gesli se ne ujemata";
}

// validations passed
else {
  echo "Registracija uspela";
}

Upvotes: 0

user3162712
user3162712

Reputation:

if (trim($_POST['studentname']) == "" || $_POST['password1'] == "" || $_POST['password2'] == "") { echo "Niste izpolnili vsa polja";

Upvotes: 0

ChristianM
ChristianM

Reputation: 1823

You register a variable $studentname with the trim result but you actually compare to $_POST['studentname']. Try to use $studentname === "".

Upvotes: 1

Related Questions