PHP / HTML - 读取表格提交后单选按钮处于活动状态的表格行

时间:2017-01-03 06:42:45

标签: php html mysql

我正在尝试创建一个网页,其中一张桌子列出了整整一个月的一顿饭。我希望能够在每行开头(1行 - 一天)中使用单选按钮来选择下一个用餐。 这背后的原因是我有另一个网页显示“下一餐:XXX”,其中XXX从数据库中读取(类似“where nextmeal = 1”)。 基本上我想要的是当我激活我的一个单选按钮时,此行中的一餐应该在数据库条目中得到nextmeal = 1。 数据库表包含以下 columns = date,meat,veggie,nextmeal 我尝试了一些关于如何向服务器发送活动单选按钮值的建议,但是我无法将正确的日期粘贴到服务器上以进行正确的查询。

表格网页的源代码:

<?php
require_once 'db_config.inc.php';
require_once 'functionsgerichte.php';
$db = new mysqli($db_server,$db_user,$db_password,$db_database);
if(!empty($_GET['monat'])) {$monat = $_GET['monat'];} //define default_month
else $monat = 14;
if(!empty($_GET['jahr'])) {$activ_year = $_GET['jahr'];} //define  default_year
else $activ_year = 2010;
?>
<html>
<style>
#gerichtetext {
width: 100%;
padding: 5px;
margin: 0px;
background-color:rgba(0, 0, 0, 0);
color:black;
border: none;
outline:none;
}
input[type=radio] {
width: 100%;
padding: 5px;
margin: 0px;
height: 50%;

}

</style>

<link rel="stylesheet" href="../includes/jqueryui/jquery-ui.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script src="../includes/jqueryui/jquery-ui.min.js"></script>

<body bgcolor = '#e3e3e3'>

<form action style='float:left'>
<select name='monat' size='1'>

<?php 
        $monate = array("","Januar","Februar","M&auml;rz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");  //
        for($i=1; $i<=12;$i++)
        {
            if($monat== 14) echo "<option value=".$i.">". $monate[$i] ."";
            else if($i == $_GET['monat']) echo "<option selected='selected' value=".$i.">". $monate[$i] ."";
            else echo "<option value=".$i.">". $monate[$i] ."";
        }
        echo "</select>";
        $jahrabfrage="SELECT DISTINCT YEAR(timestamp) as jahre FROM essenslog ORDER BY year(timestamp)";
        $jab = mysqli_query($db,$jahrabfrage);
        echo "<select name='jahr' size='1'>";
        while($year = mysqli_fetch_object($jab))
        {
            if($year->jahre == $_GET["jahr"]) echo "<option value='$activ_year' selected='selected'> $year->jahre</option>"; 
            else echo "<option value='$year->jahre'>$year->jahre</option>";
        }
    ?>
</select>
<input type='submit' value='send'>

</form>

<?php

$month=$_GET['monat'];  $year = $_GET['jahr']; // Variablen übergeben 

$unixtime = "$year-$month-21 12:00:00";         //21. Tag des Monats des Jahres

$unixmonat = date("t", strtotime($unixtime));   //Ermittlung der Anzahl der Tage des Monats des Jahres

$Tage = Array_Fill(1, $unixmonat,42);           //Auffüllen eines Arrays (von 1-Anzahl der Tage eines Monats)

//Ermittlung der Anzahl der DB-Einträge

$groesse = count($alle);

$tmp = array_keys($Tage);

$LetzterTag = end($tmp)+1;

$temptimer = 0;

$AlleTage = array();

for($i=0; $i<$LetzterTag;$i++) //Erstellung von je einem Object je Tag

{ 
$ActiveDay[$i] = new NeuerTag;      
array_push($AlleTage,$ActiveDay[$i]);

}       

$i = 0; //initialisieren des Tagecounters

?>

<form action='gericht2db.php' id='MyForm' method='get' >

<input type='hidden' value='<?php echo $month; ?>' name='monat'/>

