Dear Sir,
I have modified your codes once again as

  <?php
  if (isset($_POST['button1'])){
  $name=$_POST['text1'];
  }else{
  $name="";     
  }

  if ($name) {
  echo 'My name is ' . $name;
 }else{
 echo 'No name entered';
 }
 ?>

 <html>
   <head>
     <title>
       First form
     </title>
   </head>

   <body>

     <form name="aa" method="POST">
       Name
       <input type="text" name="text1" value="">
       <input  type="submit" value="Display" name="button1">
     </form>

   </body>

 </html>

It has no error, I need little modification again.
When I load page it says:

No name entered

When I press Display button then it must run following codes

<?php
if (isset($_POST['button1']))&& !empty($_POST['text1']){
    $name=$_POST['text'];
if ($name) {
echo 'My name is ' . $name;
}else{
echo 'No name was Entered';
}
}
?>

Why it runs above codes without pressing Display Button?

The code in your page

<?php
  if (isset($_POST['button1'])){
  $name=$_POST['text1'];
  }else{
  $name="";     
  }
  if ($name) {
  echo 'My name is ' . $name;
 }else{
 echo 'No name entered';
 }
 ?>

is different to the code that you have posted below and incorrect to only run the echo if the form has been submitted, change it to the correct code

<?php
    if (isset($_POST['button1']))&& !empty($_POST['text1']){
        $name=$_POST['text'];
    if ($name) {
    echo 'My name is ' . $name;
    }else{
    echo 'No name was Entered';
    }
    }
    ?>

Notice the if else statement above is warpped inside the if stement to check if any data has been posted

Sir on this line

if (isset($_POST['button1']))&& !empty($_POST['text1']){

it says:
Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND) in C:\wamp\www\Test\jiml.php on line 2

how to get rid of it?

It should be if (isset($_POST['button1']) && !empty($_POST['text1'])) {

Thanks now it works as i wish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.