简体   繁体   中英

Comment section wont post comments

I have a problem in my code. When I post a comment aka when I hit the button, it wont show in the comment field before I refresh the site. Can somebody help me with that? It would be truly appreciated!

<?php
  $db = mysqli_connect("localhost", "root", "", "mydb");

if (mysqli_connect_errno()) {
die(mysqli_connect_error());
}

$query = "SELECT * FROM kommentar";

$resultat = mysqli_query($db,$query);
if (!$resultat) echo "<b>FEIL: ikke i stand til å sende.</b>";

$rows = array();

while ($row = mysqli_fetch_array($resultat, MYSQL_ASSOC)) {
$rows[] = $row;
}


$query2 = sprintf("select * from kommentar");

// Sender spørring til databasen og tester på om den gjekk OK 
$resultat2=mysqli_query($db, $query2);    
if (!$resultat2) echo "<b>FEIL: ikkje i stand til å senda.</b>";

// Initialisere rows2 som ein 'array'  
$rows2 = array();  
// Henter verdiar til rows1 frå database-svaret  
while ($row2 = mysqli_fetch_array($resultat2, MYSQL_ASSOC)) {  
    $rows2[] = $row2;    
}

if (isset($_GET['id']) && intval($_GET['id']) > 0) {

// Hentar id frå querystreng
$id = $_GET['id'];

// "Cast" id til integer, dvs. gjer om id til heiltal
$id = (int) $id;
}  
?>


 <!DOCTYPE html>
 <html>
 <head>
 <title>Kommentarfelt</title>
 </head>
 <body>
   <h3> Kommentarer: </h3> 
     <?php foreach($rows2 as $row2): ?>  
             <?php echo $row2['tekst']; ?> 
             Lagt inn <?php echo $row2['opprettet']; ?> av <?php echo
  $row2['navn']; ?><br>                                                   
     <?php endforeach; ?>
     <h4> Skriv ny kommentar: </h4>
     <form method="POST" action="test_envy.php?id=<?php echo $row1['id']; ?>">   
     <b>Navn:</b><br>   
<input type="navn" name="navn"><br>   
<b>Kommentar:</b><br>
<textarea cols="60" rows="6" name="tekst"></textarea><br>
<input type="submit" name="sendknapp" value="Send"></form> 
</body>
<?php  

/*if ($_POST["sendknapp"] == "Send")*/


if($_SERVER['REQUEST_METHOD'] == "POST")

{    
//mysql_connect("localhost","root",""); /* server, username, passord */
//mysql_select_db("mydb");   
$db = mysqli_connect("localhost","root","","mydb");     
$navn=$_POST["navn"];  
$tekst=$_POST["tekst"];    
$query ="INSERT INTO kommentar (navn, tekst)";    
$query.="VALUES ('$navn','$tekst')";    
$resultat=mysqli_query($db, $query);    
if ($resultat) {
printf("Kommentar registrert", mysqli_insert_id($db));
echo ("<a href='vg.no" . $id . "';> Oppdater side </a>");}

else printf("ikkje i stand til å senda query:%s", $query);;  
}    
?>

Until page reload, you need to temporarily add the comment via AJAX.

Something like this:

// Add this line in your form
<input type='hidden' id='ajaxURL' value='test_envy.php?id=<?php echo $row1['id'] ?>'>

// Add this to your HTML
<script>
$('input[name="sendknapp"]').click(function(e){

    e.preventDefault(); // Stops your page from reloading

    var ajaxurl = $('#ajaxURL').val();

    $.ajax({ url: ajaxurl, // The URL you're gonna pass to the script
         data: {action: 'test'}, // Variables you wanna pass to the function
         type: 'post',
         success: function(response) {
             // Get your variables from the response
             alert(response); // This will show you the response in an alert box
             // $('#elementID').append("<a href='" + variable + "'> Oppdater side </a>");
         }
    });
});
</script>

So this should popup a box with a response in it. You need to tweak from here on. Your PHP needs to echo the response and then you append it to your HTML.

You need to pass variables via data . It all depends on what your php script does... In this case test_envy.php.

One more essential thing I forgot to mention is you need to add this line to the head of your html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>

This is to load jQuery on your page... Without it, the script won't work.

Let me know if you need more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM