Jim Goodley
Jim Goodley

Reputation: 11

php isset always returns false

I admit I am a newbie but I spent 2 days trying to get the following code to echo 'pressed or not pressed' when pressing the submit button. I stripped all irrelevant code and am left with below. This is really basic stuff and every forum I read suggested my code is correct. I am just using a laptop with windows 10 and IE connected to a WAMP server.

<!doctype html>
<html>
<head>
<meta charset="iso-8859-1">
<title>SystemAdministrator</title>
<link href="pval.css" rel="stylesheet">
<link href="system_administrator.css" rel="stylesheet">

<?php
//test to see if submit button was pressed or not
if (isset($_post['create_userdb'])) {
   echo 'pressed';
   $username = $_POST['username'];
   echo $username;
} else {
   echo 'not pressed';
   $username = $_POST['username'];
   echo $username;
}
?>

</head>
<body>
<Form name ="form1" Method ="POST" Action ="system_administrator.php">
<INPUT TYPE = "TEXT" VALUE ="username" Name ="username">
<INPUT TYPE = "Submit" Name = "create_userdb" VALUE = "create_userdb">
</FORM>
</body>
</html>

Upvotes: 1

Views: 1254

Answers (3)

Realit&#228;tsverlust
Realit&#228;tsverlust

Reputation: 3953

  1. $_post is wrong. You need $_POST

  2. The submit-button doesn't return a value to PHP. If you use var_dump($_POST), you can see all the posted data from the form, and create_userdb won't be there.

You can simply check for the $_POST array like this if you want to check if the button was pressed:

<?php
//test to see if submit button was pressed or not
if (isset($_POST)) {
   echo 'pressed';
   $username = $_POST['username'];
   echo $username;
} else {
   echo 'not pressed';
   $username = $_POST['username'];
   echo $username;
}
?>

Edit: So in my case, it didn't return anything. PHP 5.3 installed.

test.html

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <form method="POST" enctype="multipart/form-data" action="test.php">
    <input type="text" value="username" name="username">
    <input type="submit" name="create_userdb" value="create_userdb">
  </form>
</body>
</html>

Test.php

<?php
var_dump($_POST);

Output:

array (size=1)
'username' => string 'username' (length=8)

Upvotes: -1

Professor Abronsius
Professor Abronsius

Reputation: 33813

The problem was the lowercase $_post - it should be as below $_POST

<!doctype html>
<html>
    <head>
        <meta charset="iso-8859-1">
        <title>SystemAdministrator</title>
        <link href="pval.css" rel="stylesheet">
        <link href="system_administrator.css" rel="stylesheet">
        <?php
        if( $_SERVER['REQUEST_METHOD']=='POST' ){
            if ( isset( $_POST['create_userdb'])) {
               echo 'pressed';
               $username = $_POST['username'];
               echo $username;
            } else {
               echo 'not pressed';
               $username = $_POST['username'];
               echo $username;
            }
        }
        ?>
    </head>
    <body>
        <Form name="form1" Method ="POST">
            <INPUT TYPE="TEXT" VALUE="username" Name="username" />
            <INPUT TYPE="Submit" Name="create_userdb" VALUE="create_userdb" />
        </FORM>
    </body>
</html>

Upvotes: -1

Manish Shukla
Manish Shukla

Reputation: 1365

there is typing mistake of $_post. It should be $_POST

if (isset($_POST['create_userdb'])) {
   echo 'pressed';
   $username = $_POST['username'];
   echo $username;
} else {
   echo 'not pressed';
   $username = $_POST['username'];
   echo $username;
}

Upvotes: 3

Related Questions