How to keep a value in a select menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
john7911
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2015 6:21 am

How to keep a value in a select menu

Post by john7911 »

Hi,
I need your help for 2 problems, the first is, when I open the page (the link below) I have in the menu HEA100 but nothing is displayed, I have to clic in Afficher.
Image

The second problem is, when I choose an other value, for exemple HEA260 , the values are displayed but the menu return to HEA100
Image

The link:
http://faouweb.net/profile/hea.php

I have the same on a local server (WAMP) here is the code:

Code: Select all


<?php
$message = '';
$db = new mysqli('localhost', 'root', '', 'profile');
if ($db->connect_error) {
    $message = $db->connect_error;
} else {
    $sql = 'SELECT * FROM heb ORDER BY `id` ASC ';
    $result = $db->query($sql);
    if ($db->error) {
        $message = $db->error;
    }
}
?>
 
 
 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>HEA</title>
 
        <link rel="stylesheet" type="text/css" href="./css/profile.css">
    </head>
 
    <body>
    <div id="bigbox">
        <form  method="post">
            <?php
            if($message){
                echo "$message";
            }
            echo "<select name='sub1'>";
            while ($row = $result -> fetch_assoc()) {
 
                echo "<option value='" . $row['nom'] . "'>" . $row['nom'] . "</option>";
            }
            echo "</select>";
            ?>
            <input type="submit" name="afficher" value="Afficher">
<?php
            if (isset($_POST['sub1'])) {
                $query = "SELECT * FROM heb WHERE nom = ?";
                $stmt = $db->prepare($query);
                $stmt->bind_param('s', $_POST['sub1']);
                $exec = $stmt->execute();
                if ($exec) {
                    $result = $stmt->get_result();
                    $data = $result->fetch_assoc();
                }
 
                echo '<p class="nom">' . $data['nom'] . "</p>";
                echo '<p class="hauteur">' . $data['hauteur'] . "</p>";
                echo '<p class="base">' . $data['base'] . "</p>";
                echo '<p class="tw">' . $data['tw'] . "</p>";
                echo '<p class="tf">' . $data['tf'] . "</p>";
                echo '<p class="rayon">' . "R" . $data['rayon'] . "</p>";
                echo '<p class="poids">' . "Poids(kg/m) = " . $data['poids'] . "</p>";
                echo '<p class="diametre">' . "Diamettre = ". $data['diametre'] . "</p>";
		echo '<p class="air">' . "Air section = ". $data['air'] . "</p>";
 
            }
 
            ?>
 
        </form>
 
    </div>
    </body>
    </html>
What I have to do :?

Thank you :D
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to keep a value in a select menu

Post by Celauran »

You need to add 'selected="selected"' to the current item in your select list. Also, you're only pulling specifics from the database once the form is submitted. If you want HEA100 to be displayed on load, you'll need to pull it from the DB if nothing else is selected.
john7911
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2015 6:21 am

Re: How to keep a value in a select menu

Post by john7911 »

Thank you,
I added these some code but the result is not good :cry:
http://faouweb.net/profile/hea.php

Code: Select all

        <form  method="post">
        <!--start-->
            <?php
            if($message){
                echo "$message";
            }
            echo "<select name='sub1'>";
   $query = "SELECT * FROM heb WHERE nom = ?";
                $stmt = $db->prepare($query);
                $stmt->bind_param('s', $sub1);
            echo "</select>";         
            
 $sub1 = isset($_POST['sub1']) ? $_POST['sub1'] : 'HEB 100'; 
            while ($row = $result -> fetch_assoc()) {
       $selected = ($row['nom'] == $sub1) ? ' selected="selected" ' : '';
       echo "<option value='" . $row['nom'] . "'>" . $row['nom'] . "</option>";
}

<!--end-->
            ?>
            <input type="submit" name="afficher" value="Afficher">
<?php
            if (isset($_POST['sub1'])) {
                $query = "SELECT * FROM hea WHERE nom = ?";
                $stmt = $db->prepare($query);
                $stmt->bind_param('s', $_POST['sub1']);

                $exec = $stmt->execute();
                if ($exec) {
                    $result = $stmt->get_result();
                    $data = $result->fetch_assoc();
                }

                echo '<p class="nom">' . $data['nom'] . "</p>";
                echo '<p class="hauteur">' . $data['hauteur'] . "</p>";
                echo '<p class="base">' . $data['base'] . "</p>";
                echo '<p class="tw">' . $data['tw'] . "</p>";
                echo '<p class="tf">' . $data['tf'] . "</p>";
                echo '<p class="rayon">' . "R" . $data['rayon'] . "</p>";
                echo '<p class="poids">' . "Poids = " . $data['poids'] . " kg/m</p>";
                echo '<p class="diametre">' . "Diamettre (D) = ". $data['diametre'] . "</p>";
		echo '<p class="air">' . "Air section = ". $data['air'] . " cm<SUP>2</SUP></p>";
		echo '<p class="air">' . "P_Min = ". $data['p-min'] . " mm</p>";
		echo '<p class="air">' . "P_Max = ". $data['p-max'] . " mm</p>";


            }

            ?>

        </form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to keep a value in a select menu

Post by Celauran »

What if you tried like this

Code: Select all

<?php

$message = '';

$db = new mysqli('localhost', 'root', '', 'profile');
if ($db->connect_error) {
    $message = $db->connect_error;
}

function getProductList($db)
{
    $query = "SELECT nom FROM heb ORDER BY id ASC";
    $result = $db->query($query);

    $data = [];
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

    return $data;
}

function getProductByName($db, $name)
{
    $query = "SELECT * FROM heb WHERE nom = ?";
    $stmt = $db->prepare($query);
    $stmt->bind_param('s', $name);
    $exec = $stmt->execute();

    if ($exec) {
        $result = $stmt->get_result();
        $data = $result->fetch_assoc();
    }

    return $exec ? $data : false;
}

$product_listing = getProductList($db);
if (empty($product_listing)) {
    $message = $db->error;
}

$product_name = isset($_POST['sub1']) ? $_POST['sub1'] : 'HEA 100';
$product_details = getProductByName($db, $product_name);
if (!$product_details) {
    $message = $db->error;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>HEA</title>

        <link rel="stylesheet" type="text/css" href="./css/profile.css">
    </head>

    <body>
        <div id="bigbox">
            <form  method="post">
                <?php if ($message): ?>
                    <?= $message; ?>
                <?php endif; ?>
                <select name="sub1">
                    <?php foreach ($product_listing as $row): ?>
                        <?php $selected = ($row['nom'] == $product_name) ? 'selected="selected"' : ''; ?>
                        <option value="<?= $row['nom']; ?>" <?= $selected; ?>><?= $row['nom']; ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="submit" name="afficher" value="Afficher">
            </form>

            <?php if ($product_details): ?>
                <p class="hauteur"><?= $product_details['hauteur']; ?></p>
                <p class="base"><?= $product_details['base']; ?></p>
                <p class="tw"><?= $product_details['tw']; ?></p>
                <p class="tf"><?= $product_details['tf']; ?></p>
                <p class="rayon">R<?= $product_details['rayon']; ?></p>
                <p class="poids">Poids(kg/m) = <?= $product_details['poids']; ?></p>
                <p class="diametre">Diametre = <?= $product_details['diametre']; ?></p>
                <p class="air">Air section = <?= $product_details['air']; ?></p>
            <?php endif; ?>

        </div>
    </body>
</html>
john7911
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2015 6:21 am

Re: How to keep a value in a select menu

Post by john7911 »

Thank you very much Celauran ;)
Post Reply