< >

felder-dynamisch-mit-js-erstellen.php


Quell Code


<body>
<form method="GET">Erstelle 
<input type="number" max="20" name="menge" value="<?php if(isset($_GET['menge'])){ echo $_GET['menge'];}else{echo 1;} ?>" require>
 Charaktere mit Max <input type="number" name="max" value="<?php if(isset($_GET['max'])){ echo $_GET['max'];}else{echo 100;} ?>" max="1000" step="10" require>
<input type="submit">
</form>


<?php  
$max=0;
if(isset($_GET['menge'])<=20 AND isset($_GET['max'])<=1000){

     if($_GET['menge']<=20){
         $menge=htmlspecialChars($_GET['menge']);
     }else{
         $menge=20;
     }

     if($_GET['max']<=1000){
         $max=htmlspecialChars($_GET['max']);
     }else{
         $max=1000;
     }

   
     for ($i=1; $i <= $menge; $i++) { 
    
        echo "<tr><td> <p class='tdname'>Sieler $i (derzeit $max)</p> </td>
                  <td><input class='inputfieldlep' name='h_lep$i' id='h_lep$i' type='number'value=''>
                      <input class='inputfieldlep' type='number' name='h_lep_max' value='' id='h_lep_max$i'>
                      <span id='text$i'> </span></td></tr>";
     }
}else{ 
    echo "Eingaben Falsch";
}
?>
<script>
var maxwerte=<?php echo $max; ?>;
dd=document.querySelectorAll('.inputfieldlep');
dd.forEach((u)=>{
    u.addEventListener('click',function(){
 
        if (u.value<=maxwerte/4*1) {
            u.nextElementSibling.value=maxwerte/4*1;
        }else
        if (u.value<=maxwerte/4*2) {
            u.nextElementSibling.value=maxwerte/4*2;
        }else
        if (u.value<=maxwerte/4*3) {
            u.nextElementSibling.value=maxwerte/4*3;
        }else
        if (u.value<=maxwerte/4*4) {
            u.nextElementSibling.value=maxwerte/4*4;
        }
   });
});
</script>
</body>