<input type='hidden' value='<?php echo $year; ?>' name='jahr'/>

<input type='hidden' value='<?php echo $LetzterTag; ?>' name='letztertag'/>

<input type='hidden' value='' name='radio_row'/>

<input style='float:right' onclick='check()' id='submit-button' type='submit' value='SPEICHERN'/>

<table border ='1' width ='100%' height ='100%' style='clear:both'><tr bgcolor='#666666' style='color:white'><td width='5%'>NächstesEssen</td><td width='10%'>Wochentag</td><td width='10%'>Datum</td><td width='35%'>Fleisch</td><td width='35%'>Vegetarisch</td></tr>
<?php
for($i=1; $i<$LetzterTag;$i++)
{
    $time = "$year-$month-$i";
    if(date('l',strtotime($time)) == "Saturday" ||date('l',strtotime($time)) == "Sunday" || in_array(strtotime($time), $Freietage)) $ActiveDay[$i]->Feiertag = true;
    if($ActiveDay[$i]->Begin == 0)
    {
        $color = "#969696";
        if(date('l',strtotime($time)) == "Saturday" ||date('l',strtotime($time)) == "Sunday" || in_array(strtotime($time), $Freietage))
        {
            $color = "#86191a";
            $fontcolor = "#a1a1a1";
        }
        else
        {
            $color = "#969696";
            $fontcolor = "black";
        }           
        echo "<tr bgcolor='$color' style='color:$fontcolor'>";  
        echo "<td >";
        if(date('l',strtotime($time)) == "Saturday" ||date('l',strtotime($time)) == "Sunday" || in_array(strtotime($time), $Freietage))
        {
            echo "</td>";
        }
        else
        {
            echo "<input type='radio' id='radio_".$i."' name='radio' value='radio_".$i."' />";
            echo "</td>";
        }           
        echo "<td>".date('l',strtotime($time))."</td>";
        echo "<td>".date("d", strtotime($time)).".".date("m", strtotime($time)).".".date("Y", strtotime($time))."</td>";
        $fleisch=fleisch(date("Y", strtotime($time))."-".date("m", strtotime($time))."-".date("d", strtotime($time)));
        $veggie=veggie(date("Y", strtotime($time))."-".date("m", strtotime($time))."-".date("d", strtotime($time)));
        $temp="fleisch_".$i ;
        echo "<td >";
        if(date('l',strtotime($time)) == "Saturday" ||date('l',strtotime($time)) == "Sunday" || in_array(strtotime($time), $Freietage))
        {
            echo "</td>";
        }
        else
        {
            echo "<input type='text' id='gerichtetext' name='".$temp."' value='".$fleisch."'/></td>";
            echo "</td>";
        }
        $temp="veggie_".$i ;
        echo "<td>";
        if(date('l',strtotime($time)) == "Saturday" ||date('l',strtotime($time)) == "Sunday" || in_array(strtotime($time), $Freietage))
        {
            echo "</td>";
        }
        else
        {
            echo "<input type='text' id='gerichtetext' name='".$temp."' value='".$veggie."'/></td>";
            echo "</td>";
        }
        echo "</tr>";
    }
    echo "&nbsp</td>";


}

?>


</tr>
</table>

</form>

<script>

$('tbody').on('change', ':radio', function()

{
var line = $('input[name=radio]:checked').val();
// alert(line);

});

</script>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">


$.ajax({

 url: 'gericht2db.php',

type: 'POST',

data: 'radio_row=blablubeldiblub' ,

success: function(data) {
//Optional callback function on success

 }

});

</script>

</body>

</html>

表单的代码点击:

