-1

I'm new at PHP so I made my first activity for practice and i can't determine the problem in my code.

<!DOCTYPE html>
<html>
<head><title>PHP activity</title></head>

<form action="act1.php" method="post"> 
Name: <input type="text" name="name"><input type="submit">

</form>
    <?php

 if ($_POST["name"] = " ") {
     echo "Please Enter Your Name!";
 }
else {
echo $_POST["name"];
}
?>
</html>

It does not give me my expected output. I was expecting that if the form is blank it must return "Please bla bla" , else , if it's not blank, it will return the input which is name.

But the output is always "Please bla bla"

I am really new to PHP

jerome_mjt
  • 280
  • 3
  • 12

6 Answers6

1

= is for assignment not comparison change if ($_POST["name"] = " ") to if ($_POST["name"] == "") So your if/else becomes

if ($_POST["name"] == "") { 
     echo "Please Enter Your Name!";
 }
else {
echo $_POST["name"];
}
Nabin Kunwar
  • 1,965
  • 14
  • 29
1

Change this lineif ($_POST["name"] = " ") {

To this if ($_POST["name"] == "") {

Hassan Naqvi
  • 424
  • 6
  • 18
0

Because you have used the wrong operator for comparison.

  • = is used to assign value to a variable. $x=5; means the value of x has been set to 5.
  • == is used for comparison. $x==5; will return true if the the value of $x is 5.

Make the correction as shown below:

    if ($_POST["name"] ==" ") 
{ 
echo "Please Enter Your Name!"; 
} 
else
 { 
echo $_POST["name"]; 
}

Also I think, you are checking if $_POST["name"] is null or not in the if condition. But your if block will only be entered if the value of $_POST["name"] is a space. It may not work for multiple spaces and when nothing is entered at all.

You may have to change the condition to:

if ($_POST["name"] =="\0" || ($_POST["name"] ==" " )

This again wouldn't work to the expectation if the user enters multiple spaces or special characters.

Note:

\0 means null character.

Mathews Mathai
  • 1,707
  • 13
  • 31
0

You are not using comparison in your example its assigning the value.

One more issue $var == " " its not correct it will consider empty space as value "" != " "

You can also use empty() in your example as:

if (empty($_POST["name"])) { 
   echo "Please Enter Your Name!"; 
} else { 
   echo $_POST["name"]; 
}
devpro
  • 16,184
  • 3
  • 27
  • 38
0

Check if your passed some data in POST parameter like below.

if (trim($_POST["name"]) == "") {

}

Don't forget to trim it to avoid blank character submission.

You have two mistakes in your code

  • You have to check user input using == operator instead of = operator.

  • You are comparing user input with " " i.e. blank character which'll be true only when user add one space character in input field.

NOTE : This condition will be executed every time you visit the page.

So, you have to check if user pressed submit button and then only you have to display error message.

Hope it'll work.

Virus
  • 70
  • 4
0

1.change the "name" attribute with "Name" in "text" field
2.add "value" attribute in submit field where value="submit" and change name="Form" in the same
3.Now in your php code check whether form has been submitted if it is submitted then just check whether there is empty name or not

Name:<input type="text" name="Name"/>
     <input type="submit" name="Form" value="submit"/>
<?php
     if($_POST["Form"])
     {
        if ($_POST["Name"]) {
            echo $_POST["Name"];
         }
        else {
             echo "Please Enter Your Name!";   
         }
     }
 ?>