Not Displaying Anything

Hi,

<?php
if(isset($_POST['submit']) {
if($_GET['lang'] == "english") {
echo("First name: " . $_POST['firstname'] . "<br />\n");
echo("Last name: " . $_POST['lastname'] . "<br />\n");
} else if($_GET['lang'] == "spanish") {
echo("Nombre: " . $_POST['firstname'] . "<br />\n");
echo("Apellido: " . $_POST['lastname'] . "<br />\n");
}

?>

<form method="post">
<p>First name: <input type="text" name="firstname" /></p>
<p>Last name: <input type="text" name="firstname" /></p>
<input type="submit" name="submit" value="Submit" />
</form>

Its not displaying anything. Some body please guide me what is the problem with this code?

Zulfi.

Hi @Zulfi6000 and a warm welcome to the forum.

There is a missing closing bracket and the form input firstname is being called twice:

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');


if( isset($_POST['submit']) ) 
{
  if($_GET['lang'] == "english")
  {
    echo("First name: " . $_POST['firstname'] . "<br />\n");
    echo("Last name: " . $_POST['lastname'] . "<br />\n");

  } elseif( $_GET['lang'] == "spanish" ) {
    echo("Nombre: " . $_POST['firstname'] . "<br />\n");
    echo("Apellido: " . $_POST['lastname'] . "<br />\n");
  }
} // MISSING CLOSING BRACKET

?>
<!doctyoe html>
<html>
<head>
<title> Just testing </title>
</head>
<body>
  <h1> just testing </h1>

  <form method="post">
    <p>
    First name: 
    <input type="text" name="firstname" />
   </p>
    <p>
    Last name: 
     <input type="text" name="firstname" />
    </p>
    <input type="submit" name="submit" value="Submit" />
  </form>

</body>

</html>

Edit:
Also the doctype is spelt incorrectly.

Validation:
https://validator.w3.org/nu/#textarea

3 Likes

A couple other things.

  1. echo does not need parenthesis (). It just clutters the code base
  2. You can eliminate 50% of the redundant code by using a ternary to determine the text to display.
  3. You need to check the SERVER REQUEST method. Checking for the name of a button will completely fail in certain cases.
  4. Use HTML5
  5. NEVER EVER output user supplied data. You need to use htmlspecialchars on your output. Your code is vulnerable to an XSS Attack.
  6. You can remove the name attribute from the submit button, thus shortening the code even further.
2 Likes

Notice : Undefined index: lang in /var/www/html/form5_2.php on line 9

Notice : Undefined index: lang in /var/www/html/form5_2.php on line 14

Try this instead:

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');


//========================================================
  function debug($val='not set')
  {
  echo '<pre>DEBUG: '; // add linefeeds
    print_r($val);
    // var_dump($val);
  echo '</pre>';
  }//

//========================================================
  function getPostValue($val)
  :string 
  {
   $result  = isset($_POST["$val"]) && ! empty($_POST["$val"])  
            ? $_POST["$val"]
            : 'Not set???'
            ;
  #echo '<br> $result ==> '.$val . ' --- ' .$result .'<br>';
    return $result;
  }


// CHECK GET and POST DEFAULTS
  $id     = isset($_GET['id']) ? $_GET['id'] : NULL; 

  $lang   = getPostValue ('lang');  
  $first  = getPostValue ('firstname');  
  $first  = getPostValue ('firstname');  
  $last   = getPostValue ('lastname');  
  $submit = isset($_POST['submit']) ? $_POST['submit'] : NULL; 


  $msgs = '';
  if( $submit ) // isset($_POST['submit']) ) 
  {
    $msgs = '<table>';
      $msgs .= '<tr> <td>Language: </td><td>' . $lang .'</td> </tr>';

      if($lang === "english")
      {
        $msgs .= "<tr> <td> First name: </td><td>" . $first . "</td> </tr>";
        $msgs .= "<tr> <td> Last name:  </td><td>" . $last . "</td> </tr>";
      } elseif( $lang === "spanish" ) {
        $msgs .= "<tr> <td> Nombre:   </td><td>"  . $first . "</td> </tr>";
        $msgs .= "<tr> <td> Apellido: </td><td>"  . $last  . "</td> </tr>";
      }
      else {
        $msgs .= "<tr> <td> Nombre:   </td><td>"  . $first . "</td> </tr>";
        $msgs .= "<tr> <td> Apellido: </td><td>"  . $last  . "</td> </tr>";
      }
      $msgs .= '<tr> <td> <br> id: </td><td>' . $id .'</td> </tr>';
    $msgs .= '</table>';
  } // MISSING CLOSING BRACKET

// CHECK GET
  if ($id) 
  {
    $sql = " SELECT *
             FROM links 
             WHERE id='$id' 
             LIMIT 1"; 
             // this area is where i get the error
    if( isset($link) ) 
    {
      $result = mysqli_query($link, $sql);
      if( $result )    
      {
        while($list = mysqli_fetch_assoc($result))
        {
          // echo '<br>' .__line__;
        }//endwhile
      } else {
        $msgs .= '<br> Yes we have no $result ???';
      }
    } else {
      $msgs .= '<br> Yes we have no $link ???';
    }
  } else {
    $msgs .= '<br> Yes we have no $link ???';
}//endif  


?><!doctype html>
<html>
<head>
<title> Just testing </title>
<style>
  .fll {float: left;} 
  .flr {float: right;}
  .ftr {position: fixed; left: 0; bottom: 0;}
  .w99 {width: 99%;}
</style>
</head>
<body>

  <h1> just testing </h1>

  <form action = "" method = "post">
    <p> 
      <label> Language: </label>
      <input type="radio" name="lang" value="english" /> English
      <input type="radio" name="lang" value="spanish" /> Spanish
      <input type="radio" name="lang" value="gibberish" /> No language
    </p>
    <p>
      <label> First name: </label>
      <input type="text" name="firstname"  /> 
    </p>
    <p>
      <label> Last name: </label>
      <input type="text" name="lastname"  />  
    </p>
    <p>
      <input type="submit" name="submit" value="Submit"  />
    </p>
  </form>

  <hr>
    <dl> <dt> <b> DEBUG: </b> $msgs </dt>
      <dd>
        <?= $msgs ?>
      </dd>
    </dl>    
  <hr>  

  <?php debug($_POST); ?>

<!--
  <div class="ftr w99">
    <hr>
    <a class="fll" href="#"> HTML validation </a> 
    <a class="flr" href="#"> CSS validation </a> 
  </div>  
-->
</body>
</html>

Edit:*
Remmed unnecessary footer.

Not sure how this got so complicated. How did Mysql get into this? This is quite simple.

Assuming that the GET lang parameter is set somewhere…
filename.php?lang=spanish OR no lang set filename.php


<?php declare (strict_types = 1);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $first = 'First Name';
    $last = 'Last Name';

    if (isset($_GET['lang']) == "spanish") {
        $first = 'Nombre';
        $last = 'Apellido';
    }

    echo "$first: {$_POST['firstname']}<br>\n$last:{$_POST['lastname']}";
}
?>
<form method="post">
<p>First name: <input type="text" name="firstname"></p>
<p>Last name: <input type="text" name="lastname"></p>
<input type="submit" value="Submit">
</form>

Important Point: You never want to directly output user supplied data as shown. Run the output through htmlspecialchars.

* Code is based on the OP. I have no idea what he is actually up to.

1 Like