require_once 'db_config.inc.php';
require_once 'functionsgerichte.php';
$connection = mysql_connect($db_server,$db_user,$db_password);
mysql_select_db($db_database,$connection);
$LetzterTag = $_REQUEST['letztertag'];
$AlleTage = array();
$year= $_REQUEST['jahr'];
$month= $_REQUEST['monat'];
for($i=0; $i<$LetzterTag;$i++) { 
    //Erstellung von je einem Object je Tag
    $ActiveDay[$i] = new NeuerTag;      
    array_push($AlleTage,$ActiveDay[$i]);
}       
$i = 0 ;
for($i=1; $i<$LetzterTag;$i++) {
    $time = $year.'-'.$month.'-'.$i;
    if(date('l',strtotime($time)) == "Saturday" ||date('l',strtotime($time)) == "Sunday" || in_array(strtotime($time), $Freietage)) 
        $ActiveDay[$i]->Feiertag = true;

    if($ActiveDay[$i]->Begin == 0) {    
        $naechstesessen= $_REQUEST['naechstesessen'];
        $fleischrequest='fleisch_'.$i ;
        $fleisch= $_REQUEST[$fleischrequest];
        $veggierequest='veggie_'.$i ;
        $veggie= $_REQUEST[$veggierequest];
        $connection=mysql_connect('oris', 'root', 'orhro');
        mysql_select_db('anwesenheit',$connection);

        if ($naechstesessen== "radio_1") {
            $sql = "update gericht set naechstesessen='1'";         
            try {
                $query = mysql_query($sql);
            } catch (Exception $e) {
                echo $e->getMessage();
            }   
            $sql = "update gericht set naechstesessen='1' where Datum='".$time."'";
            try {
                $query = mysql_query($sql);
            } catch (Exception $e) {
                echo $e->getMessage();
            }               
        }
        $sql = "DELETE from gericht where Datum='".$time."'";
        try {
            $query = mysql_query($sql);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        $sql = "INSERT INTO gericht (Datum,Fleisch,Vegetarisch) VALUES ('".$time."','".$fleisch."','".$veggie."')";
        try {
            $query = mysql_query($sql);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}
Header("Location: ".$_SERVER['HTTP_REFERER']."");

编辑:在点击表单后,我还希望保持之前单击的单选按钮保持活动状态,将表单添加到源代码中。

2 个答案:

答案 0 :(得分:1)

这是答案......

将收音机类型名称视为收音机[]

示例"<input type='radio' id='radio_".$i."' name='radio[]' value='radio_".$i."' />;

“radio []”中存储的无线电值

在提交busson clcik上获取类似

的数组
$data = $_POST['radio'];
Print_r($data);

我希望它对你有帮助..

答案 1 :(得分:0)

我已根据您的用户界面创建了HTML。

执行操作就像用户可以选择任何单选按钮并单击存储在数据库中的SAVE按钮单选按钮值。

在页面页面上加载先前的选择。

我已经捕获了同样的视频,请看一次,如果您对此有任何顾虑,请告诉我。

网址:http://screencast.com/t/ebTv28LHT4 PHP

<?php
$db_server = "localhost";
$db_user = "root";
$db_password = "";
$db_database = "team44";

$db = mysqli_connect($db_server, $db_user, $db_password, $db_database);

  $suc="";
if (isset($_POST['save'])) {
    $naechstesessen = $_POST['naechstesessen'];
    $jahrabfrage = "INSERT INTO post VALUES (NULL,'" . $naechstesessen . "','details')";
    $jab = mysqli_query($db, $jahrabfrage);

    if($jab){
        $suc= "Data Inserted";
    }
}
?>

HTML 强文

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <style>
            table {
                border-collapse: collapse;
                width:100%;
            }
            table  th {
                background: rgb(102, 102, 102) none repeat scroll 0 0;
                color: rgb(255, 255, 255);
            }
            table  td {
                background: #969696;
                color: #000;
            }
            table, td, th {
                border: 2px solid #878787;
                text-align: left;
                font-size: 11px;
                letter-spacing: 1px;
            }
            td:first-child input {
                display: block;
                margin: 0 auto;
                text-align: center;
            }
            td:first-child,
            td:nth-child(2) {
                width: 10%;
            }
            .speichern {
                float: right;
            }
            div.table-responsive {
                overflow-x: auto;
            }
            .bg-red td{background:#86191A;color:#969696;}
            @media(max-width:767px){table{width:1000px;}}
            .month,.year,.send,.speichern{ font-size: 10px;
                                           font-weight: bold;}
            </style>
        </head>
        <body>
            <select class="month">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="opel">Opel</option>
            <option value="audi">Audi</option>
        </select>
        <select class="year">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="opel">Opel</option>
            <option value="audi">Audi</option>
        </select>
        <form method="POST">

            <input type="submit" class="send" value="send" name="send" />
            <input type="submit" class="speichern" value="save" name="save" />
            <div class="table-responsive">

                <table>
                    <tr>
                        <th>NachstesEssen</th>
                        <th>Wochentag</th>
                        <th>Datum</th>
                        <th>Fleisch</th>
                        <th>Vegetarisch</th>
                    </tr>
                    <?php
                    $select = "select naechstesessen from post order by id DESC LIMIT 1";
                    $result = mysqli_query($db, $select);
                    $row = mysqli_fetch_assoc($result);
                    ?>
                    <tr>
                        <td><input type="radio" name="naechstesessen"  value="1.11.2016" <?php
                            if ($row['naechstesessen'] == '1.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen"  value="2.11.2016" <?php
                            if ($row['naechstesessen'] == '2.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="3.11.2016"> <?php
                            if ($row['naechstesessen'] == '3.11.2016') {
                                echo "checked";
                            }
                            ?></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="4.11.2016" <?php
                            if ($row['naechstesessen'] == '4.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr class="bg-red">
                        <td><input type="radio" name="naechstesessen" value="5.11.2016" <?php
                            if ($row['naechstesessen'] == '5.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr class="bg-red">
                        <td><input type="radio" name="naechstesessen" value="6.11.2016" <?php
                            if ($row['naechstesessen'] == '6.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="7.11.2016" <?php
                            if ($row['naechstesessen'] == '7.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="8.11.2016" <?php
                            if ($row['naechstesessen'] == '8.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="9.11.2016" <?php
                            if ($row['naechstesessen'] == '9.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="10.11.2016" <?php
                            if ($row['naechstesessen'] == '10.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="12.11.2016" <?php
                            if ($row['naechstesessen'] == '12.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr class="bg-red">
                        <td><input type="radio" name="naechstesessen" value="13.11.2016" <?php
                            if ($row['naechstesessen'] == '13.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr class="bg-red">
                        <td><input type="radio" name="naechstesessen" value="14.11.2016" <?php
                            if ($row['naechstesessen'] == '14.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="15.11.2016" <?php
                            if ($row['naechstesessen'] == '15.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="16.11.2016" <?php
                            if ($row['naechstesessen'] == '16.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="17.11.2016" <?php
                            if ($row['naechstesessen'] == '17.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="18.11.2016" <?php
                            if ($row['naechstesessen'] == '18.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr class="bg-red">
                        <td><input type="radio" name="naechstesessen" value="19.11.2016" <?php
                            if ($row['naechstesessen'] == '19.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr class="bg-red">
                        <td><input type="radio" name="naechstesessen" value="20.11.2016" <?php
                            if ($row['naechstesessen'] == '20.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="21.11.2016" <?php
                            if ($row['naechstesessen'] == '21.11.2016') {
                                echo "checked";
                            }
                            ?> ></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><input type="radio" name="naechstesessen" value="22.11.2016" <?php
                            if ($row['naechstesessen'] == '22.11.2016') {
                                echo "checked";
                            }
                            ?>></td>
                        <td>Peter</td>
                        <td>Griffin</td>
                        <td></td>
                        <td></td>
                    </tr>

                </table>
                <center><h1 style="color: green"><?php echo $suc; ?></h1></center>
            </div>
        </form>
    </body>
</html>

以下是相同

的代码