Page 1 of 1

How to keep a value in a select menu

Posted: Fri Jan 22, 2016 6:03 am
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

Re: How to keep a value in a select menu

Posted: Fri Jan 22, 2016 7:41 am
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.

Re: How to keep a value in a select menu

Posted: Fri Jan 22, 2016 8:01 am
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>

Re: How to keep a value in a select menu

Posted: Fri Jan 22, 2016 8:21 am
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>

Re: How to keep a value in a select menu

Posted: Fri Jan 22, 2016 1:20 pm
by john7911
Thank you very much Celauran ;)