功能修剪不能正常运行php

时间:2018-03-17 15:27:22

标签: php trim

我有一个修剪问题,它没有像我预期的那样工作,当用户只用用户名表格写出空格时(" studentname")它应该写你doint填充所有字段(" niste) izpolnili vsa polja")我不知道如何实现这一点,抱歉,如果问题重复,但我没有找到解决问题的方法来解决我的问题 这是代码:



<!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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

您使用修剪结果注册变量$studentname,但实际上您与$_POST['studentname']进行了比较。尝试使用$studentname === ""

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我会增强以下内容来修剪所有输入字段,而不仅仅是studentname

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

然后确保在变量上写下条件,而不是原始的POSTed值。这是您当前代码中的主要问题。

我还重写了你的其他/如果要更清楚一点。

// 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";
}