<?php
 
include("class/autoloader.php");
 
 
//Mysqli-Datenbank-Verbindung
 
include ("connection_data.php"); //in connection_data.php werden $server, $user, $passwort und $db definiert
 
$mysqli= new MySQLi($server, $user, $passwort, $db);
 
if($mysqli->connect_errno) {
 
    echo $mysqli->connect_error;
 
}    
 
 
if(isset($_GET["edit"])){
 
    $id=$_GET["edit"];
 
}else{
 
    $id=0;
 
}
 
try{
 
//Databinding-Procedure anlegen
 
$databinding=new Jb_Databinding_Procedure();
 
//Formular anlegen
 
$form=new Jb_Form_Model_Form();
 
//Textfeld
 
$textfeld= new Jb_Form_Model_FormElement();
 
$textfeld->name="name";
 
$textfeld->label="Name";
 
$textfeld->setEditoption("textfeld");
 
 
//Verbindung zwischen Databinding und Formular herstellen
 
$databinding->addPostBinder($textfeld);
 
//Verbindung zwischen Databinding und Datenbank herstellen
 
$mysqliBinder = new Jb_Databinding_MysqliBinder($mysqli,"user","name",$id);
 
$databinding->addReadWriteBinder($textfeld,$mysqliBinder);
 
//Textfeld zum Formular hinzufügen
 
$form->addElement($textfeld);    
 
 
//Submitbutton
 
$button=new  Jb_Form_Model_FormElement();
 
$button->name="Submit";
 
$button->value="Absenden";
 
$button->setEditoption('submit');
 
$form->addElement($button);
 
 
 
//Diesen Block am Besten komplett so übernehmen
 
 
//Leere Felder erlauben, wenn das Formular abgesendet wurde
 
if(!empty($_POST)){
 
    $databinding->allowempty=1;
 
}
 
//Databinding durchführen, wenn das Formular abgesendet wurde, oder $_GET["edit"] existiert
 
if(!empty($_POST) or isset($_GET["edit"])){
 
    if($databinding->bindElements()==true){
 
        $id=$mysqliBinder->id;    
 
        //Erfolgsmeldung
 
        $meldung="Eintrag erfolgt! (".$id.") \\n".date("d.m.Y - H:i:s");
 
        //Weiterleitungsadresse definieren (weitere $_GET-Parameter werden übernommen)
 
        if(!isset($_GET["edit"])){
 
            $href=$_SERVER['SCRIPT_NAME']."?edit=".$id;
 
            if(!empty($_SERVER["argv"])){
 
                for($i=0;$i<$_SERVER["argc"];$i++){
 
                    $href.="&".$_SERVER["argv"][$i];
 
                }
 
            }    
 
        }
 
    }
 
}else{
 
    $databinding->deleteSession();
 
}
 
 
//output muss hinter Databinding stehen!
 
$viewForm=new Jb_Form_View_HtmlForm($form);
 
}catch( Jb_Exception $e){
 
    echo $e->getExceptionDetails();
 
}
 
 
?>
 
<html>
 
<head>
 
<title>Jb_Databinding einfaches Mysqli-Formular</title>
 
<script type="text/javascript">
 
 
function alerter(){
 
<?php if(!empty($meldung)){?>
 
alert('<?php echo $meldung?>');
 
 
<?php 
 
    if(isset($href)){?>
 
    window.location.href='<?php echo $href?>';
 
    <?php 
 
    }
 
} 
 
?>
 
}
 
window.onload=function(){
 
alerter();
 
}
 
</script>
 
</head>
 
<body>
 
<?php 
 
//Formular anzeigen
 
//print_r($viewForm);
 
$viewForm->output();
 
 
?>
 
</body>
 
</html>
 
 |