Tag,
ich möchte nach Absenden eines Buttons etwas in die Datenbank senden wobei die Seite nicht neu geladen werden soll!
Problem ist, die Seite ladet sich immernoch neu & nach dem Eintrag soll er ja zu div#result gehen. Da geht er auch nicht hin.
Spoiler anzeigen
PHP
<?php
if(isset($_POST['submit'])) {
$msg = '';
$pid = $_POST["pid"];
$comment = $_POST['comment'];
$uid = 2;
$sql = 'INSERT INTO comments (p_id, u_id, comment) VALUES (:p_id, :u_id, :comment)';
$stmts = $pdo->prepare($sql);
if(!$stmts) { echo $stmts->errorInfo(); }
$stmts->BindParam(':p_id', $pid);
$stmts->BindParam(':u_id', $uid);
$stmts->BindParam(':comment', $comment);
if(!$stmts->execute()) {
echo $stmts->errorInfo();
} else {
$msg = 'Eingefügt';
}
}
?>
<?php while($row = $stmt->fetch()) { ?>
<form action="world_two.php" id="form" method="POST">
<?php echo '<img src="data:image/' . $row['im_type'] . ';base64,' . base64_encode($row['image']) . '"/>'; ?>
<?php echo '' . $row['username'] . ''; ?>
<?php echo '<p>' . $row['date'] . '</p>'; ?>
<?php echo '<p><input type="hidden" name="pid" value="'.$row['p_id'].'"></p>'; ?>
<?php echo '<p><input type="text" name="comment"></p>'; ?>
<?php echo '<p><input type="submit" name="submit" id="submit"></p>'; ?>
</form>
<?php } ?>
<span id="result">
<?php if(isset($msg)) { echo $msg; } ?>
</span>
<script src="jQuery/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="js/global.js"></script>
Alles anzeigen
jQuery:
Spoiler anzeigen
Ebenso habe ich diese Methode probiert jedoch ohne Erfolg.
Spoiler anzeigen
PHP
<?php while($row = $stmt->fetch()) { ?>
<form action="test.php" id="form" method="POST">
<?php echo '<img src="data:image/' . $row['im_type'] . ';base64,' . base64_encode($row['image']) . '"/>'; ?>
<?php echo '' . $row['username'] . ''; ?>
<?php echo '<p>' . $row['date'] . '</p>'; ?>
<?php echo '<p><input type="hidden" name="pid" value="'.$row['p_id'].'"></p>'; ?>
<?php echo '<p><input type="text" name="comment"></p>'; ?>
<?php echo '<p><input type="submit" name="submit" id="submit"></p>'; ?>
<script>
$("#form").on("submit", function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "test.php",
data: $(this).serialize(),
success: function(data) {
$("#result").append(data);
},
});
});
</script>
</form>
<?php } ?>
<span id="result">
<?php if(isset($msg)) { echo $msg; } ?>
</span>
<script src="jQuery/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="js/global.js"></script>
Alles anzeigen
test.php
Spoiler anzeigen
PHP
<?php
include 'config/connect.php';
if(isset($_POST['submit'])) {
$msg = '';
$pid = $_POST["pid"];
$comment = $_POST['comment'];
$uid = 2;
$sql = 'INSERT INTO comments (p_id, u_id, comment) VALUES (:p_id, :u_id, :comment)';
$stmts = $pdo->prepare($sql);
if(!$stmts) { echo $stmts->errorInfo(); }
$stmts->BindParam(':p_id', $pid);
$stmts->BindParam(':u_id', $uid);
$stmts->BindParam(':comment', $comment);
if(!$stmts->execute()) {
echo $stmts->errorInfo();
} else {
$msg = 'Eingefügt';
}
}
?>
Alles anzeigen