Php code not displaying anything

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
zak100
New php-forum User
New php-forum User
Posts: 9
Joined: Fri May 24, 2019 1:36 pm

Sun Sep 01, 2019 4:10 pm

Hi,
I got a following code from the internet but when i execute it, it displays nothing. Its form5.php

Code: Select all


<?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>

Somebody please guide me what is the problem with above code.

Zulfi.

chorn
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Fri Apr 01, 2016 2:18 am

Sun Sep 01, 2019 11:05 pm

then you are missing the error reporting

Code: Select all

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

zak100
New php-forum User
New php-forum User
Posts: 9
Joined: Fri May 24, 2019 1:36 pm

Mon Sep 02, 2019 8:39 pm

Hi,

Do you mean I have to append this code in the PhP file? Please guide me I can't understand. I am a starter.

Zulfi.

chorn
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Fri Apr 01, 2016 2:18 am

Mon Sep 02, 2019 11:28 pm

Just prepend that to the very beginning of the very first file called after <?php

zak100
New php-forum User
New php-forum User
Posts: 9
Joined: Fri May 24, 2019 1:36 pm

Wed Sep 04, 2019 9:00 pm

Hi,
I changed the code to the following:

Code: Select all

 
<?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");
  }
} 
?>

<!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>
 
I am getting following error message:
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

Somebody please guide me.

Zulfi.

chorn
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Fri Apr 01, 2016 2:18 am

Wed Sep 04, 2019 10:38 pm

So you changed the code and fixed the first error and get a new one now.

That's because you are accessing an array key that does not exist, you always have to check if an array key exists, just like you did with $_POST['submit'], either by isset() or empty(), depending on your requirement.

Post Reply