简体   繁体   中英

Displaying data with php depending on what is selected in select option

Is it possible to display data of the current selected option, from mysql using php only?. So my select option works fine and it displays good results. But I want 2 other form-group divs to display data depending on what is currently selected in select option . Is that possible using php only or is javascript necessary?

Html

<div class="form-group">
    <label for="naslov">Naslov</label>
    <input type="text" class="form-control" id="naslov" name="naslov" value="<?php echo $titedit; ?>">
</div>

<div class="form-group">
    <label for="oglas">Sadržaj</label>
    <textarea class="form-control" id="oglas" name="oglas" rows="5" ><?php echo $contedit; ?></textarea>
</div>

PHP

<?php
session_start();
include_once("db.php");

$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
$title = $row['title'];
$mxnr = mysqli_num_rows('$query');


if ($mxnr > 0) {

    echo '<select class="form-control">';

    $counter = 1;

    while($row = mysqli_fetch_array($query)) {


        if($counter == 1){
            echo '<option ' . ($counter == 1 ? 'selected' : '') . ' value="opt' . $counter . '">' . $row["id"] . '. ' . $row["title"] . '</option>';
            $titedit = $row['title'];
            $contedit = $row['content'];

        }else{
            echo '<option value="opt' . $counter . '">' . $row["id"] . '. ' . $row["title"] . '</option>';
        }

        $counter++;

    }

    echo '</select>';

} else {
    echo "0 results";
}
?>

You will need javascript for that, since PHP loads before everything else, you can use JQuery to change the divs when you select something making it interactive and responsive.

If i may add my opinion, rely on javascript, its really usefull most of the time and will help you a lot. Consider JQuery.

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