Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use function return value

I'm new to PHP and developing a login form. Please find below the code I used. When I tried it gave me the following error:

Fatal error: Can't use function return value in write context in C:\xampp\htdocs\forsiteSystem\login.php on line 3

Please help me to fix the issue.

Source code for thems/login.html:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action=".\login.php" method="get">
        Name: <input type="text" name="name"><br>
        E-mail: <input type="text" name="email"><br>
                <input type="submit"  id="Submit_button">
        </form>
    </body>
</html>

Source code for index.php:

<?php
        // venushka thisara dharmasiri

        require 'config.php';
        require 'thems\login.html';

 ?>

Source code for login.php:

<?php

  if(isset($_POST("Submit_button"))==true)  
      print("Submit button pressed");
  else
      print("submit button sorry");

?>

Source code for config.php:

<?php

$dbUser="root";
$dbPassword="";
$dbName="forsitelogin";
$dbHost="localHost";

$dbConnection= mysql_connect($dbHost, $dbUser, $dbPassword);

if($dbConnection)
{
    mysql_select_db($dbName);
    //print("Sucessfully connected to database");
}
else
    die("<strong>Cound not connect to database </strong> ");

?>
like image 798
VenushkaT Avatar asked Jan 05 '14 05:01

VenushkaT


2 Answers

Should be $_POST["Submit_button"] instead of $_POST("Submit_button")

like image 133
Shankar Narayana Damodaran Avatar answered Sep 19 '22 20:09

Shankar Narayana Damodaran


The error the script returns explains it:

Fatal error: Can't use function return value in write context in C:\xampp\htdocs\forsiteSystem\login.php on line 3

If you don’t understand the meaning of the error—and believe me, most error messages are bizarre even to experienced programmers—look at the line number referenced. And looking at line 3 in login.php shows me the error; $_POST("Submit_button") is invalid:

if(isset($_POST("Submit_button"))==true)  
    print("Submit button pressed");
else
    print("submit button sorry");

It should be $_POST["Submit_button"]:

if(isset($_POST["Submit_button"])==true)  
    print("Submit button pressed");
else
    print("submit button sorry");

But looking at it further, why is there an ==true? It can simply be like this:

if(isset($_POST["Submit_button"]))  
    print("Submit button pressed");
else
    print("submit button sorry");

But I would recommend doing a better check on that value like this:

if(array_key_exists("Submit_button", $_POST) && !empty(trim($_POST["Submit_button"])))  
    print("Submit button pressed");
else
    print("submit button sorry");

I find that using array_key_exists and a combination of !empty with trim works better for basic user submitted data verification.

like image 44
Giacomo1968 Avatar answered Sep 21 '22 20:09

Giacomo1968