<?php
require_once("config.inc.php");
$db = dbconnect();
session_start();
//mitgegebene Parameter einlesen:

$action = $_GET['action'];


if($action == ''){

  if (isset($_GET['imtuid'])) {  //welcher Datensatz
 	$imtuid = $_GET['imtuid'];
# 	$_SESSION["anr_imtuid_$imtuid"] = $imtuid;
  }
  
  #if (isset($_SESSION["anr_imtuid"])){
  #      $imtuid = $_SESSION["anr_imtuid"];
  #}


//Anzeige des Buttons entsprechend grÃ¼n oder rot
      echo "<form action=\"?action=anrede_save\" method='post' name='erfassen'>";

      $query_anr_vorh = $db->query("SELECT arid
                                      FROM dep_user
                                     WHERE imtuid='$imtuid'");
      $row_anr_vorh  = $query_anr_vorh->fetch_array();
      $arid = $row_anr_vorh['arid'];
      
      $query_anr = "SELECT arid, bezeichnung
                      FROM dep_kat_anrede
                  ORDER BY 2";
    
      $result_anr = $db->query ($query_anr)
                 or die ("Cannot execute query_anr");
                 
      echo"<input type='hidden' name='user' value='$imtuid'>";
      
      echo "<select name=\"anrede\" onchange=\"this.form.submit()\">";
      echo "<option value=''>Bitte Anrede wählen</option>";
      while ($row_anr = $result_anr->fetch_array()){
       echo "<option value='$row_anr[arid]' "; if($arid == $row_anr['arid']){echo "selected";} echo">$row_anr[bezeichnung]&nbsp;&nbsp;&nbsp;</option>";
      }
      echo"</select>
           </form>
          ";
}
if($action == 'anrede_save'){
$anrede = $_POST['anrede'];
$user   = $_POST['user'];
#$imtuid = $_SESSION["anr_imtuid"];

  if($user != '' AND $anrede != ''){
    $sql1 = $db->query("UPDATE dep_user 
                           SET arid='$anrede'
                         WHERE imtuid='$user'
                       ");

    echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."?imtuid=$user\">";
  }else{
    echo "Unzulässige Eingabe";
    echo "<meta http-equiv=\"refresh\" content=\"1; URL=".$_SERVER['PHP_SELF']."?imtuid=$user\">";
  }


}


?>
