Lunyx
Lunyx

Reputation: 51

MySQL on PHP need 2 reloads to update the values

I made a message deleter button, but I need 2 reloads to appear the changes... (The rest of the code work, so it's normal that I don't show you the rest of the code...)

            <?php while($r = $replies->fetch()) { ?>
        <div class="message" id="<?= $r['id'] ?>">
            <div class="profile">
                <img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
                <h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
            </div>
            <div class="content">
                <div class="date">
                    <?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
                </div>
                <br><br>
                <?= htmlspecialchars_decode($r['content']) ?>
                <form method="POST"><button name="delete<?= $r['id'] ?>">Test</button></form>
                <?php
                    $test = "delete".$r['id'];

                    if(isset($_POST[$test])) { 
                        $delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
                        $delete->execute(array($r['id']));
                        $success = "Your message was successfully removed !";
                    }
                ?>
            </div>
        </div>
        <br>
        <?php } ?>

UPDATE:

I added the deleting code at the top of my php code, and it's working, thanks to Ray Andison

By the way thanks to keidakida too; he helped me to find a solution to my value problem. (And I think he don't know that)

Upvotes: 0

Views: 92

Answers (3)

keidakida
keidakida

Reputation: 741

Since it would be better and easier with AJAX, this is how it goes:
main.php

<?php
while ($r = $replies->fetch()) { ?>
    <div class="message" id="<?= $r['id'] ?>">
        <?php echo htmlspecialchars_decode($r['content']) ?>
        <button onclick="delete('<?php echo $r['id']; ?>')">Delete</button>
    </div>
    <br>
<?php } ?>
<script>
    function delete(id) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                alert("Delete successfully");
                location.reload();
            }
        };
        xmlhttp.open("POST", "delete.php", true);
        // Mandatory for simple POST request
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // Send id you want to delete
        xmlhttp.send("id=" + id);
    }
</script>

And make another PHP file name is delete.php like this:

<?php

include 'YOUR_DB_Connect.php';

if(isset($_POST["delete"])) { 
    $delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
    $delete->execute(array($_POST['delete']));
}

?>

Upvotes: 0

keidakida
keidakida

Reputation: 741

It is because the delete PHP code is at the bottom. Actions such as delete should be at the top of the HTML or while loops before presenting the data. SO try this:

<?php
    if(isset($_POST["delete"])) { 
        $delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
        $delete->execute(array($_POST['delete']));
        $success = "Your message was successfully removed !";
    }

    while($r = $replies->fetch()) { ?>
        <div class="message" id="<?= $r['id'] ?>">
            <div class="profile">
                <img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
                <h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
            </div>
            <div class="content">
                <div class="date">
                    <?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
                </div>
                <br><br>
                <?= htmlspecialchars_decode($r['content']) ?>
                <form method="POST">
                    <button type="button" name="delete" value="<?php echo $r['id']; ?>">Test</button>
                </form>
            </div>
        </div>
        <br>
    <?php 
    }
?>

But you can do the same functionality without any page reload. Check AJAX PHP

Upvotes: 1

Ray Andison
Ray Andison

Reputation: 284

Your form doesn't contain any data (the id to be deleted) or action (page to submit data to)?

<form method="POST" action="thispage.php">
<input id="test" name="test" type="hidden" value="<?= $r['id'] ?>">
<input type="submit">
</form>

UPDATED:

<?
  if(isset($_POST[id])) { 
    $delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
    $delete->execute(array($_POST[id]));
    $success = "Your message was successfully removed !";
  }

while($r = $replies->fetch()){
echo '
<div class="message" id="'.$r[id].'">
  <div class="profile">
    <img class="avatar" src="members/avatars/';

    if(empty(get_avatar($r[id_author]))){
      echo "default.png";
    }else{
      echo get_avatar($r[id_author]);
    }

echo '
    " width="150" height="150">
    <h3 style="text-align:center;">
      '.get_username($r[id_author]).'
    </h3>
  </div>
  <div class="content">
    <div class="date">
    '.date('d F Y - g:iA', strtotime($r[date_hour_post])).'
    </div>
    <br>
    <br>
    '.htmlspecialchars_decode($r[content]).'

    <form method="POST" action="thispage.php">
      <input id="id" name="id" type="hidden" value="'.$r[id].'">
      <input type="submit">
    </form>
  </div>
</div>';
}
?>

This is how I would code this, you need to change the action="thispage.php" to be the name of itself so it posts to itself, replace with the actual name of your php file

Upvotes: 1

Related Questions