Fajar Merah Diwangkaraa Fajar Merah Diwangkaraa - 3 months ago 6
HTML Question

php html div set POST variable in click

hello i want to set a $_POST value in php when someone click a div element , here's part of my code :

<?php
$looperone = 1;
$judulcountquery = "SELECT nomorsoal FROM soal";
$judulcountrun = mysqli_query($konek,$judulcountquery);
if ( $judulcountrun == FALSE ) {
die(mysqli_error());
} else
{}
$judulcount = mysqli_num_rows($judulcountrun);
$amountofrows = $judulcount;
while($looperone == 1) {
$judulherequery = "SELECT * FROM soal WHERE nomorsoal = $amountofrows";
$judulsatuqueryconnect = mysqli_query($konek,$judulherequery);
$judulsatufetch = mysqli_fetch_assoc($judulsatuqueryconnect);
if($judulsatufetch != NULL) {
$judulsatu = $judulsatufetch['judul'];
} else
{
break;
}
$judulnya = $judulsatufetch['judul'];
echo "<div class=\"head-main-recenttest-result\" style=\"text- decoration:none;\">". "<a href=\"bacasoal.php\">".$amountofrows.".".$judulnya." </a></div>";
$looperone++;
$amountofrows--;
}
?>


example of output from echo :



<div class="head-main-recenttest-result" style="text-decoration:none;">
<a href="bacasoal.php">Latihan 1</a></div>





as you see that only will redirect people to the link without giving post data , anyone know the method ? also im new so i can't ask well

Answer

By the looks of your code, you are not sending any post variable to the bacasoal.php. If you just want to submit then this will suffice.

<form method="post" action="bacasoal.php">
    <div class="head-main-recenttest-result" style="text-decoration:none;">
       <input type="submit" name="button" value="Latihan 1">
     </div>
</form>

Note: Your <a> tag was replaced by a an submit input

And if you want to do it via AJAX when click on the <a> then

HTML

<div class="head-main-recenttest-result" style="text-decoration:none;">
     <a href="#">Latihan 1</a>
</div>

AJAX

$('a').click(function(){
    $.ajax({
        url: "bacasoal.php",
        type: "post",
        success:(unction(){
            alert("success");
        },
        error:function(){
            alert("error");
        }
    })
});

In the first approach the page will be redirected to bacasoal.php. In second one the page will stay(unless you make it to).

Comments