-1

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 ''? Commented Oct 11, 2022 at 9:04

2 Answers 2

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>' ;
    
}
0

PHP has the <?= shortcut that I find a bit more legible, especially when used in conjunction with PHP Braceless syntax.

/* First let's exit PHP mode */ 
?>
<tbody>
  <?php while ($row = mysqli_fetch_array($name_loop)): ?>
  <tr>
    <td><?= $row["name"] ?></td>
  </tr>
  <?php endwhile ?>          
</tbody>

<form style="padding-top: 30px" method= "<?= $_SERVER["PHP_SELF"] ?>" >
  <?php if (isset($error)): ?>
   <p><?= $error ?></p>
  <?php endif ?>
  <input type="text" placeholder="Enter student information" name="task">
  <button type="submit" name="submit" style="margin-top: 10px">Register Student</button>
</form>

<?php /* this returns us to php mode. */

Personally, I go a step further and use the Timber library which bridges into the Twig template engine. This is what the code above looks like in Twig

<tbody>
  {% for row in rows %}
  <tr>
    <td>{{ row.name }}</td>
  </tr>
  {% endfor %}          
</tbody>

<form style="padding-top: 30px" method= "<?= $_SERVER["PHP_SELF"] ?>" >
  {% if error %>
   <p>{{ error }}</p>
  {% endif %>
  <input type="text" placeholder="Enter student information" name="task">
  <button type="submit" name="submit" style="margin-top: 10px">Register Student</button>
</form>

Of course, to use the template, you have to load your data into it which is beyond the scope of this response. If the above looks interesting the project guide is here: https://timber.github.io/docs/v2/

P.S. There is no reason to use the mysqli functions directly in a WordPress application. I strongly advise you use the $wpdb connector object or, even better, the WP_Query class to work with the database.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.