简体   繁体   中英

Won't read variable in function in another script

Whenever I leave my input field empty, $error['commment'] should be set and echoed, but it won't echo, but if I just say echo "some text"; , it echo's it. The comments function is in my functions.php file and $error[] = array() is given in my text.php file above my comments() function, so I don't understand why it's not working, please help guys.

The last bit of PHP code is in a while loop that has to display all the results of my SQL query.

Code above my HTML in text.php:

<?php

session_start();

include("connect.php");
include("functions.php");

$userId = "";

if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn']){
    $userId = $_SESSION['id'];
}


$error[] = array();

comments();
?>

Code in my functions.php:

function comments(){
if (isset($_POST['submit'])) {
    $text = $_POST['text'];
    $filledIn = true;

    if (empty($text)) {
        $error['comment'] = "No text filled in";
        $filledIn = false;
    }

}

}

This is the code in my text.php:

<?php
if(isset($error['comment'])) echo "<p class='error'>".$error['comment']."</p>";
?>

Because $error is not in the scope of the comments() function. So $error['comment'] never gets set.

Ideally you would want to do something like this:

text.php

session_start();

include("connect.php");
include("functions.php");

$userId = "";

if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn']){
    $userId = $_SESSION['id'];
}

$error['comment'] = comments();

functions.php

function comments(){
    if (isset($_POST['submit'])) {
        $text = $_POST['text'];    
        if (empty($text)) {
            return "No text filled in";
        }
    }
    return null;
}

text.php

<?php
if(!empty($error['comment'])) echo "<p class='error'>".$error['comment']."</p>";
?>

Rather than setting the array key "comments" directly I would use a return value from the comments() function to set it. This allows you to avoid having to use global variables.

Edit : I removed the $filledIn variable from comments() because it wasn't being used in the provided code.

@pu4cu

imo, since you dont come across as very advanced, so that you dont have to make many code changes to what you have now which might get you the minimal edits, and easiest for you to understand;

if in your comment function, you just return a response from this function, like a good little function does, then your response will be available when you call the function, when you set that function to a variable.

//functions.php (note this sets error to true to be failsafe, but this depends on how your using it. set the $response to an empty array in the first line instgad, ie array(); if you don't want it failsafe.

<?php
function comments()
{
    $response = array(
            'error' => TRUE,
             'filledIn' => FALSE
    );

    if (isset($_POST['submit']))
    {
        $text = $_POST['text'];
        $response['filledIn'] = TRUE;

        if (empty($text)) 
        {
            $response['error']['comment'] = "No text filled in";
        } 
        else{
            $response['error'] = NULL;
        }
    }
    return $response;
}

//index.php

session_start();

include("connect.php");
include("functions.php");

$userId = "";

if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn']){
    $userId = $_SESSION['id'];
}

$response = comments();

//text.php

<?php
if($response['error']['comment'])) echo "<p class='error'>".$response['error']['comment']."</p>";

I find your code overly complicated, 3 files, 2 includes, and 1 function, when all you really needed is this:

$error = array();
$error['comment'] = empty($_POST['text']) ? "No text filled in" : $_POST['text'];
echo "<p class='error'>".$error['comment']."</p>";

Your scopes are all mixed up. Your comments() function checks for $_POST, which should be checked before the function is called, and then tries to set a variable within its scope, but you try to access the same variable from outside.

The correct way would be:

text.php:

<?php

     session_start();

     include("connect.php");
     include("functions.php");

     $userId = "";

     if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn']){
         $userId = $_SESSION['id'];
     }


     $error[] = array();
     if (isset($_POST['submit']) {
        comments($_POST);
     }

?>

functions.php

function comments($data){
    if (isset($data['text'])) {
        $text = $data['text'];    
        if (empty($text)) {
            return array('comment' => 'No text filled in');
        }
        return true;
    }
    return null;
}

Then you can use the values returned by your function on to act on the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM