简体   繁体   中英

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!

you are passing the variables through the form to php-verarbeitung.php. On that page you can load the content based on the $_POST variables that are sent through the form. so you can query the DB again on the second page to get the info you need, and present formated html using the values from the form passed through the post array, in your case $_POST['vorname'], $_POST['name'] and $_POST['firmen'] should all be accessible when the php-verarbeitung.php page is called using your script above.

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