duanne9313
2019-04-15 04:44
浏览 84
已采纳

无法从PHP函数回显或打印

I am having an issue echoing or printing from my PHP to my HTML. I am sure I am missing something basic with how PHP works, however I have been unable to find anything about this issue on the internet or on here. I am very confused, because I am not receiving an errors or any context to the problem at all. The rest of my code works fine, except for echo.

I normally use echo() for testing to ensure stuff exists, however this issue has plagued me during the entire development of this application. It only now bothers me because I will need to echo out a script to load in data (Which I will hide after the load using Vue).

This issue oddly enough only has happened inside of areas where I am using if to do things, especially when it has array_key_exists.

If anyone could help I would appreciate it (Or if someone could provide a better idea for me to transfer json data from PHP to JavaScript other than having to echo it out, since that opens a weak spot in my code for users to cheat my clicker game).

I have tried to test this in other parts of my code with the same results. I cannot use echo() or print() within any if statement. I can use it elsewhere however.

All of my PHP files end up inside of my index.php by usage of require

<?php
    if (array_key_exists("saveData", $_POST)) {
        $saveData = $_POST['saveData'];
        $token = $_COOKIE['validToken'];
        $token = mysqli_real_escape_string($link, $token);
        $query = "UPDATE userData SET saveData = '$saveData' WHERE token = '$token' LIMIT 1";
        mysqli_query($link, $query);
    }
    if (array_key_exists("logSubmit", $_POST)) {
        $email = $_POST["logEmail"];
        $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Clean Email
        $email = filter_var($email, FILTER_VALIDATE_EMAIL); // Confirm Valid Email
        $query = "SELECT saveData FROM userData WHERE email = '$email' LIMIT 1";
        $loadData = mysqli_query($link, $query);
        echo("SALO Ready!");
    }
?>

I would expect for this to echo out "SALO Ready" however I get nothing instead.

EDIT: PHP's white screen of death does not work or apply. Everything else in my application works as expected

UPDATE #1: I have done some testing as recommended, and found that when I echo out of my if statements, one of two things happen

1) The echo() fires, and in the preview under Network on Chrome you can see it their however it does not display in the DOM. This is the case for any if statement that does not meet the below situation.

2) If the echo() is fired from a block used for registering or logging in, it will not display in the preview either. I will be restructuring my code to have those be functions called by the forms when submitted, instead of them just being conditional blocks. While this code is not included in this question, the initial login runs the code below as well to load up user data.

UPDATE #2: I have consolidated my code and followed some recommendations. My code is now inside of functions, that are fired by some if isset conditions. This actually PARTLY fixed my issue. I can echo from all of my functions (Register, Login, Save, Load). Now my issue would appear to be a comment below in relations to output buffering. While my initial question is (Mostly) solved, would anyone be able to help explain how to solve this? I only have ob_start() right now, followed by my functions, if isset conditions, and then everything else in my application. How can I get the echo go to them DOM? I will include the chunk of my code below that will absolutely need to echo out down the road.

EDIT: I have changed how I have ob_start() setup within my code. I call it in the functions that need it and then flush it afterwards. I will be testing with my load script later tonight to try to force the echo() out of the function. Genuinely confused as to why it had not worked in functions but works outside of them, even before this edit

function load() {
    $link = // Link Excluded for Security ;
    if (mysqli_connect_error()) {
        die ("DB Connection Error");
    }
    $email = $_POST["logEmail"];
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Clean Email
    $email = filter_var($email, FILTER_VALIDATE_EMAIL); // Confirm Valid Email
    $query = "SELECT saveData FROM userData WHERE email = '$email' LIMIT 1";
    $loadData = mysqli_query($link, $query);
    mysqli_close($link);
    echo("Loading Triggered!");
}
if(isset($_POST['logSubmit'])) {
    login();
    load();
}

As mentioned, this shows the echo() inside of the network tab when I click on primary.php and click preview, it just isn't going to the DOM

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongyang4615 2019-04-15 04:49
    最佳回答

    Just use keep your code inside a function and call the function name while post your input

    Ex:

    function yourFunctionName(){
        if (array_key_exists("saveData", $_POST)) {
            $saveData = $_POST['saveData'];
            $token = $_COOKIE['validToken'];
            $token = mysqli_real_escape_string($link, $token);
            $query = "UPDATE userData SET saveData = '$saveData' WHERE token = '$token' LIMIT 1";
            mysqli_query($link, $query);
        }
        if (array_key_exists("logSubmit", $_POST)) {
            $email = $_POST["logEmail"];
            $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Clean Email
            $email = filter_var($email, FILTER_VALIDATE_EMAIL); // Confirm Valid Email
            $query = "SELECT saveData FROM userData WHERE email = '$email' LIMIT 1";
            $loadData = mysqli_query($link, $query);
            echo("SALO Ready!");
        }
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题