0

I am writing within the functions.php file, and am trying to display HTML code, along with back-end php code. My problem is using the echo statement. It is rather difficult to use the echo function, as by putting things in ' ' gets rid of color coding. It also makes it more difficult to work with databases. I am able to display my code, but none of the php code seems to work. If I click on the submit button, nothing happens, no error. It's as if it is basic HTML. Is there any alternative to echo? If not, how does one work with php back-end code with echo?

EDIT: Everything shown below until the ending tag is encapsulated by ' ' due to echo

My first lines:

function add_content2() {
    echo '

<!-- PHP CODE -->
<?php

$error = "";

// CONNECTS TO DATABASE
$dbtype = mysqli_connect("localhost", "JohnLyons", "2012Zombies123!", "b1s243006078611");

if(!$dbtype) {
die(mysqli_error($dbtype));
}

// WHAT HAPPENS ON SUBMIT
if(isset($_POST["submit"])) {
    $name = $_POST["name"];
    if(empty($name)) {
        wc_add_notice(("Fill in the entire field"), "error");
    } else {
    // Queries database. How to get header to stay on page
        mysqli_query($dbtype, "INSERT INTO todo(name) VALUES ("$name")");
        $referer = $_SERVER["HTTP_REFERER"]; 
        header("location: $referer");
    }
}

$name_loop = mysqli_query($db, "SELECT * FROM todo");

?>

The table itsef:

<tbody>
        <!-- LOOPS THROUGH ARRAY -->
            <?php while ($row = mysqli_fetch_array($name_loop)) { ?>

            <tr>
                <td> <?php echo $row["name"]; ?> </td>
            </tr>
            
            <?php } ?>
            
        </tbody>

Finally, the submit button:

<form style="padding-top: 30px" method= "<?php echo $_SERVER["PHP_SELF"]; ?>" >
        <?php if (isset($error)) { ?>
            <p><?php echo $error; ?></p>
        <?php } ?>
        <input type="text" placeholder="Enter student information" name="task">
        <button type="submit" name="submit" style="margin-top: 10px">Register Student</button>
    </form>
1
  • 3
    why are you putting all your database code inside an echo statement with a big ''?
    – Tom J Nowell
    Commented Oct 11, 2022 at 9:04

1 Answer 1

0

I can't speak to how the above code is actually implemented, but the very first part is a misuse of "echo." Everything under "" should be outside of of it.

How exactly you handle the rest of the code (and whether it makes sense) is impossible for me to say, but you might see something like (I'd probably not write it this way, but whatever...):

function add_content2() {   

    // CONNECTS TO DATABASE
    $dbtype = mysqli_connect("localhost", "JohnLyons", "2012Zombies123!", "b1s243006078611");

    if( ! $dbtype ) {
    die(mysqli_error($dbtype));
    }

    // WHAT HAPPENS ON SUBMIT
    if(isset($_POST["submit"])) {
        $name = $_POST["name"];
        if(empty($name)) {
            wc_add_notice(("Fill in the entire field"), "error");
        } else {
        // Queries database. How to get header to stay on page
            mysqli_query($dbtype, "INSERT INTO todo(name) VALUES ("$name")");
            $referer = $_SERVER["HTTP_REFERER"]; 
            header("location: $referer");
        }
    }

    $name_loop = mysqli_query($db, "SELECT * FROM todo"); 

    //END FIRST LINES - maybe something more/else goes here or even is in a separate function or file?   

    echo '<tbody>' ;

    while ($row = mysqli_fetch_array($name_loop)) { 

        echo '<tr>
                <td>' . $row["name"] . '</td>
            </tr>' ;
                
    }   
    
    echo '</tbody> ' 

    echo '<form style="padding-top: 30px" method= "' . $_SERVER["PHP_SELF"]; . ' >' ;

    if (isset($error)) { 
        echo '<p>' . $error . '</p>' ;  
    } 

    echo '<input type="text" placeholder="Enter student information" name="task">' ;

    echo '<button type="submit" name="submit" style="margin-top: 10px">Register Student</button>' ;

    echo '</form>' ;
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.