lundi 20 avril 2015

PHP work with dropdown values from mySQL-db

so I've finally figured out how I can get values from my mySQL-db into a dropdown form in PHP. My problem is, how can I use/work with the option that was selected by the user? My goal is a functioning email-signature generator. I want the user to be able to insert unique data like their name, and select the office where they work from the dropdown form. After they hit the submit button, they should then be lead to the next site that displays their name with the signature for the selected office. The name is no problem, that is only a simple html-form with a post method, I know how I can retrieve and access that. But how can I work with the option that was selected by the user in the dropdown? After selecting an option and hitting submit, the whole "row" in the mySQL-db should be displayed, in formated form.

My current situation is that the dropdown shows the correct values from the mySQL-db, and the two name fields, that are also working as they should. The name of the mySQL database is "firmen", the name of the column I use is "niederlassung".

I'm happy to provide more information if needed. My code:

[...]
<form action="php-verarbeitung.php" method="post">
<div>
  <label for="1">Vorname: </label>
  <input type="text" name="vorname" id="1" value=""><br><br>
</div> 
<div>
  <label for="2">Nachname: </label>
  <input type="text" name="name" id="2" value=""><br><br>
</div> 

<input type = "submit" name = "button" value = "Senden"><br><br>


<?php
// Establish mySQL PDO Connection
$server='mysql:host=*****.com.mysql;dbname=*****'; // Host and DB-Name
$user="******";       // Username
$password="*****";          // Password

$pdo=new PDO($server, $user, $password);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Write out the query
$query = "SELECT niederlassung FROM firmen";

// Execute it, or let it throw an error message if there's a problem
$stmt = $pdo->query($query);

$dropdown = "<SELECT name='firmen'>";
foreach ($stmt as $row) {
  $dropdown .= "\r\n<option value='{$row['niederlassung']}'>{$row['niederlassung']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
</form>
</body>
</html>

Thanks for any help in advance!

Aucun commentaire:

Enregistrer un commentaire