Fajar Merah Diwangkaraa Fajar Merah Diwangkaraa - 1 year ago 39
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 :

$looperone = 1;
$judulcountquery = "SELECT nomorsoal FROM soal";
$judulcountrun = mysqli_query($konek,$judulcountquery);
if ( $judulcountrun == FALSE ) {
} 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
$judulnya = $judulsatufetch['judul'];
echo "<div class=\"head-main-recenttest-result\" style=\"text- decoration:none;\">". "<a href=\"bacasoal.php\">".$amountofrows.".".$judulnya." </a></div>";

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 Source

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">

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


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


        url: "bacasoal.php",
        type: "post",

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