0

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?

2
Contributors
4
Replies
16
Views
3 Years
Discussion Span
Last Post by vbdotnetlover
0

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

0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.