functions – Alternative to php echo within code

Question

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>

0
John Lyons 2 months 2022-10-10T14:41:44-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse