0

I am beginner in PHP. In edit section I have defined a Do-While loop to get the information from data base. But when the user choose from the drop-down and submit the selected item disappears and fist option appears on drop-down. How Can I retrieve and echo selected items after submitting.

<?php
  $cat_sql= "SELECT * FROM cars";
        $cat_sql_query=mysql_query($cat_sql);
        $cat_sql_row=mysql_fetch_assoc($cat_sql_query);
?>

<form action="MSPEditstock.php" method="POST">
<span style="font-size: 18px; margin:10px;">Please Choose your car from the dropdown:</span>
  <select id="sortmethod" name="editstock" >
  <?php do{?>
  <option value="<?php echo $cat_sql_row['id']; ?>"  > <?php echo $cat_sql_row['Brand'].$cat_sql_row['Model'];?> </option>
  <?php ; }while($cat_sql_row=mysql_fetch_assoc($cat_sql_query))?>
 </select>
 <input type="submit" value="Start Editting" class="fvisual">
</form>
Peter
  • 19
  • 6
  • Try getting rid of the javascript. It sounds like client side changing it after rendering. – Matt Williamson May 08 '17 at 21:12
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 08 '17 at 21:12

1 Answers1

0

If I understood correctly you wish for the value selected by a user before submitting the form to be selected in the dropdown menu once the page has reloaded? Hopefully the following will give an idea as to how you could achieve that.

<?php
    $cat_sql= "SELECT * FROM cars";
        $cat_sql_query=mysql_query( $cat_sql );
        $cat_sql_row=mysql_fetch_assoc( $cat_sql_query );

    $editstock=!empty( $_POST['editstock'] ) ? $_POST['editstock'] : false;
?>

<form action="MSPEditstock.php" method="POST">
    <span style="font-size: 18px; margin:10px;">Please Choose your car from the dropdown:</span>
    <select id="sortmethod" name="editstock" >
        <?php

        while( $cat_sql_row=mysql_fetch_assoc( $cat_sql_query ) ){

            $id=$cat_sql_row['id'];
            $brand=$cat_sql_row['Brand'];
            $model=$cat_sql_row['Model'];
            $selected=$editstock && $editstock==$id ? ' selected ' : '';

            echo "<option value='{$id}'{$selected}>{$brand}{$model}";
        }

        ?>
    </select>
    <input type="submit" value="Start Editting" class="fvisual">
</form>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46