I'm haveing a problem when coding the following form:
<select value="" required>
<option value = "0" >Choose Organization </option>
<option value="1" ' . $var1 . ' >Highschool</option>
<option value="2" ' . $var2 . '>University</option>
</select>
<label>Type of Organization</label>
What I want to do is: connect to the database, get the data from there (if Highschool, then = 1; If Uni, then = 2). I want the option to be selected for Highschool if the database returns 1 and Uni if it returns 2, but I do not know how. :(
Not really sure what you're trying to do... But I'm assuming is something similar to this?
<?php
function getDB() {
$host = "localhost";
$user = "root";
$password = "pass";
$dbname = "schools";
return new PDO(
sprintf(
'mysql:host=%s;dbname=%s;charset=utf8',
$host,
$dbname
),
$user,
$password
);
}
$db = getDB();
$stmt = $db->prepare('SELECT * FROM `database` WHERE value1 = :value1');
$stmt->bindValue(':value1', $value1);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<select value="" required>
<?php
foreach ($data as $value) {
if($value == "Highschool") {
echo '<option value="1">Highschool</option>';
}
if($value == "University") {
echo '<option value="2">University</option>';
}
}
?>
</select>
<label>Type of Organization</label>
you could change this
<select value="" required>
<option value = "0" >Choose Organization </option>
<option value="1" ' . $var1 . ' >Highschool</option>
<option value="2" ' . $var2 . '>University</option>
</select>
<label>Type of Organization</label>
to
<select name="name" class="form-control">
<?php
include('config_file.php');
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['id'].'">';
echo $row['route'].' :'.$row['type'].' :'.$row['time'];
echo '</option>';
}
?>
</select>